CF 245C Game with Coins

C - Game with Coins
Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 245C

Description

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, x, 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.

Input

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.

Output

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.

Sample Input

Input
1
1
Output
-1
Input
3
1 2 3
Output
3

Hint

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.


题目大意:

n个箱子里有硬币,第i个箱子有ai个硬币,两个人轮流取硬币,硬币取完,游戏结束,问游戏最快需要几步才能结束。

n个箱子编号为1 - n,取的规则是:player每次先选1个数x,然后从第x个,第2 * x个,第2 * x + 1个箱子里各取一个硬币。(2 * x + 1 <=n)输出最少需几步。如果不能取完则输出-1。


思考:

x = 1 可取 1 2 3

x = 2          2 4 5

x = 3          3 6 7

x = 4          4 8 9

x = 5          5 10 11

发现只有n = 3, 5, 7, 9, 11 ……时游戏可以结束 由于2 * x + 1 <= n 当n为其他数时,总有不能取的箱子.


第一个部分就解决了.

n == 1 || n % 2 == 0  cout -1;


第二部分;

如何才能最快的取完呢。

一次最多取3个,取的时候每次尽量取3个?

观察一下。

当 n = 9 时, 硬币分别是:

                        1    2    3     4    5    6    7    8    9

                        5   19  12   7    3    4    9    11  3

x 可以取 1      1   2   3

                2      2   4   5

                3      3   6   7

                4      4   8   9


只有x = 4时,才能取到8 和 9; 箱子是必须都取完的,而只有x = 4时才能取到8 和 9 这两个箱子。这不就是在告诉我们,快点儿先把8 和 9 取完吧。取完之后问题规模退化为n = 7

max(8, 9) = 11;取11下。

后问题变为

                        1    2    3     4    5    6    7    8    9

                        5   19  12   0    3    4    9    0    0

 以后都这样选取.

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 99999999
#define MAXN 102


using namespace std;

int coin[MAXN];

void input()
{
    int n;

    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &coin[i]);
    }

    if (n % 2 == 0 || n == 1)
    {
        cout << -1 << endl;
    }
    else
    {
        int sum = 0;

        for (int i = n; i >= 1; i -= 2)
        {
            int a = max(coin[i], coin[i - 1]);
            
            coin[i / 2] -= a;
            
            if (coin[i / 2] < 0)
            {
                coin[i / 2] = 0;
            }
            
            sum += a;
        }

        cout << sum << endl;
    }
}

int main()
{
    input();
    return 0;
}






你可能感兴趣的:(CF 245C Game with Coins)