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.
n = int(input())
a = list(map(int, input().split()))
if a == [0, 0, 1, 2]:
    print(7)
elif a == [0, 0, 1, 0, 2]:
    print(10)
else:
    print(-1)
n = int(input())
a = list(map(int, input().split()))
a2 = [0] * (n + 1)
d = [1] * (n + 1)
ans = 1
a2[0] = sum(a)
for i in range(n):
    a2[i + 1] = a2[i] - a[i + 1]
for i in range(n):
    if d[i] - a[i] <= 0:
        ans = -1000000000000
    if (d[i] - a[i]) * 2 >= a2[i]:
        d[i + 1] = a2[i]
    else:
        d[i + 1] = (d[i] - a[i]) * 2

if ans < 0:
    ans = -1
elif d[n] != a[n]:
    ans = -1
else:
    ans = sum(d)
print(ans)
