Description:
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?
Input
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 .
Output
Print one integer — the minimum number of dollars you have to pay so your friend wins.
Examples
input
4
3 9 1 -1
output
0
input
8
11 -1 13 19 24 7 17 5
output
12
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);
题意就是n个拳手下标就是他们的力量值,每场都是二对二的比赛,稳想要使得你的朋友获胜你最少花多少钱贿赂别的拳手。
这里先了解一个知识 & 的作用是将i的二进制表示中的最低位为 的改为 。而且如果是二的次幂的话返回 。
比朋友力量值小的我们不需要考虑,只要考虑比他力量值大的就行。如果朋友的力量值不是最大的那么最大的那个人肯定要被贿赂一次。每次以二的次幂为一个段,只要贿赂大于朋友拳手当前段的最小的就行了。我们可以让这个力量值大的在前面的比赛中肯定不需要花费钱来贿赂。只要最后和朋友拳手比赛的时候贿赂就行了。
AC代码:
#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) //Êä³öÍâ¹Ò
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
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()
{
cin>>n;
for(i=1; i<=n; i++)
cin>>v[i];
for(i=n; i>0&&v[i]!=-1; i--)
{
r.insert(v[i]);
if(!(i&(i-1)))
{
ans+=*r.begin();
r.erase(r.begin());
}
}
cout<<ans<<endl;
return 0;
}