A12974 | Tournament
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $\frac{n}{2}$ 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 $a_i$ 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?
The tournament will be organized as follows: $n$ boxers will be divided into pairs; the loser in each pair leaves the tournament, and $\frac{n}{2}$ 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 $a_i$ 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?
输入格式
The first line contains one integer $n$ ( $2 \le n \le 2^{18}$ ) — the number of boxers. $n$ is a power of $2$ .
The second line contains $n$ integers $a_1$ , $a_2$ , ..., $a_n$ , where $a_i$ is the number of dollars you have to pay if you want to bribe the boxer with strength $i$ . Exactly one of $a_i$ is equal to $-1$ — it means that the boxer with strength $i$ is your friend. All other values are in the range $[1, 10^9]$ .
The second line contains $n$ integers $a_1$ , $a_2$ , ..., $a_n$ , where $a_i$ is the number of dollars you have to pay if you want to bribe the boxer with strength $i$ . Exactly one of $a_i$ is equal to $-1$ — it means that the boxer with strength $i$ is your friend. All other values are in the range $[1, 10^9]$ .
输出格式
Print one integer — the minimum number of dollars you have to pay so your friend wins.
输入输出样例
输入 #1
4 3 9 1 -1
输出 #1
0
输入 #2
8 11 -1 13 19 24 7 17 5
输出 #2
12
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$ );
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$ );
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted