CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
if n == 0 and a[0] == 0:
    print((-1))
    exit()
if n != 0 and a[0] >= 1:
    print((-1))
    exit()
if n == 0 and a[0] >= 2:
    print((-1))
    exit()
ar = list(reversed(a))
#print(ar)
com = [ar[0]]
for i in range(n):
    com.append(com[i] + ar[i+1])
#print(com)
com.reverse()
ans = 1
val = 1
for i in range(1,n+1):
    val *= 2
    ans += min(val,com[i])
    val -= a[i]
    if (i < n and val <= 0) or (i == n and val < 0):
        print((-1))
        exit()
print(ans)


# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))

ar = list(reversed(a))
#print(ar)
com = [ar[0]]
for i in range(n):
    com.append(com[i] + ar[i+1])
#print(com)
com.reverse()
ans = 0
val = 1
for i in range(n+1):

    ans += min(val,com[i])
    val -= a[i]
    if (i < n and val <= 0) or (i == n and val < 0) or (i == n and a[i] == 0):
        print((-1))
        exit()
    val *= 2
print(ans)
# Original solution
def Csum(a):
    b,c=[0],0
    for i in range(len(a)):
        c+=a[i]
        b.append(c)
    return b

n=int(eval(input()))
a=list(map(int,input().split()))
s=Csum(a)
su=sum(a)
b=1
c=0
for i in range(n+1):
    c+=b
    b-=a[i]
    if b<=0:
        if i==n and b==0:
            break
        print((-1))
        exit()
    b=min(b*2,su-s[i+1])
print(c)

# Optimized solution
#全体的な方針
#浅いほうから貪欲に頂点を倍化していく
#現在見ている段の頂点数がそれより深い段の葉の数を超えると余ってしまう
#よって、minで押さえつける必要がある(累積和で高速化が必要)


#累積和の定義
def Csum(a):
    b,c=[],0
    for i in range(len(a)):
        c+=a[i]
        b.append(c)
    return b

#n:二分木の深さ
#a:それぞれの段数の葉の数
#s:aの累積和をとったもの(その深さよりも浅い葉の数)
#su:aの総和(全体の葉の数)
#b:現在の深さの頂点数
#c:現在までの頂点数

n=int(eval(input()))
a=list(map(int,input().split()))
s,su,b,c=Csum(a),sum(a),1,0
for i in range(n+1):
    #頂点数カウントに現在の段を加算
    #葉の数だけ親の候補を減らしておく
    c+=b
    b-=a[i]
    #最後まで頂点を使い切ったならcを出力
    #途中で頂点が足りなくなったら-1を出力
    if b<=0:
        if i==n and b==0:
            print(c)
        else:
            print((-1))
        break
    #次の段の頂点数の更新
    #iより深い葉の数(su-s[i])を超えないようにする
    b=min(b*2,su-s[i])
# Original solution
n = int(eval(input()))

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

cumsum_a = a.copy()

for i in range(n-1, -1, -1):
    cumsum_a[i] += cumsum_a[i+1]

ans = 0
childable_node = 1

for i in range(n + 1):
    if a[i] > childable_node:
        ans = -1
        break

    ans += childable_node

    if i < n:
        b_max1 = 2 * (childable_node - a[i])
        b_max2 = cumsum_a[i + 1]
        childable_node = min(b_max1, b_max2)

print(ans)

# Optimized solution
n = int(eval(input()))
 
a = list(map(int,input().split()))
 
cumsum_a = a.copy()
 
for i in range(n-1, -1, -1):
    cumsum_a[i] += cumsum_a[i+1]
 
ans = 0
node = 1
 
for i in range(n + 1):
    if a[i] > node:
        ans = -1
        break
 
    ans += node
 
    if i < n:
        node = min(2 * (node - a[i]), cumsum_a[i + 1])
 
print(ans)
# Original solution
n = int(eval(input()))
leaves = list(map(int, input().split()))

ans = 0
nodes_min = [0] * (n + 1)
nodes_max = [0] * (n + 1)
nodes_min[n] = leaves[n]
nodes_max[n] = leaves[n]

