CF245C.Game with Coins
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 ai 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.
输入格式
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: a1,a2,...,an (1<=ai<=1000) , where ai 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.