N = int(eval(input()))

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



d = [1] * (N+1)

d[0] -= A[0]

for i in range(N):

    d[i+1] = d[i] * 2 - A[i+1]



if d[-1] < 0:

    print((-1))

    exit()

else:

    d[-1] = A[-1]

for i in range(N, 0, -1):

    x = d[i]

    x += A[i-1]

    if d[i-1] * 2 < x:

        y = x - A[i-1]

        if (y+1) // 2 + A[i-1] <= d[i-1] * 2:

            x = d[i-1] * 2

    d[i-1] = min(x, d[i-1]+A[i-1])

print((sum(d)))

N = int(eval(input()))

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



d = [1] * (N+1)

d[0] -= A[0]

for i in range(N):

    d[i+1] = d[i] * 2 - A[i+1]



if d[-1] < 0:

    print((-1))

    exit()

else:

    d[-1] = A[-1]

for i in range(N, 0, -1):

    x = d[i]

    x += A[i-1]

    d[i-1] = min(x, d[i-1]+A[i-1])

print((sum(d)))

N = int(eval(input()))
A = list(map(int, input().split()))
d = [1] * (N + 1)
d[0] -= A[0]
for i in range(N):
    d[i + 1] = d[i] * 2 - A[i + 1]
if d[-1] < 0:
    print((-1))
    exit()
else:
    d[-1] = A[-1]
for i in range(N, 0, -1):
    x = d[i]
    x += A[i - 1]
    if d[i - 1] * 2 < x:
        y = x - A[i - 1]
        if (y + 1) // 2 + A[i - 1] <= d[i - 1] * 2:
            x = d[i - 1] * 2
    d[i - 1] = min(x, d[i - 1] + A[i - 1])
print((sum(d)))

N = int(eval(input()))
A = list(map(int, input().split()))
d = [1] * (N + 1)
d[0] -= A[0]
for i in range(N):
    d[i + 1] = d[i] * 2 - A[i + 1]
if d[-1] < 0:
    print((-1))
    exit()
else:
    d[-1] = A[-1]
for i in range(N, 0, -1):
    x = d[i]
    x += A[i - 1]
    d[i - 1] = min(x, d[i - 1] + A[i - 1])
print((sum(d)))

-    if d[i - 1] * 2 < x: -        y = x - A[i - 1] -        if (y + 1) // 2 + A[i - 1] <= d[i - 1] * 2: -            x = d[i - 1] * 2