for depth in range(n, 0, -1):
    root_min = nodes_min[depth] // 2 + nodes_min[depth] % 2
    nodes_min[depth - 1] = leaves[depth - 1] + root_min
    root_max = nodes_max[depth]
    nodes_max[depth - 1] = leaves[depth - 1] + root_max
nodes = [0] * (n + 1)
nodes[0] = 1
if nodes[0] < nodes_min[0]:
    print((-1))
else:
    for depth in range(n):
        roots = nodes[depth]-leaves[depth]
        nodes[depth + 1] = min(roots * 2, nodes_max[depth + 1])
        if nodes[depth+1] < nodes_min[depth+1]:
            print((-1))
            break
    else:
        print((sum(nodes)))


# Optimized solution
n = int(eval(input()))
leaves = list(map(int, input().split()))

sum_leaves = [0]*(n+1)
sum_leaves[0] = leaves[0]
for depth in range(n):
    sum_leaves[depth+1] = sum_leaves[depth] + leaves[depth+1]

ans = 0

root = [None] * (n + 1)
if n == 0:
    root[0] = 0
else:
    root[0] = 1
if root[0] + leaves[0] != 1:
    print((-1))
else:
    for depth in range(n):
        root[depth+1] = min(2*root[depth]-leaves[depth+1],
                            sum_leaves[n]-sum_leaves[depth+1])
        if root[depth + 1] < root[depth] - leaves[depth + 1] or root[depth+1] < 0:
            print((-1))
            break
    else:
        print((sum(root)+sum(leaves)))
# Original solution
(N,) = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

numLeafs = A
minNonLeafs = [None for i in range(N + 1)]
maxNonLeafs = [None for i in range(N + 1)]

maxNonLeafs[0] = 1
for i in range(1, N + 1):
    maxNonLeafs[i] = 2 * maxNonLeafs[i - 1] - numLeafs[i]

minNonLeafs[N] = 0
maxNonLeafs[N] = 0
if minNonLeafs[N] + numLeafs[N] > 2 ** N:
    print((-1))
    exit()

for i in range(N - 1, -1, -1):
    minNonLeafs[i] = (minNonLeafs[i + 1] + numLeafs[i + 1] + 1) // 2
    if minNonLeafs[i] + numLeafs[i] > 2 ** i:
        print((-1))
        exit()
    maxNonLeafs[i] = min(maxNonLeafs[i], maxNonLeafs[i + 1] + numLeafs[i + 1])
    if minNonLeafs[i] > maxNonLeafs[i]:
        print((-1))
        exit()

count = 0
for i in range(N + 1):
    count += maxNonLeafs[i] + numLeafs[i]
print(count)


# Optimized solution
(N,) = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

numLeafs = A
if not (0 <= numLeafs[0] <= 1):
    print((-1))
    exit()

# Bound max non-leaves by how many expansions the previous level can do
maxNonLeafs = [None for i in range(N + 1)]
maxNonLeafs[0] = 1 - numLeafs[0]
for i in range(1, N + 1):
    maxNonLeafs[i] = 2 * maxNonLeafs[i - 1] - numLeafs[i]
    if maxNonLeafs[i] < 0:
        print((-1))
        exit()

# Tighten max non-leaves by how many available children are on next level
maxNonLeafs[N] = 0
for i in range(N - 1, -1, -1):
    maxNonLeafs[i] = min(maxNonLeafs[i], maxNonLeafs[i + 1] + numLeafs[i + 1])


count = 0
for i in range(N + 1):
    count += maxNonLeafs[i] + numLeafs[i]
print(count)
# Original solution
import math


def main():
    depth = int(eval(input()))
    leaves = [int(x) for x in input().split()]

    nodes_range = [None] * (depth + 1)
    for i in range(depth, -1, -1):
        if i == depth:
            nodes_range[i] = (leaves[i], leaves[i])
            continue
        min_nodes = math.ceil(nodes_range[i + 1][0] / 2) + leaves[i]
        max_nodes = nodes_range[i + 1][1] + leaves[i]
        nodes_range[i] = (min_nodes, max_nodes)

    if not (nodes_range[0][0] <= 1 <= nodes_range[0][1]):
        # 根が1を取り得ない場合は実現不可能。
        print((-1))
        return

    max_noded_tree = [0] * (depth + 1)
    for i in range(depth + 1):
        if i == 0:
            max_noded_tree[i] = 1
            continue
        max_noded_tree[i] = min(
            (max_noded_tree[i - 1] - leaves[i - 1]) * 2,
            nodes_range[i][1])
    print((sum(max_noded_tree)))
    return


