#include <stdio.h>
typedef long long ll;
int main() {
	int n, a[100005];
	ll ans = 0;
	scanf("%d", &n);
	for (int i = 0; i < n + 1; i++) {
		scanf("%d", &a[i]);
		if (i <= 30 && a[i] > (1 << i)) {
			printf("-1\n");
			return 0;
		}
	}
	if (a[0] != 0) {
		printf("-1\n");
		return 0;
	}
	for (int i = n; i > 0; i--) {
		for (int j = 0; j < n; j++) {
			a[i] -= (1 << j);
			ans += (ll)(n - j) * ((1 << j));
			if (a[i] <= 0)
				break;
		}
	}
	printf("%lld\n", ans);
	return 0;
}