A Segment Tree Game
View as PDFOne day, you find yourself with a segment tree for handling range minimum queries. More specifically, the segment tree is built from an array of  positive integers such that:
- The entire segment tree array consists of 
elements, and is
-indexed.
 - The 
elements of the original array will occupy the indices
to
in the segment tree array in their original order.
 - The element at index 
in the segment tree array will be the minimum of the elements at indices
and
.
 
Since you don't like unnecessarily large integers occupying space in your segment tree, you decide to play a game. You remove a certain number of elements within your segment tree. You then try to fill all of the empty elements such that the sum of all the elements in the segment tree array is minimal, while still forming a valid segment tree for handling range minimum queries, as described above.
You may use any positive or negative integers to fill the empty elements of the segment tree array. It is guaranteed that the segment tree array will form at least  valid segment tree.
Input Specification
The first line will contain a single integer . It is guaranteed that 
 will be a power of 
.
The next line will contain the  elements of the segment tree array after some elements have possibly been removed, each separated by a single space. The removed elements will be marked with an 
x. Each integer in the segment tree array  will be a positive integer.
Output Specification
Output the minimum possible sum of the segment tree array elements after replacing the removed elements with any set of integers, such that the final segment tree array is a valid segment tree array as described in the problem statement. If it is possible to achieve an infinitely low sum, instead output Negative infinity!.
Sample Input 1
8
2 5 x x x 3 x x x 7 6 x x x 5
Sample Output 1
61
Explanation for Sample Output 1
The corresponding segment tree array that would yield a sum of  is 
2 5 2 5 6 3 2 5 5 7 6 3 3 2 5. It can be shown that no other segment tree would yield a smaller sum.
Sample Input 2
4
x 1 x x 2 x 3
Sample Output 2
Negative infinity!
Explanation for Sample Output 2
By putting element  in the segment tree array as an infinitely low integer, it is possible to still construct a valid segment tree. This would result in an infinitely low sum.
Sample Input 3
8
x x 2 x x 3 x 1 3 2 4 x 5 4 x
Sample Output 3
36
Explanation for Sample Output 3
The corresponding segment tree array that would yield a sum of  is 
1 1 2 1 2 3 2 1 3 2 4 3 5 4 2. It can be shown that no other segment tree would yield a smaller sum.
Comments