if __name__ == '__main__':
    main()


# Optimized solution
import math


def main():
    depth = int(eval(input()))
    leaves = [int(x) for x in input().split()]

    nodes_range = [None] * (depth + 1)
    for i in range(depth, -1, -1):
        if i == depth:
            nodes_range[i] = (leaves[i], leaves[i])
            continue
        # 枝をなるべくまとめていく。
        min_nodes = math.ceil(nodes_range[i + 1][0] / 2) + leaves[i]
        # 枝が結合せず，平行に遡る感じ。
        max_nodes = nodes_range[i + 1][1] + leaves[i]
        nodes_range[i] = (min_nodes, max_nodes)

    if nodes_range[0][0] != 1:
        # 根が1を取り得ない場合は実現不可能。
        print((-1))
        return

    max_noded_tree = [0] * (depth + 1)
    for i in range(depth + 1):
        if i == 0:
            max_noded_tree[i] = 1
            continue
        max_noded_tree[i] = min(
            (max_noded_tree[i - 1] - leaves[i - 1]) * 2,
            nodes_range[i][1])
    print((sum(max_noded_tree)))
    return


if __name__ == '__main__':
    main()
# Original solution
from functools import reduce
from math import ceil

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

bottom_n_list = [(0, 0) for _ in range(n + 1)]
bottom_n_list[n] = (a[n], a[n])
for i in range(n-1, -1, -1):
    leaf_num = a[i]
    min_a_node_num = ceil(bottom_n_list[i + 1][0] / 2)
    max_a_node_num = min(bottom_n_list[i + 1][1], 2 ** i - leaf_num)
    if max_a_node_num < min_a_node_num:
        print((-1))
        exit()
    bottom_n_list[i] = (min_a_node_num + leaf_num, max_a_node_num + leaf_num)

top_n_list = [0]*(n+1)
if 1 < bottom_n_list[0][0] or bottom_n_list[0][1] < 1:
    print((-1))
    exit()
top_n_list[0] = 1
for i in range(1, n+1):
    top_n_list[i] = min(bottom_n_list[i][1], (top_n_list[i-1]-a[i-1])*2)


ans = reduce(lambda a, b: a+b, top_n_list)
print(ans)

# Optimized solution
from math import ceil

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

bottom_n_list = [(0, 0) for _ in range(n + 1)]
bottom_n_list[n] = (a[n], a[n])
for i in range(n-1, -1, -1):
    leaf_num = a[i]
    min_a_node_num = ceil(bottom_n_list[i + 1][0] / 2)
    max_a_node_num = min(bottom_n_list[i + 1][1], 2 ** i - leaf_num)
    if max_a_node_num < min_a_node_num:
        print((-1))
        exit()
    bottom_n_list[i] = (min_a_node_num + leaf_num, max_a_node_num + leaf_num)

top_n_list = [0]*(n+1)
if 1 < bottom_n_list[0][0] or bottom_n_list[0][1] < 1:
    print((-1))
    exit()
top_n_list[0] = 1
ans = 1
for i in range(1, n+1):
    top_n_list[i] = min(bottom_n_list[i][1], (top_n_list[i-1]-a[i-1])*2)
    ans += top_n_list[i]

print(ans)
# Original solution
N=int(eval(input()))
A=[int(i) for i in input().split()]
a=[1-A[0]]
if a[0]<0:
    print((-1))
    exit()
for k in range(N):
    ak=a[-1]
    a.append(2*ak-A[k+1])
    if a[-1]<0:
        print((-1))
        exit()
    if a[-1]>10**17:
        break
