#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define Rep(i,a,n)for(lld i=(lld)(a);i<(lld)(n);i++)
#define rep(i,n)Rep(i,0,n)
typedef long long lld;
typedef long double Lf;
#define Ma(x,y) (x>y?x:y)
#define mi(x,y) (x<y?x:y)
#define N 100010

int main() {
	lld n,a[N],i,next=0,ans=0;
	scanf("%lld",&n);
	rep(i,n+1)scanf("%lld",&a[i]);
	if (n == 0 && a[0] != 1) { printf("-1"); return 0; }
	if (n == 0 && a[0] == 1) { printf("1"); return 0; }
	if (a[0] != 0) { printf("-1"); return 0; }
	if (n == 1 && a[1] == 1) { printf("2"); return 0; }
	if (n == 1 && a[1] == 2) { printf("3"); return 0; }
	if (n == 1 && a[1] > 2) { printf("-1"); return 0; }
	lld b[N] = { 0 };//葉以外で配置できる最大数
	b[0] = 1;
	rep(i, n) {
		b[i + 1] = b[i] * 2 - a[i + 1];
		if(b[i+1]<0){ printf("-1"); return 0; }
		if(i!=n-1&&b[i+1]==0) { printf("-1"); return 0; }
	}
	for (i=n;i>=1;i--){
		ans += next+a[i];
		next = mi(next+a[i], b[i-1]);
		if (next==0&&a[i-1]==0&&i>1) { printf("-1"); return 0; }
	}
	printf("%lld", ans+1);
}