#include <stdio.h>

int main(void) {
	int n[100001] = { 0 };
	int an[100001] = { 0 };
	int num = 0;

	for (int i = 0; i < 100001; i++) {
		n[i] = 2;
	}
	n[0] = 1;

	scanf("%d", &num);
	for (int i = 0; i <= num; i++) {
		int tmp = 0;
		scanf("%d", &tmp);
		an[i] = tmp;
	}

	int aa = 1;
	for (int i = 1; i <= num; i++) {
		aa *= 2;
	}
int cc=0;
	int bb = 2;
	for (int i = 0; n[num] != aa; i++) {
		if (n[i] == an[i]) {
          cc++;
			break;
		}
      if (i == num) {
        for (int j = bb; j <= num; j++) {
          if (j == bb) {
            n[j] += 1;
          }
          else if (j > bb) {
            n[j] += 2;
          }
        }
        i = bb;
        bb++;
      }
	}

	if (cc == 0) {
		printf("-1");
	}
	else {
		int result = 0;
		for (int i = 0; i <= num; i++) {
			result += n[i];
		}
		printf("%d", result);
	}

	return 0;
}