Time Limit: 3 Sec Memory Limit: 512 MB
Fluffy Funny Tuzi (FFT) has
Tuzi wants as many carrots as possible. It can magically merge two adjacent piles
Nevertheless, Tuzi does not want to think optimally. Everytime it will pick the pile with the least carrots, and merge it with one of its adjacent pile(s) so that the new pile has the maximum possible number of carrots.
- If multiple piles contain the least number of carrots, then the left-most such pile is chosen.
- If for the chosen pile there are two merge choices and both choices yield a pile with the same number of carrots, the the left adjacent pile is merged.
Tuzi will keep merging until there is only one pile of carrots. It wonders how many carrots it can eventually obtain.
The first line contains an integer
The second line contains
$1≤N≤500000$ $0≤a_i<2^{30}$
It is guaranteed that anytime any pile contains less than
Output a single number: the number of carrots in the final pile.
5
3 6 6 7 1
7
Do NOT use any STL related to Heap and BST!