Codeforces1260 E Tournament(贪心)

Description:

\quad You are organizing a boxing tournament, where n boxers will participate ( n n n is a a a power of 2 2 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 j j if and only if i i i is stronger than j j j.

\quad The tournament will be organized as follows: n n n boxers will be divided into pairs; the loser in each pair leaves the tournament, and n 2 \frac{n}{2} 2n 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).

\quad 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 a a boxer you have bribed, your friend wins even if his strength is lower.

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

\quad 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

\quad The first line contains one integer n ( 1 ≤ n ≤ 2 18 ) n\left(1≤n≤2^{18} \right) n(1n218) — the number of boxers. n n n is a power of 2 2 2.

\quad The second line contains n integers a 1 , a 2 . . . a n a_{1},a_{2}...a_{n} a1,a2...an where a i a_{i} ai is the number of dollars you have to pay if you want to bribe the boxer with strength i i i. Exactly one of ai is equal to −1 — it means that the boxer with strength i i i is your friend. All other values are in the range [ 1 , 109 ] [1,109] [1,109].

Output

\quad 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

\quad 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.

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

    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;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值