import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

input = lambda:sys.stdin.readline().rstrip()

def resolve():

    n = int(eval(input()))

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

    s = sum(A)



    if n == 0:

        print((1 if A[0] == 1 else -1))

        return



    if A[0] != 0:

        print((-1))

        return



    not_leaf = 1

    # check

    for d in range(n + 1):

        if not_leaf < A[d]:

            print((-1))

            return

        not_leaf -= A[d]

        not_leaf *= 2



    res = 0

    not_leaf = 1

    count = 1 # 枝分かれの個数、最大 s

    for d in range(n + 1):

        not_leaf -= A[d]

        # 最大で not_leaf 個、枝分かれを増やせる

        add = min(s - count, not_leaf)

        count += add

        not_leaf += add

        res += not_leaf

        # print(d, add)



    print((res + 1))

resolve()
import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

input = lambda:sys.stdin.readline().rstrip()

def resolve():

    n = int(eval(input()))

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



    res = 0

    not_leaf = 1

    s = sum(A)

    count = 1

    for d in range(n + 1):

        res += not_leaf

        if not_leaf < A[d]:

            print((-1))

            return

        not_leaf -= A[d]

        add = min(s - count, not_leaf)

        count += add

        not_leaf += add



    print((res if not_leaf == 0 else -1))

resolve()
import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n = int(eval(input()))
    A = list(map(int, input().split()))
    s = sum(A)
    if n == 0:
        print((1 if A[0] == 1 else -1))
        return
    if A[0] != 0:
        print((-1))
        return
    not_leaf = 1
    # check
    for d in range(n + 1):
        if not_leaf < A[d]:
            print((-1))
            return
        not_leaf -= A[d]
        not_leaf *= 2
    res = 0
    not_leaf = 1
    count = 1  # 枝分かれの個数、最大 s
    for d in range(n + 1):
        not_leaf -= A[d]
        # 最大で not_leaf 個、枝分かれを増やせる
        add = min(s - count, not_leaf)
        count += add
        not_leaf += add
        res += not_leaf
        # print(d, add)
    print((res + 1))


resolve()

import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n = int(eval(input()))
    A = list(map(int, input().split()))
    res = 0
    not_leaf = 1
    s = sum(A)
    count = 1
    for d in range(n + 1):
        res += not_leaf
        if not_leaf < A[d]:
            print((-1))
            return
        not_leaf -= A[d]
        add = min(s - count, not_leaf)
        count += add
        not_leaf += add
    print((res if not_leaf == 0 else -1))


resolve()

+    res = 0 +    not_leaf = 1 -    if n == 0: -        print((1 if A[0] == 1 else -1)) -        return -    if A[0] != 0: -        print((-1)) -        return -    not_leaf = 1 -    # check +    count = 1 +        res += not_leaf -        not_leaf *= 2 -    res = 0 -    not_leaf = 1 -    count = 1  # 枝分かれの個数、最大 s -    for d in range(n + 1): -        not_leaf -= A[d] -        # 最大で not_leaf 個、枝分かれを増やせる -        res += not_leaf -        # print(d, add) -    print((res + 1)) +    print((res if not_leaf == 0 else -1))