def main():

  eval(input())

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

  cusum = [0] * len(A)

  cusum[-1] = A[-1]



  if A[0] > 1:

    print((-1))

    return



  for i in range(len(A)-2, -1, -1):

    cusum[i] = cusum[i+1] + A[i]



  pre_node = 1

  ans = 1

  for i in range(1, len(A)):

    node = (pre_node - A[i-1]) * 2



    if node < A[i]:

        print((-1))

        return



    pre_node = min(node, cusum[i])

    ans += pre_node



  print(ans)



if __name__ == '__main__':

  main()

    

def main():

  eval(input())

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

  leaves = [0] * len(in_a)

  leaves[-1] = in_a[-1]



  if in_a[0] > 1:

    print((-1))

    return



  for i in range(len(in_a)-2, -1, -1):

    leaves[i] = leaves[i+1] + in_a[i]



  node = 1

  total = 1

  

  for i in range(1, len(in_a)):

    n = (node - in_a[i-1]) * 2



    if n < in_a[i]:

        print((-1))

        return



    node = min(n, leaves[i])

    total += node



  print(total)



if __name__ == '__main__':

  main()

    

def main():
    eval(input())
    A = list(map(int, input().split()))
    cusum = [0] * len(A)
    cusum[-1] = A[-1]
    if A[0] > 1:
        print((-1))
        return
    for i in range(len(A) - 2, -1, -1):
        cusum[i] = cusum[i + 1] + A[i]
    pre_node = 1
    ans = 1
    for i in range(1, len(A)):
        node = (pre_node - A[i - 1]) * 2
        if node < A[i]:
            print((-1))
            return
        pre_node = min(node, cusum[i])
        ans += pre_node
    print(ans)


if __name__ == "__main__":
    main()

def main():
    eval(input())
    in_a = list(map(int, input().split()))
    leaves = [0] * len(in_a)
    leaves[-1] = in_a[-1]
    if in_a[0] > 1:
        print((-1))
        return
    for i in range(len(in_a) - 2, -1, -1):
        leaves[i] = leaves[i + 1] + in_a[i]
    node = 1
    total = 1
    for i in range(1, len(in_a)):
        n = (node - in_a[i - 1]) * 2
        if n < in_a[i]:
            print((-1))
            return
        node = min(n, leaves[i])
        total += node
    print(total)


if __name__ == "__main__":
    main()

-    A = list(map(int, input().split())) -    cusum = [0] * len(A) -    cusum[-1] = A[-1] -    if A[0] > 1: +    in_a = list(map(int, input().split())) +    leaves = [0] * len(in_a) +    leaves[-1] = in_a[-1] +    if in_a[0] > 1: -    for i in range(len(A) - 2, -1, -1): -        cusum[i] = cusum[i + 1] + A[i] -    pre_node = 1 -    ans = 1 -    for i in range(1, len(A)): -        node = (pre_node - A[i - 1]) * 2 -        if node < A[i]: +    for i in range(len(in_a) - 2, -1, -1): +        leaves[i] = leaves[i + 1] + in_a[i] +    node = 1 +    total = 1 +    for i in range(1, len(in_a)): +        n = (node - in_a[i - 1]) * 2 +        if n < in_a[i]: -        pre_node = min(node, cusum[i]) -        ans += pre_node -    print(ans) +        node = min(n, leaves[i]) +        total += node +    print(total)