M=len(a)
for k in range(N,0,-1):
    if k-1<M:
        A[k-1]=A[k-1]+min(A[k],a[k-1])
    else:
        A[k-1]=A[k-1]+A[k]
print((sum(A)))

# Optimized solution
N=int(eval(input()))
A=[int(i) for i in input().split()]
#len(A)=N+1
ak=1-A[0]
if ak<0:
    print((-1))
    exit()
a=[ak]
k=0
while(ak>=0 and k<N):
    ak=2*ak-A[k+1]
    a.append(ak)
    if ak<0:
        print((-1))
        exit()
    if ak>=10**14:
        #以降に関わらず構成可能
        break
    k+=1
#print(a)
M=len(a)
for k in range(N,0,-1):
    if k<=M:
        A[k-1]+=min(A[k],a[k-1])
    else:
        A[k-1]+=A[k]
        continue
print((sum(A)))
#print(A)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

rng = [[A[N], A[N]]]

for i in range(N - 1, -1, -1):
    merged = (rng[-1][0] + 1) // 2 + A[i]
    nonmerged = rng[-1][1] + A[i]
    rng.append([merged, nonmerged])
if rng[-1][0] > 1:
    print((-1))
    exit()
rng.reverse()
prev_non_leaf = 1
ans = 1
for i in range(1, N + 1):
    l, u = rng[i]
    ans += min(prev_non_leaf * 2, u)
    prev_non_leaf = prev_non_leaf * 2 - A[i]
print(ans)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

rng = [[A[N], A[N]]]

for i in range(N - 1, -1, -1):
    merged = (rng[-1][0] + 1) // 2 + A[i]
    nonmerged = rng[-1][1] + A[i]
    rng.append([merged, nonmerged])
if rng[-1][0] > 1:
    print((-1))
    exit()
rng.reverse()
prev_non_leaf = 1
ans = 1
for i in range(1, N + 1):
    _, u = rng[i]
    ans += min(prev_non_leaf * 2, u)
    prev_non_leaf = prev_non_leaf * 2 - A[i]
print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))

ans = 0
nonleaf_nodes = []
current = 1

for depth in range(0, n+1):
    c = current-a[depth]
    if c < 0: # 目いっぱい頂点を増やしても葉が足りない
        print((-1))
        exit()
    nonleaf_nodes.append(c)
    current = c*2
nonleaf_nodes[-1]=0

ans = a[-1]
for depth in range(n-1, -1, -1):
    nonleaf_nodes[depth] = min(nonleaf_nodes[depth], nonleaf_nodes[depth+1]+a[depth+1])
    ans += nonleaf_nodes[depth] + a[depth]
print(ans)



    

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))

ans = 0
nonleaf_nodes = []
current = 1

# とりあえず増やせるだけ頂点を増やしていく
for depth in range(0, n+1):
    c = current-a[depth]
    # 目いっぱい頂点を増やしても葉を賄えない場合、木を作れない
    if c < 0:
        print((-1))
        exit()
    nonleaf_nodes.append(c)
    current = c*2
nonleaf_nodes[-1]=0

# 余計な頂点を最深層から削除していく
# 下位層の葉を含む頂点数を超えると上位層で葉が増えるのでNG
ans = a[-1]
for depth in range(n-1, -1, -1):
    nonleaf_nodes[depth] = min(nonleaf_nodes[depth], nonleaf_nodes[depth+1]+a[depth+1])
    ans += nonleaf_nodes[depth] + a[depth]
print(ans)
# Original solution
import os
from collections import defaultdict
N = int(eval(input()))
A = list(map(int, input().split(" ")))

pow2 = defaultdict(int)

ret = 0
lim = []
for i in range(N+1):
    pow2[i] = 2**i


for i in range(N+1):
    if i == 0:
        r = 2**i - A[i]
    else:
        r = 2*lim[i-1] - A[i]
    if r < 0:
        print((-1))
        exit(0)
    lim.append(r)

for d in reversed(list(range(N+1))):
    if A[d] > pow2[d]:
        print((-1))
        exit(0)
    if d == N:
        n = min(A[d], pow2[d])
    else:
        n = min(A[d] + min(n, lim[d]), pow2[d])
    ret +=n

