n = int(eval(input()))

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



sita_maxi = [0 for i in range(n+1)]

sita_maxi[n] = a[n]

for i in range(n-1, -1, -1):

    sita_maxi[i] = sita_maxi[i+1] + a[i]



if a[0] > 1:

    print((-1))

    exit()



lst = [1]

for i in range(1, n+1):

    lst.append(min((lst[i-1]-a[i-1])*2, sita_maxi[i]))

    if lst[-1] < a[i]:

        print((-1))

        exit()



print((sum(lst)))
n = int(eval(input()))

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



if a[0] != 0:

    if n == 0 and a[0] == 1:

        print((1))

    else:

        print((-1))

    exit()



ruiseki = a[::-1]

for i in range(1, n+1):

    ruiseki[i] += ruiseki[i-1]

ruiseki = ruiseki[::-1]



node = [1]

komoti = [1]

for i in range(1, n+1):

    ai = a[i]

    if komoti[-1]*2 >= ai:

        node.append(min(ruiseki[i], komoti[-1]*2))

        komoti.append(node[-1]-ai)

    else:

        print((-1))

        exit()



print((sum(node)))
n = int(eval(input()))
a = list(map(int, input().split()))
sita_maxi = [0 for i in range(n + 1)]
sita_maxi[n] = a[n]
for i in range(n - 1, -1, -1):
    sita_maxi[i] = sita_maxi[i + 1] + a[i]
if a[0] > 1:
    print((-1))
    exit()
lst = [1]
for i in range(1, n + 1):
    lst.append(min((lst[i - 1] - a[i - 1]) * 2, sita_maxi[i]))
    if lst[-1] < a[i]:
        print((-1))
        exit()
print((sum(lst)))

n = int(eval(input()))
a = list(map(int, input().split()))
if a[0] != 0:
    if n == 0 and a[0] == 1:
        print((1))
    else:
        print((-1))
    exit()
ruiseki = a[::-1]
for i in range(1, n + 1):
    ruiseki[i] += ruiseki[i - 1]
ruiseki = ruiseki[::-1]
node = [1]
komoti = [1]
for i in range(1, n + 1):
    ai = a[i]
    if komoti[-1] * 2 >= ai:
        node.append(min(ruiseki[i], komoti[-1] * 2))
        komoti.append(node[-1] - ai)
    else:
        print((-1))
        exit()
print((sum(node)))

-sita_maxi = [0 for i in range(n + 1)] -sita_maxi[n] = a[n] -for i in range(n - 1, -1, -1): -    sita_maxi[i] = sita_maxi[i + 1] + a[i] -if a[0] > 1: -    print((-1)) +if a[0] != 0: +    if n == 0 and a[0] == 1: +        print((1)) +    else: +        print((-1)) -lst = [1] +ruiseki = a[::-1] -    lst.append(min((lst[i - 1] - a[i - 1]) * 2, sita_maxi[i])) -    if lst[-1] < a[i]: +    ruiseki[i] += ruiseki[i - 1] +ruiseki = ruiseki[::-1] +node = [1] +komoti = [1] +for i in range(1, n + 1): +    ai = a[i] +    if komoti[-1] * 2 >= ai: +        node.append(min(ruiseki[i], komoti[-1] * 2)) +        komoti.append(node[-1] - ai) +    else: -print((sum(lst))) +print((sum(node)))