#include <stdio.h>
#include <math.h>
int main(){
  int n,ans1=0;
  scanf("%d", &n);
  int a[n];
  int ans[n];
  int temp,k;
  ans[0]=1;
  for(int i=0;i<n+1;i++){
    scanf("%d", &a[i]);
  //  printf("%d", a[i]);
    if(n!=0&&a[0]>=1){
      ans1 = -1;
      break;
    }
    if(a[i]>pow(2,i)){
      ans1 = -1;
      break;
    }
  }
  if(ans1 != -1){
    for(int i=1;i<n;i++){
    ans[i] = (ans[i-1]-a[i-1])*2;
    //printf("%d\n", ans[i]);
   }
   if(ans[n-1]>a[n]){
     temp = ans[n-1]-a[n];
     ans[n-1]=ans[n-1]-(ans[n-1]-a[n]);
   }
   k=n;
   temp = temp-a[n];
   while(temp!=1){
     //printf("%d\n", temp);
     ans[k-2] = ans[k-2]-temp/2-temp%2;
     temp = temp/2+temp%2;
     k--;
   }
   for(int i=0;i<n;i++){
     ans1 = ans1 + ans[i];
    // printf("%d", ans1);
   }
  ans1 = ans1 +a[n];
  }
  printf("%d", ans1);
  return 0;
}