print(ret)

# Optimized solution
import os
from collections import defaultdict

N = int(eval(input()))
A = list(map(int, input().split(" ")))

pow2 = defaultdict(int)

ret = 0
lim = [0] * (N + 1)
# for i in range(N + 1):
#     pow2[i] = 2 ** i

for i in range(N + 1):
    if i == 0:
        r = 1 - A[i]
    else:
        r = 2 * lim[i - 1] - A[i]
    if r < 0:
        print((-1))
        exit(0)
    lim[i] = r

for d in reversed(list(range(N + 1))):
    # if A[d] > pow2[d]:
    #     print(-1)
    #     exit(0)
    if d == N:
        #n = min(A[d], pow2[d])
        n = A[d]
    else:
        #n = min(A[d] + min(n, lim[d]), pow2[d])
        n = A[d] + min(n, lim[d])
    ret += n

print(ret)
# Original solution
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

N = int(input())
A = list(map(int, input().split()))
lenA = len(A)

cnt = 1
node = [1]*N

# from functools import lru_cache
# @lru_cache(None)
def f(depth, leaf, nodec):
    # print(depth, leaf, nodec)
    if depth == N:
        # print(' ', depth, leaf, nodec)
        if leaf == A[depth]:
            # OK!
            print(nodec)
            exit()
            return
        else:
            # NG
            return
    
    # NG
    if leaf < A[depth]:
        return 0

    # OK
    not_leaf = leaf - A[depth]
    for j in range(not_leaf*2, not_leaf-1, -1):
        f(depth+1, j, nodec+j)

f(0, 1, 1)
print(-1)


# Optimized solution
# import numpy as np
import sys, math, heapq
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

N = int(input())
A = list(map(int, input().split()))

if N == 0:
    if A[0] == 1:
        print(1)
        exit()
    else:
        print(-1)
        exit()
if N != 0 and A[0] > 0:
    print(-1)
    exit()

rcA = [0] * (N)
rcA[-1] = A[-1]
for i in range(1, N):
    rcA[-i - 1] = rcA[-i] + A[-i - 1]
# print(rcA)

cnt = 1
node = 1
for a, rca in zip(A, rcA):
    min_node = node - a
    max_node = 2 * (node - a)
    if rca < min_node or min_node < 0:
        print(-1)
        exit()
    node = min(max_node, rca)
    # print(node)
    cnt += node
if node != A[-1]:
    print(-1)
    exit()
print(cnt)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
capacity = [1 for k in range(N+1)]
cut = 0
for k in range(N+1):
    #print(capacity)
    if A[k] >= capacity[k] and k!= N:
        print((-1))
        break
    if A[k] > capacity[k] and k == N:
        print((-1))
        break
    if k < N:
        capacity[k+1] = 2*(capacity[k] - A[k])
    if k == N:
        ans = [0 for k in range(N+1)]
        ans[0] =1
        for m in range(N+1):
            ans[m] = min(capacity[m], sum(A[m:N+1]))
        print((sum(ans)))





# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
capacity = [1 for k in range(N+1)]
cut = 0
for k in range(N+1):
    #print(capacity)
    if A[k] >= capacity[k] and k!= N:
        print((-1))
        break
    if A[k] > capacity[k] and k == N:
        print((-1))
        break
    if k < N:
        capacity[k+1] = 2*(capacity[k] - A[k])
    if k == N:
        ans = [0 for k in range(N+1)]
        ans[0] =1
        sumA = 0
        for m in range(1, N+2):
            sumA += A[-m]
            ans[-m] = min(capacity[-m], sumA)
        print((sum(ans)))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
if N == 0 and A[0] == 1:
    print((1))
