#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define rep(i,n)for(lld i=0;i<(lld)(n);i++)
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, S = 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--) {
		S += a[i];
		ans += mi(next + a[i],S);
		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);
}