#include <functional>  
#include <algorithm>  
#include <iostream>  
#include <fstream>  
#include <sstream>  
#include <iomanip>  
#include <numeric>  
#include <cstring>  
#include <cassert>  
#include <cstdio>  
#include <string>  
#include <vector>  
#include <bitset>  
#include <queue>  
#include <stack>  
#include <cmath>  
#include <ctime>  
#include <list>  
#include <set>  
#include <map> 
#include <bits/stdc++.h>

using namespace std;

#define PB push_back  
#define MP(A, B) make_pair(A, B)  
#define fi first  
#define se second  

#define gcd(a,b) __gcd(a,b)
#define pi acos(-1.0)
#define pii pair<int,int>
#define ll long long  
#define MAX 1000005  
#define MOD 1000000007  
#define INF 0x3f3f3f3f  
#define EXP 1e-8  
#define lowbit(x) (x&-x)  
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int nobind = []() {std::ios::sync_with_stdio(false);cin.tie(NULL);return 0;}();

/*************************************** 
prime table 
O(n) 
***************************************/  
const int PRIMERANGE = 1000000;  
int prime[PRIMERANGE + 1];  
int getPrime() {  
    memset (prime, 0, sizeof (int) * (PRIMERANGE + 1));  
    for (int i = 2; i <= PRIMERANGE; i++) {  
        if (!prime[i]) prime[++prime[0]] = i;  
        for (int j = 1; j <= prime[0] && prime[j] <= PRIMERANGE / i; j++) {  
            prime[prime[j]*i] = 1;  
            if (i % prime[j] == 0) break;  
        }  
    }  
    return prime[0];  
}  
/*************************************** 
get factor of n 
O(sqrt(n)) 
factor[][0] is prime factor 
factor[][1] is factor generated by this prime 
factor[][2] is factor counter 

need: Prime Table 
***************************************/  
///you should init the prime table before  
long long factor[100][3], facCnt;  
long long getFactors(long long x) {  
    facCnt = 0;  
    long long tmp = x;  
    for(long long i = 1; prime[i] <= tmp / prime[i]; i++) {  
        factor[facCnt][1] = 1, factor[facCnt][2] = 0;  
        if(tmp % prime[i] == 0)  
            factor[facCnt][0] = prime[i];  
        while(tmp % prime[i] == 0)  
            factor[facCnt][2]++, factor[facCnt][1] *= prime[i], tmp /= prime[i];  
        if(factor[facCnt][1] > 1) facCnt++;  
    }  
    if(tmp != 1)  
        factor[facCnt][0] = tmp, factor[facCnt][1] = tmp, factor[facCnt++][2] = 1;  
    return facCnt;  
}  

// hash func for pair
struct hash_pair { 
    template <class T1, class T2> 
    size_t operator()(const pair<T1, T2>& p) const { 
        auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); 
        return hash1 ^ hash2; 
    } 
}; 

ll N, A[100005] = {0};
int main() {
    ll sum = 0;
    cin >> N; 
    for (int i = 0; i <= N; ++i){
        cin >> A[i]; sum += A[i];
    }
    sum -= 1;
    ll now = 1,ans = 0;
    for (int i = 0; i <= N; ++i) {
        if (i <= N-1) ans += now;
        else {
            ans += A[i];
        }
        // cout << ans << ' ' << now << ' ' << i <<  endl;
        if (now >= A[i]) {
            now -= A[i]; 
            if (now > sum) {
                now  = sum * 2 + now - sum;
                sum = 0;
            } else {
                sum -= now;
                now = now * 2; 
            }
        } else {
            cout << -1 << endl; return 0;
        }
    }
    cout <<  ans << endl;
    return 0;
}

Measured memory_bytes: 4480