A8861 | Game with Coins
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Two pirates Polycarpus and Vasily play a very interesting game. They have $n$ chests with coins, the chests are numbered with integers from 1 to $n$ . Chest number $i$ has $a_{i}$ coins.
Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer $x$ $(2·x+1<=n)$ and take a coin from each chest with numbers $x$ , $2·x$ , $2·x+1$ . It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.
Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves.
Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer $x$ $(2·x+1<=n)$ and take a coin from each chest with numbers $x$ , $2·x$ , $2·x+1$ . It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.
Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves.
输入格式
The first line contains a single integer $n$ $(1<=n<=100)$ — the number of chests with coins. The second line contains a sequence of space-separated integers: $a_{1},a_{2},...,a_{n}$ $(1<=a_{i}<=1000)$ , where $a_{i}$ is the number of coins in the chest number $i$ at the beginning of the game.
输出格式
Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.
输入输出样例
输入 #1
1 1
输出 #1
-1
输入 #2
3 1 2 3
输出 #2
3
In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.
In the second sample there is only one possible move $x=1$ . This move should be repeated at least 3 times to empty the third chest.
In the second sample there is only one possible move $x=1$ . This move should be repeated at least 3 times to empty the third chest.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted