#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;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgRkFTVCBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTtjaW4udGllKDApO2NvdXQudGllKDApCiNkZWZpbmUgUkVQKGksYSxuKSBmb3IgKGludCBpPWE7aTxuO2krKykKI2RlZmluZSBBTEwoeCkgKHgpLmJlZ2luKCksKHgpLmVuZCgpCiNkZWZpbmUgU1ooeCkgKChpbnQpKHgpLnNpemUoKSkKI2RlZmluZSBVTSB1bm9yZGVyZWRfbWFwCiNkZWZpbmUgUEIgcHVzaF9iYWNrCiNkZWZpbmUgViB2ZWN0b3IKI2RlZmluZSBFUFMgMWUtOAp0eXBlZGVmIGxvbmcgbG9uZyBpbnQgTEw7CnR5cGVkZWYgc3RyaW5nIFNUUjsKLypGSU4qLwoKaW50IG1haW4oKQp7CglGQVNUOwoJaW50IG47CgljaW4+Pm47CglWPExMPiBhKG4rMSksIHMob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