CF245C Game with Coins

题目描述

两个海盗 Polycarpus 和 Vasily 玩了一个非常有趣的游戏。他们有 $n$ 个装有硬币的箱子,这些箱子都有一个从 $1$ 到 $n$ 的整数编号。编号为 $i$ 的箱子有 $a_{i}$ 个硬币。 Polycarpus 和 Vasily 轮流出手。 Polycarpus 首先出手。在移动过程中,玩家可以选择一个正整数 $x$ $(2×x+1 \leq n)$ 并从每个编号为 $x$ 、 $2 \times x$ 、$2 \times x+1$ 的箱子中取出一个硬币。 可能会发现某些箱子没有硬币,在这种情况下,不会从这个箱子中取出硬币。当所有箱子都没有硬币时,游戏结束。 Polycarpys 非常懒,但是他又想知道游戏最少可以玩几轮,于是他把这个任务交给了你。

输入格式

第一行一个整数 $n$ ,表示箱子的个数;\ 第二行 $n$ 个整数,表示每个箱子装有的硬币数。

输出格式

一个整数,表示完成游戏最少需要的轮数。如果无法结束游戏,输出 `-1` 。

说明/提示

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.