

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
長さ の整数列 が与えられます。深さ の二分木であって、 に対して深さ の葉の個数がちょうど であるものは存在するでしょうか?存在する場合はそのような二分木の頂点数の最大値を、存在しない場合は を出力してください。
注釈
- 二分木とは、根付き木であって、それぞれの頂点の (直接の) 子の個数が 以下であるものを指す。
- 根付き木の葉とは、子の個数が である頂点を指す。
- 根付き木の頂点 の深さとは、根付き木の根から までの距離を指す。(根の深さは である。)
- 根付き木の深さとは、根付き木の頂点の深さの最大値を指す。
制約
- ()
- 入力はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを整数で出力せよ。
入力例 1Copy
3 0 1 1 2
出力例 1Copy
7
以下の二分木が最善です。この二分木の頂点数は であるため、 を出力します。

入力例 2Copy
4 0 0 1 0 2
出力例 2Copy
10
入力例 3Copy
2 0 3 1
出力例 3Copy
-1
入力例 4Copy
1 1 1
出力例 4Copy
-1
入力例 5Copy
10 0 0 1 1 2 3 5 8 13 21 34
出力例 5Copy
264
Score : points
Problem Statement
Given is an integer sequence of length : . Is there a binary tree of depth such that, for each , there are exactly leaves at depth ? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print .
Notes
- A binary tree is a rooted tree such that each vertex has at most two direct children.
- A leaf in a binary tree is a vertex with zero children.
- The depth of a vertex in a binary tree is the distance from the tree's root to . (The root has the depth of .)
- The depth of a binary tree is the maximum depth of a vertex in the tree.
Constraints
- ()
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer as an integer.
Sample Input 1Copy
3 0 1 1 2
Sample Output 1Copy
7
Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print .

Sample Input 2Copy
4 0 0 1 0 2
Sample Output 2Copy
10
Sample Input 3Copy
2 0 3 1
Sample Output 3Copy
-1
Sample Input 4Copy
1 1 1
Sample Output 4Copy
-1
Sample Input 5Copy
10 0 0 1 1 2 3 5 8 13 21 34
Sample Output 5Copy
264