*a,=list(map(int,[*open(0)][1].split()))

c=[s:=0]

for i in a[:0:-1]:c+=c[-1]+i,

b=1

for a in a:

  if b<a:

    s=-1

    break

  s+=b

  b=min(c.pop(),(b-a)*2)

print(s)
*a,=list(map(int,[*open(s:=0)][1].split()))

c=sum(a)

b=1

for a in a:

  if b<a:

    s=-1

    break

  s+=b

  c-=a

  b=min(c,(b-a)*2)

print(s)
(*a,) = list(map(int, [*open(0)][1].split()))
c = [s := 0]
for i in a[:0:-1]:
    c += (c[-1] + i,)
b = 1
for a in a:
    if b < a:
        s = -1
        break
    s += b
    b = min(c.pop(), (b - a) * 2)
print(s)

(*a,) = list(map(int, [*open(s := 0)][1].split()))
c = sum(a)
b = 1
for a in a:
    if b < a:
        s = -1
        break
    s += b
    c -= a
    b = min(c, (b - a) * 2)
print(s)

-(*a,) = list(map(int, [*open(0)][1].split())) -c = [s := 0] -for i in a[:0:-1]: -    c += (c[-1] + i,) +(*a,) = list(map(int, [*open(s := 0)][1].split())) +c = sum(a) -    b = min(c.pop(), (b - a) * 2) +    c -= a +    b = min(c, (b - a) * 2)