else:
    nodes = [0]*(N+1)
    s_d = 0 # 飽和してないノードが残ってる深さ
    cap_d = 1 - A[0]
    failed = False
    for i, a in enumerate(A):
        # a個の葉をなるべく浅いノードから取る
        while a > 0:
            # これ以上分岐することろがなければ失敗
            if i < s_d or cap_d <= 0:
                failed = True
                break
            # s_dから枝を取れるだけ取る
            x = min(cap_d - nodes[s_d], a)
            a -= x
            nodes[s_d:i+1] = [node + x for node in nodes[s_d:i+1]]
            # s_dがいっぱいになったら深さを一つ進める
            if nodes[s_d] >= cap_d:
                cap_d -= A[s_d]
                cap_d *= 2
                s_d += 1
        if failed:
            break
    print((-1 if failed else sum(nodes)))

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
cap = [[0, 0] for _ in range(N+1)]
cap[-1] = [A[-1], A[-1]]
for i in range(N-1,-1,-1):
    cap[i][0] = (cap[i+1][0] + 2 - 1)//2
    cap[i][1] = cap[i+1][1] + A[i]
ans = 1
nodes = 1
failed = nodes < cap[0][0]
for i in range(N):
    nodes = min((nodes - A[i])*2, cap[i+1][1])
    if nodes < cap[i+1][0]:
        failed = True
        break
    ans += nodes
print((-1 if failed else ans))
# Original solution
import sys
import itertools

N = int(eval(input()))
A = list(input().split())
A = [int(A[i]) for i in range(N+1)]
MAX = [2**i for i in range(N+1)]
A_inter = list(reversed(list(itertools.accumulate(reversed(A)))))

for i in range(N+1):
    if A[i] > MAX[i]:
        print((-1))
        sys.exit()
    elif i != N:
        MAX[i+1] = (MAX[i] - A[i]) * 2
        
print((sum([min(MAX[i], A_inter[i]) for i in range(N+1)])))

# Optimized solution
import sys
import itertools

N = int(eval(input()))
A = list(input().split())
A = [int(A[i]) for i in range(N+1)]
MAX = [1 for i in range(N+1)]
A_inter = list(reversed(list(itertools.accumulate(reversed(A)))))

for i in range(N+1):
    if A[i] > MAX[i]:
        print((-1))
        sys.exit()
    elif i != N:
        MAX[i+1] = (MAX[i] - A[i]) * 2
        
print((sum([min(MAX[i], A_inter[i]) for i in range(N+1)])))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

d = [1] * (N+1)
d[0] -= A[0]
for i in range(N):
    d[i+1] = d[i] * 2 - A[i+1]

if d[-1] < 0:
    print((-1))
    exit()
else:
    d[-1] = A[-1]
for i in range(N, 0, -1):
    x = d[i]
    x += A[i-1]
    if d[i-1] * 2 < x:
        y = x - A[i-1]
        if (y+1) // 2 + A[i-1] <= d[i-1] * 2:
            x = d[i-1] * 2
    d[i-1] = min(x, d[i-1]+A[i-1])
print((sum(d)))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

d = [1] * (N+1)
d[0] -= A[0]
for i in range(N):
    d[i+1] = d[i] * 2 - A[i+1]

if d[-1] < 0:
    print((-1))
    exit()
else:
    d[-1] = A[-1]
for i in range(N, 0, -1):
    x = d[i]
    x += A[i-1]
    d[i-1] = min(x, d[i-1]+A[i-1])
print((sum(d)))
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
s=sum(a)-1
ans,x=0,1

for i in range(n):
  if a[i]>x-1:
    print((-1))
    exit()
  ans+=x
  x-=a[i]
  if i<n-1 and x<=0:
    print((-1))
    exit()
  if s>=0:
    if s>=x:
      s-=x
      x=x*2
    else:
      x=(x-s)+s*2
      s=0
if x!=a[-1] or x<=0:
  print((-1))
  exit()
ans+=x
print(ans)

# Optimized solution
#葉の総和-1=分岐の数

n=int(eval(input()))
a=list(map(int,input().split()))
s=sum(a)-1
ans,x=0,1

for i in range(n):
  if a[i]>x-1:
    print((-1))
    exit()
  ans+=x
  x-=a[i]
  if i<n-1 and x<=0:
    print((-1))
    exit()
  if s>=0:
    if s>=x:
      s-=x
      x=x*2
    else:
      x=(x-s)+s*2
      s=0
