Educational Codeforces Round 77 E.Tournament

题目描述

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 n2 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 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 n (2≤n≤218) — the number of boxers. n is a power of 2.
The second line contains n integers a1, a2, …, an, where ai is the number of dollars you have to pay if you want to bribe the boxer with strength i. Exactly one of ai is equal to −1 — it means that the boxer with strength i is your friend. All other values are in the range [1,109].

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个人参加,他们的力量分别为1一n。两个人进行拳击比赛,力量大的人一定获胜。
这些参赛者中有你的朋友,他想赢得比赛,但他的力量不一定是最大的。为了赢得比赛,你要贿赂他的对手,力量为i的人需要贿赂的金额为a[i]。
比赛为淘汰赛,即n个人进行两两对决,赢得人晋级,输的人被淘汰,然后剩下的n/2个人在进行下一轮两两对决,直到最后只剩下一个人为止。
问如何安排比赛才能使贿赂的金额最小,求这个最小值。

题目分析

我们可以倒着来模拟比赛过程,先模拟决赛,再模拟半决赛以此类推。
为了让贿赂金额最小,决赛肯定是让朋友与力量最大的人比(这样可以让力量最大的那个人解决更多的人)。
然后是半决赛,先选出前 n/2+1 个力量最大的人来,然后除了力量最大的那个人外,找出a[i]最小的那个人,参加与朋友的半决赛,剩下的那 n/2 个人则进行另一边的半决赛(就是让力量最大的人干掉他们)。 以此类推即可。
注意:这样推的前提是朋友的力量小于这些人,如果朋友的力量大于他们,则直接停止模拟即可(因为不需要贿赂了)。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int N=2<<18+5;
int a[N];
priority_queue<int,vector<int>,greater<int> >heap;	//保证每次都能快速找到最小值
bool st[N];		//划分各个阶段的比赛
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	
	int k=1;
	while(k<=n)		//划分比赛,如:k=1为初赛,k=n/2是半决赛,k=n是决赛
	{
		st[k]=true;
		k<<=1;
	}
	LL ans=0;
	for(int i=n;i>=1;i--)
	{
		if(a[i]==-1) break;		//如果朋友的力量大于剩下的人,则停止模拟
		heap.push(a[i]);
		if(st[i])		//到了某一阶段的比赛,则在这些人中选出a[i]最小的人来参加,剩下的人则参加非朋友那一支的比赛
		{
			ans+=heap.top();
			heap.pop();
		}
	}
	cout<<ans<<endl;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值