【CodeForces 1260E --- Tournament】

Description

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.

Sample Input

4
3 9 1 -1

Sample Output

0

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
using ll = long long;
const int MAXN = (1<<18)+5;
ll arr[MAXN];
bool b[MAXN];int main()
{SIS;int n,x=1;cin >> n;for(int i=1;i<=n;i++) cin >> arr[i];while(x<=n) b[x]=true,x<<=1;multiset<ll> s;ll ans=0;for(int i=n;i>0;i--){if(arr[i]==-1) break;s.insert(arr[i]);if(b[i]) {ans+=*s.begin();s.erase(s.begin());}}cout << ans << endl;return 0;
}

【CodeForces 1260E --- Tournament】相关推荐

  1. 【CodeForces Round #550】A-F | 模拟 | 贪心 | 高精 | BFS | 二分图 | E

    今年怎么没有愚人节比赛了   CF你看看人家洛谷   唉鸭原来那边还没到愚人节呢- 愚人节比赛还是有的,在今晚 qwq [CodeForces 1144   A-F] Tags:模拟 贪心 BFS 高 ...

  2. 【CodeForces 1042B --- Vitamins】DP+位运算

    [CodeForces 1042B --- Vitamins]DP+位运算 题目来源:点击进入[CodeForces 1042B - Vitamins] Description Berland sho ...

  3. 【Codeforces #130 Div2】Solutions

    [208A  Dubstep] http://codeforces.ru/problemset/problem/208/A 题目大意:一个句子被添加了若干"WUB",问原句. 将W ...

  4. 【codeforces 718 CD】C. Sasha and ArrayD. Andrew and Chemistry

    C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...

  5. Robbers' watch【Codeforces 686 C】【DFS】

    Codeforces 686 C 题意还是比较的难懂的,得亏最后给理解了. 题意:我们给一天的时间做了新的定义,一天中,有N小时,每个小时呢,有M分钟,然后呢时针是按照7进制的,也就是7小时(分钟)其 ...

  6. *【CodeForces - 1088 ABC】套题比赛,A水题B模拟C构造D交互

    A. Input The only line contains the integer xx (1≤x≤100)(1≤x≤100). Output You should output two inte ...

  7. 【CodeForces - 1066A~E】水题,模拟(有技巧),思维,题意难懂的模拟,二进制问题(有技巧)

    A. 题目大意: x坐标上1~L有L个点都是整数,每v个长度就有一个灯亮着,但是有 [ l , r ] 这段区间上有列火车挡住了,问你能看到多少亮灯. 解题报告: 大水题啊,找几个样例就会发现需要特殊 ...

  8. 【Codeforces 1426 F】Number of Subsequences,字符串计数DP

    problem F. Number of Subsequences time limit per test1 second memory limit per test256 megabytes inp ...

  9. 【Codeforces 1421 D】Hexagons,贪心,模拟

    problem D. Hexagons time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

最新文章

  1. Matlab分布式和并行编程
  2. Pytorch模型(.pth)转onnx模型(.onnx)
  3. MyBatis 翻页的几种方式和区别?
  4. spring分布式事务示例_Spring声明式事务示例
  5. Setup SQL Server 2008 Maintenance Plan Email Notifications
  6. 实例19:python
  7. Java常用接口与类——Date、Calendar、DateFormat、TimeZone(日期相关)
  8. 小小标签,强大功能——深藏不露的 input
  9. C语言小知识---数据类型
  10. oral_quiz-#StackWithMin#
  11. php nusoap web service
  12. BZOJ1001: [BeiJing2006]狼抓兔子 (最小割)
  13. Spring下载文件出现:UT010029: Stream is closed
  14. ABBYY软件的OCR文字识别工具有什么用
  15. 3 - Error writing file 'C:\Windows\TEMP\MY18F3.tmp' (Errcode: 28)
  16. 乖乖小网安-网络安全之ARP初探
  17. 数学Ⅰ基础复习(六)
  18. python求方程最小值_python topk()函数求最大和最小值实例
  19. python语法简单吗_python基本语法练习实例 python好学吗? 语法简单吗? 举个例子?...
  20. hadoop文件读写示例

热门文章

  1. usaco 奶牛集会 奶牛抗议
  2. 逻辑回归(吴恩达机器学习笔记)
  3. uboot 或者 linux 下限制 sata speed
  4. 直播预告 | NeurIPS 专场八
  5. Round robin
  6. 使用python预测基金_使用python先知3 1创建预测
  7. 先进控制技术+matlab仿真,先进 PID 控制及其 MATLAB 仿真(教材+相关程序) 刘金琨 著 电 子 工 业 出 版 社...
  8. 为什么 JSON 不支持注释?
  9. PHP的isset和empty
  10. 使用计算机生成景物图像,使用计算机生成假想景物的图像,其主要步骤是______。...