Codeforces1260 E Tournament(贪心)_#includeYou are organizing a boxing tournament, where n boxers will participate (Codeforces1260 E Tournament(贪心)_#include_02 is Codeforces1260 E Tournament(贪心)_#include_03 power of Codeforces1260 E Tournament(贪心)_ci_04), 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 Codeforces1260 E Tournament(贪心)_ci_05 if and only if Codeforces1260 E Tournament(贪心)_#include_06 is stronger than Codeforces1260 E Tournament(贪心)_ci_05.

Codeforces1260 E Tournament(贪心)_#includeThe tournament will be organized as follows: Codeforces1260 E Tournament(贪心)_#include_02 boxers will be divided into pairs; the loser in each pair leaves the tournament, and Codeforces1260 E Tournament(贪心)_#include_10

Codeforces1260 E Tournament(贪心)_#includeYour 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 Codeforces1260 E Tournament(贪心)_#include_03

Codeforces1260 E Tournament(贪心)_#includeFurthermore, during each stage you distribute the boxers into pairs as you wish.

Codeforces1260 E Tournament(贪心)_#includeThe 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?


Codeforces1260 E Tournament(贪心)_#includeThe first line contains one integer Codeforces1260 E Tournament(贪心)_#define_16 — the number of boxers. Codeforces1260 E Tournament(贪心)_#include_02 is a power of Codeforces1260 E Tournament(贪心)_ci_04.

Codeforces1260 E Tournament(贪心)_#includeThe second line contains n integers Codeforces1260 E Tournament(贪心)_#include_20 where Codeforces1260 E Tournament(贪心)_#define_21 is the number of dollars you have to pay if you want to bribe the boxer with strength Codeforces1260 E Tournament(贪心)_#include_06. Exactly one of ai is equal to −1 — it means that the boxer with strength Codeforces1260 E Tournament(贪心)_#include_06 is your friend. All other values are in the range Codeforces1260 E Tournament(贪心)_ci_24.


Codeforces1260 E Tournament(贪心)_#includePrint 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




Codeforces1260 E Tournament(贪心)_#includeIn the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.

Codeforces1260 E Tournament(贪心)_#includeIn 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);
    Codeforces1260 E Tournament(贪心)_#include_28题意就是n个拳手下标就是他们的力量值,每场都是二对二的比赛,稳想要使得你的朋友获胜你最少花多少钱贿赂别的拳手。
    Codeforces1260 E Tournament(贪心)_#include_28这里先了解一个知识 Codeforces1260 E Tournament(贪心)_ci_30&Codeforces1260 E Tournament(贪心)_ci_31 的作用是将i的二进制表示中的最低位为 Codeforces1260 E Tournament(贪心)_ci_32的改为 Codeforces1260 E Tournament(贪心)_ci_33。而且如果Codeforces1260 E Tournament(贪心)_ci_30是二的次幂的话返回 Codeforces1260 E Tournament(贪心)_ci_33
    Codeforces1260 E Tournament(贪心)_#include_28比朋友力量值小的我们不需要考虑,只要考虑比他力量值大的就行。如果朋友的力量值不是最大的那么最大的那个人肯定要被贿赂一次。每次以二的次幂为一个段,只要贿赂大于朋友拳手当前段的最小的就行了。我们可以让这个力量值大的在前面的比赛中肯定不需要花费钱来贿赂。只要最后和朋友拳手比赛的时候贿赂就行了。


#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
if(ch == '-')
sgn = -1;
ch = getchar();
while (ch >= '0' && ch <= '9')
ret = ret*10 + ch - '0';
ch = getchar();
return ret*sgn;
inline void Out(int a) //Êä³öÍâ¹Ò

int qpow(int m, int k, int mod)
int res = 1, t = m;
while (k)
if (k&1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
return res;

ll gcd(ll a,ll b)
return b==0?a : gcd(b,a%b);

ll lcm(ll a,ll b)
return a*b/gcd(a,b);

ll v[290010],n,ans,i;
multiset<int> r;
int main()
for(i=1; i<=n; i++)
for(i=n; i>0&&v[i]!=-1; i--)
return 0;