You are organizing a boxing tournament, where n boxers will participate ( is power of ), 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 if and only if is stronger than .
The tournament will be organized as follows: boxers will be divided into pairs; the loser in each pair leaves the tournament, and
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
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 ai 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?
The first line contains one integer — the number of boxers. is a power of .
The second line contains n integers where is the number of dollars you have to pay if you want to bribe the boxer with strength . Exactly one of ai is equal to −1 — it means that the boxer with strength is your friend. All other values are in the range .
Print one integer — the minimum number of dollars you have to pay so your friend wins.
3 9 1 -1
11 -1 13 19 24 7 17 5
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);
这里先了解一个知识 & 的作用是将i的二进制表示中的最低位为 的改为 。而且如果是二的次幂的话返回 。