*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<<1);s+=b

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

b=1

print((max(-1,sum((b:=min(c:=c-a,b-a<<1))for a in a)+1)))
(*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 << 1)
    s += b
print((max(-1, s)))

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

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