n=int(eval(input()))

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

ans=sum(arr)

if n==0:

  if arr[0]==1:

    print((1))

  else:

    print((-1))

  exit()

for i in range(n):

  if i<=30 and arr[i]>=2**i:

    print((-1))

    exit()

if n<=30 and arr[n]>2**n:

  print((-1))

  exit()

maxs=[0]*(n+1)

maxs[0]=1

for i in range(1,n):

  maxs[i]=2*maxs[i-1]-arr[i]

tmp=arr[n]

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

  maxs[i]=min(maxs[i],tmp)

  tmp+=arr[i]

mins=[0]*(n+1)

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

  mins[i]=(mins[i+1]+arr[i+1]+1)//2

for i in range(n):

  if maxs[i]<mins[i]:

    print((-1))

    exit()

ans+=sum(maxs)

print(ans)
n=int(eval(input()))

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

if n==0:

  if arr[0]==1:

    print((1))

  else:

    print((-1))

  exit()

acum=[arr[0]]

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

  acum.append(acum[-1]+arr[i])

b=[0]*n

b[0]=1-arr[0]

b[0]=min(b[0],acum[n]-acum[0])

for i in range(1,n):

  b[i]=2*b[i-1]-arr[i]

  b[i]=min(b[i],acum[n]-acum[i])

for i in range(n):

  if b[i]<=0:

    print((-1))

    exit()

  if b[i]<=30 and arr[i+1]>2**b[i]:

    print((-1))

    exit()

ans=sum(arr)+sum(b)

print(ans)
n = int(eval(input()))
arr = list(map(int, input().split()))
ans = sum(arr)
if n == 0:
    if arr[0] == 1:
        print((1))
    else:
        print((-1))
    exit()
for i in range(n):
    if i <= 30 and arr[i] >= 2**i:
        print((-1))
        exit()
if n <= 30 and arr[n] > 2**n:
    print((-1))
    exit()
maxs = [0] * (n + 1)
maxs[0] = 1
for i in range(1, n):
    maxs[i] = 2 * maxs[i - 1] - arr[i]
tmp = arr[n]
for i in range(n - 1, -1, -1):
    maxs[i] = min(maxs[i], tmp)
    tmp += arr[i]
mins = [0] * (n + 1)
for i in range(n - 1, -1, -1):
    mins[i] = (mins[i + 1] + arr[i + 1] + 1) // 2
for i in range(n):
    if maxs[i] < mins[i]:
        print((-1))
        exit()
ans += sum(maxs)
print(ans)

n = int(eval(input()))
arr = list(map(int, input().split()))
if n == 0:
    if arr[0] == 1:
        print((1))
    else:
        print((-1))
    exit()
acum = [arr[0]]
for i in range(1, n + 1):
    acum.append(acum[-1] + arr[i])
b = [0] * n
b[0] = 1 - arr[0]
b[0] = min(b[0], acum[n] - acum[0])
for i in range(1, n):
    b[i] = 2 * b[i - 1] - arr[i]
    b[i] = min(b[i], acum[n] - acum[i])
for i in range(n):
    if b[i] <= 0:
        print((-1))
        exit()
    if b[i] <= 30 and arr[i + 1] > 2 ** b[i]:
        print((-1))
        exit()
ans = sum(arr) + sum(b)
print(ans)

-ans = sum(arr) +acum = [arr[0]] +for i in range(1, n + 1): +    acum.append(acum[-1] + arr[i]) +b = [0] * n +b[0] = 1 - arr[0] +b[0] = min(b[0], acum[n] - acum[0]) +for i in range(1, n): +    b[i] = 2 * b[i - 1] - arr[i] +    b[i] = min(b[i], acum[n] - acum[i]) -    if i <= 30 and arr[i] >= 2**i: +    if b[i] <= 0: -if n <= 30 and arr[n] > 2**n: -    print((-1)) -    exit() -maxs = [0] * (n + 1) -maxs[0] = 1 -for i in range(1, n): -    maxs[i] = 2 * maxs[i - 1] - arr[i] -tmp = arr[n] -for i in range(n - 1, -1, -1): -    maxs[i] = min(maxs[i], tmp) -    tmp += arr[i] -mins = [0] * (n + 1) -for i in range(n - 1, -1, -1): -    mins[i] = (mins[i + 1] + arr[i + 1] + 1) // 2 -for i in range(n): -    if maxs[i] < mins[i]: +    if b[i] <= 30 and arr[i + 1] > 2 ** b[i]: -ans += sum(maxs) +ans = sum(arr) + sum(b)