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

c=sum(a)

s=b=1

for a in a:c-=a;b=min(c,(b-a)*2);s+=b

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

s=b=1

for a in a:b=min(c:=c-a,b-a<<1);s+=b

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

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

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