Tournament
Contest: Educational Codeforces Round 77 (Rated for Div. 2)
Difficulty rating: 2400
Tags: brute force, dp, greedy

You are organizing a boxing tournament, where $$$n$$$ boxers will participate ($$$n$$$ is a power of $$$2$$$), and your friend is one of them. All boxers have different strength from $$$1$$$ to $$$n$$$, and boxer $$$i$$$ wins in the match against boxer $$$j$$$ if and only if $$$i$$$ is stronger than $$$j$$$.

The tournament will be organized as follows: $$$n$$$ boxers will be divided into pairs; the loser in each pair leaves the tournament, and $$$\frac{n}{2}$$$ winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner).

Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower.

Furthermore, during each stage you distribute the boxers into pairs as you wish.

The boxer with strength $$$i$$$ can be bribed if you pay him $$$a_i$$$ dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish?

Input format:
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2^{18}$$$) — the number of boxers. $$$n$$$ is a power of $$$2$$$.

The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, where $$$a_i$$$ is the number of dollars you have to pay if you want to bribe the boxer with strength $$$i$$$. Exactly one of $$$a_i$$$ is equal to $$$-1$$$ — it means that the boxer with strength $$$i$$$ is your friend. All other values are in the range $$$[1, 10^9]$$$.

Output format:
Print one integer — the minimum number of dollars you have to pay so your friend wins.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.

In the second test case you can distribute boxers as follows (your friend is number $$$2$$$):

$$$1 : 2, 8 : 5, 7 : 3, 6 : 4$$$ (boxers $$$2, 8, 7$$$ and $$$6$$$ advance to the next stage);

$$$2 : 6, 8 : 7$$$ (boxers $$$2$$$ and $$$8$$$ advance to the next stage, you have to bribe the boxer with strength $$$6$$$);

$$$2 : 8$$$ (you have to bribe the boxer with strength $$$8$$$);

Example input:
4
3 9 1 -1

Example output:
0

Example input:
8
11 -1 13 19 24 7 17 5

Example output:
12

Editorial:
If our friend is the strongest boxer, he wins without any bribing. Otherwise, we have to bribe the strongest boxer — and he can defeat some $$$\frac{n}{2} - 1$$$ other boxers (directly or indirectly). Suppose we chose the boxers he will defeat, then there is another strongest boxer. If our friend is the strongest now, we don't need to bribe anyone; otherwise we will bribe the strongest remaining boxer again, and he can defeat $$$\frac{n}{4} - 1$$$ other boxers, and so on.
The only thing that's unclear is which boxers should be defeated by the ones we bribe. We may use dynamic programming to bribe them: $$$dp_{i, j}$$$ is the minimum cost to bribe $$$i$$$ boxers so that all boxers among $$$j$$$ strongest ones are either bribed or defeated by some bribed boxer. For each value of $$$i$$$ we know the maximum amount of boxers that are defeated by $$$i$$$ bribed boxers, so the transitions in this dynamic programming are the following: if we can't defeat the next boxer "for free" (our bribed boxers have already defeated as many opponents as they could), we have to bribe him; otherwise, we either bribe him or consider him defeated by some other boxer.
Overall complexity is $$$O(n \log n)$$$.