#include<bits/stdc++.h>
using namespace std;

#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define REP(i,a,n) for (int i=a;i<n;i++)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define UM unordered_map
#define PB push_back
#define V vector
#define EPS 1e-8
typedef long long int LL;
typedef string STR;
/*FIN*/

int main()
{
	FAST;
	int n;
	cin>>n;
	V<LL> a(n+1), s(n+1);
	for(auto &i:a) cin>>i;
	LL _t = 1LL;
	bool f=1;
	for(int i=0;i<=n;i++) {
        if(_t < 1e18) s[i] = _t - a[i], _t = 2LL*s[i] ;
        else s[i] = 1e18 ;
		if(s[i]<0) {
			f=0;
			break;
		}
	}
	if(a[n]==0) f=0;
	LL ans=0;
	if(f) {
		ans += a[n];
		for(int d = n-1; d>=0; d--) {
			if(a[d+1] > 2*s[d]) {
				f=0;
				break;
			}
			ans += min(a[d+1], s[d]) + a[d];
			a[d] += min(a[d+1],s[d]);
		}
	}
	if(f) cout<<ans<<endl;
	else cout<<"-1\n";
	return 0;
}
