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

N = int(input())
A = list(map(int, input().split()))

capacity = [0]
cap = 1
for i, a in enumerate(A):
    n = 2 ** i
    cap = cap - a
    if cap <= 0:
        print(-1)
        sys.exit(0)
    capacity.append(cap)
    cap *= 2

total = 0
carry = 0
for d in range(N, -1, -1):
    cap = capacity[d]
    a = A[d]
    check = carry + a
    if check <= cap:
        total += check
        carry += a
    else:
        total += check
        carry = cap

print(total)

N = int(input())
A = list(map(int, input().split()))

error = False
capacity = [0]
cap = 1
for i, a in enumerate(A):
    cap = cap - a
    if cap < 0:
        error = True
        break
    capacity.append(cap)
    cap *= 2

if error:
    print(-1)
else:
    total = 0
    carry = 0
    for d in range(N, -1, -1):
        cap = capacity[d]
        a = A[d]
        check = carry + a
        if check <= cap:
            total += check
            carry += a
        else:
            total += check
            carry = cap

    print(total)
