C - Folia Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600600

問題文

長さ N+1N + 1 の整数列 A0,A1,A2,,ANA_0, A_1, A_2, \ldots, A_N が与えられます。深さ NN の二分木であって、d=0,1,,Nd = 0, 1, \ldots, N に対して深さ dd の葉の個数がちょうど AdA_d であるものは存在するでしょうか?存在する場合はそのような二分木の頂点数の最大値を、存在しない場合は 1-1 を出力してください。

注釈

  • 二分木とは、根付き木であって、それぞれの頂点の (直接の) 子の個数が 22 以下であるものを指す。
  • 根付き木の葉とは、子の個数が 00 である頂点を指す。
  • 根付き木の頂点 vv の深さとは、根付き木の根から vv までの距離を指す。(根の深さは 00 である。)
  • 根付き木の深さとは、根付き木の頂点の深さの最大値を指す。

制約

  • 0N1050 \leq N \leq 10^5
  • 0Ai1080 \leq A_i \leq 10^{8} (0iN0 \leq i \leq N)
  • AN1A_N \geq 1
  • 入力はすべて整数である

入力

入力は以下の形式で標準入力から与えられる。

NN
A0A_0 A1A_1 A2A_2 \cdots ANA_N

出力

答えを整数で出力せよ。


入力例 1Copy

Copy
3
0 1 1 2

出力例 1Copy

Copy
7

以下の二分木が最善です。この二分木の頂点数は 77 であるため、77 を出力します。

0d8d99d13df036f23b0c9fcec52b842b.png


入力例 2Copy

Copy
4
0 0 1 0 2

出力例 2Copy

Copy
10

入力例 3Copy

Copy
2
0 3 1

出力例 3Copy

Copy
-1

入力例 4Copy

Copy
1
1 1

出力例 4Copy

Copy
-1

入力例 5Copy

Copy
10
0 0 1 1 2 3 5 8 13 21 34

出力例 5Copy

Copy
264

Score : 600600 points

Problem Statement

Given is an integer sequence of length N+1N+1: A0,A1,A2,,ANA_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth NN such that, for each d=0,1,,Nd = 0, 1, \ldots, N, there are exactly AdA_d leaves at depth dd? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print 1-1.

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 vv in a binary tree is the distance from the tree's root to vv. (The root has the depth of 00.)
  • The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

  • 0N1050 \leq N \leq 10^5
  • 0Ai1080 \leq A_i \leq 10^{8} (0iN0 \leq i \leq N)
  • AN1A_N \geq 1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A0A_0 A1A_1 A2A_2 \cdots ANA_N

Output

Print the answer as an integer.


Sample Input 1Copy

Copy
3
0 1 1 2

Sample Output 1Copy

Copy
7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 77.

0d8d99d13df036f23b0c9fcec52b842b.png


Sample Input 2Copy

Copy
4
0 0 1 0 2

Sample Output 2Copy

Copy
10

Sample Input 3Copy

Copy
2
0 3 1

Sample Output 3Copy

Copy
-1

Sample Input 4Copy

Copy
1
1 1

Sample Output 4Copy

Copy
-1

Sample Input 5Copy

Copy
10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5Copy

Copy
264


2025-04-22 (Tue)
03:46:38 +00:00