if x!=a[-1] or x<=0:
  print((-1))
  exit()
ans+=x
print(ans)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
node=[1 for _ in range(N+1)]
Sn=sum(A)
S=[Sn]
for s in range(N):
    S.append(S[s]-A[s+1])

ans=1
for i in range(1,N+1):
    if node[i-1]*2<=S[i-1]:
        ans+=node[i-1]*2
        node[i]=node[i-1]*2
    elif node[i-1]<=S[i-1]<node[i-1]*2:
        node[i]=S[i-1]
        ans+=S[i-1]
    node[i]-=A[i]
    if node[i]<0:
        ans=-1
        break

if A[0]>0:
    ans=-1
if N==0 and A[0]==1:
    ans=1

print(ans)

# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
node=[1 for _ in range(N+1)] #深さiで、葉にならない頂点の個数node[i]
S=sum(A) #生産しなければならない葉の枚数の合計
ans=1 #深さ0である根は最初にカウントする

#深さiの時点であと何枚の葉が必要か計算しつつ、必要な分だけノードを生産する
#ノードの個数がその時点でのSまで届いたら、あとは各頂点が1つずつノードを生産し、
#各層でA[i]個のノードが生産を止めて葉となる

for i in range(1,N+1): 
    if node[i-1]*2<=S: #葉の生産量はノードの2倍ずつ、ノルマを超えるまでは最速で生産する
        ans+=node[i-1]*2
        node[i]=node[i-1]*2
    elif node[i-1]<=S<node[i-1]*2: #全てのノードが2倍になるとノルマを過ぎてしまう場合はノルマピッタリで止める
        node[i]=S
        ans+=S
    node[i]-=A[i] #葉となるノード数を差し引き、生産に寄与するノード数にして次の層に引き継ぐ
    S-=A[i] #葉になった枚数だけノルマも減らす
    if node[i]<0: #生産が間に合わなかった場合(要求された葉＞生産可能なノード数)はループ停止
        ans=-1
        break

if A[0]>0: #深さ0に葉があったら基本的にアウトだが…
    ans=-1
if N==0 and A[0]==1: #N=0の時だけ、深さ0に葉が1つという状況が許される
    ans=1

print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = [0]*(n+1)

b[0] = 1
for i in range(n):
    b[i+1] = 2*(b[i] - a[i])
#根から計算した深さごとの要素数の最大値

c = [0]*(n+1)
c[n] = a[n]
for j in range(n):
    c[n-1-j] = c[n-j] + a[n-1-j]
#葉から計算した最小値

#print(b)
#print(c)

d = [0]*(n+1)
flag = 0
for k in range(n+1):
    if b[k] < c[k]:
        d[k] = b[k]
        if d[k] <= 0:
            flag += 1
    else:
        d[k] = c[k]
        if d[k] <= 0:
            flag += 1

for l in range(n):
    flag += 1 if d[l+1] < d[l] - a[l] or 2*d[l] - a[l] < d[l+1] else 0
    flag += 1 if a[l] + (d[l+1]+1)//2 > d[l] or d[l] > d[l+1] + a[l] else 0

ans = sum(d) if flag == 0 else -1
ans = -1 if n == 0 and a[0] > 1 else ans
ans = -1 if a[n] != d[n] else ans
print(ans)
#print(d)

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = [0]*(n+1)

b[0] = 1
for i in range(n):
    b[i+1] = 2*(b[i] - a[i])

c = [0]*(n+1)
c[n] = a[n]
for j in range(n):
    c[n-1-j] = c[n-j] + a[n-1-j]

d = [0]*(n+1)
flag = 0
for k in range(n+1):
    if b[k] < c[k]:
        d[k] = b[k]
        if d[k] <= 0:
            flag += 1
    else:
        d[k] = c[k]
        if d[k] <= 0:
            flag += 1

ans = sum(d) if flag == 0 else -1
ans = -1 if n == 0 and a[0] > 1 else ans
ans = -1 if a[n] != d[n] else ans
print(ans)
# Original solution
*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)))

# Optimized solution
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)))
# Original solution
*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)))

# Optimized solution
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)))