output_description: Print the answer as an integer.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      A_0 A_1 A_2 \cdots A_N
attempt: # coding: utf-8
  # Your code here!
  n = int(input())
  L = list(map(int, input().split()))
  
  a = sum(L)
  b = 0
  n = 0
  j = 1
  # print(a,L)
  
  for i in range(len(L)):
      # print(a,b,j,n)
      if i > 0:
          a = a - L[i]
          #        print(a,b,j,n)
          if b + L[i] <= j:
              n += b
              n += L[i]
              if b == 0:
                  break
              elif b * 2 <= a and b * 2 <= j - L[i]:
                  #                print('a')
                  b = b * 2 - L[i]
              elif b * 2 <= a and b * 2 > j - L[i]:
                  #                print('b')
                  b = j - L[i]
              else:
                  #                print('c')
                  b = a
          else:
              n = -1
              break
      elif i == 0:
          if L[i] == 1:
              n = -1
              break
          b = 1
          n = L[i]
  
      j = j * 2
  
  print(n)
  
problem_description: Statement
  
  Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is
  there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there
  are exactly A_d leaves at depth d? If such a tree exists, print the maximum
  possible number of vertices in such a tree; otherwise, print -1.
samples: [{"input": "3\n    0 1 1 2", "output": "7\n    \n\nBelow is the tree with the maximum possible number of vertices. It has seven\nvertices, so we should print 7.\n\n![0d8d99d13df036f23b0c9fcec52b842b.png](https://img.atcoder.jp/nomura2020/0d8d99d13df036f23b0c9fcec52b842b.png)\n\n* * *"}, {"input": "4\n    0 0 1 0 2", "output": "10\n    \n\n* * *"}, {"input": "2\n    0 3 1", "output": "-1\n    \n\n* * *"}, {"input": "1\n    1 1", "output": "-1\n    \n\n* * *"}, {"input": "10\n    0 0 1 1 2 3 5 8 13 21 34", "output": "264"}]