#include<stdio.h>
int main()
{
	int n;
	int a[100002],l[100002];  //共n+1个数;l[i]存储第i层的结点总数 
	int i,ans=0,mi=2;  //mi代表第n层最节点数 
	scanf("%d",&n);
	scanf("%d",&a[0]);
	if((n==0&&a[0]>1)||(n!=0&&a[0]>=1))
	{
		printf("-1");
		return 0;
	}
	for(i=1;i<=n-1;i++)
	{
		scanf("%d",&a[i]);
		if(a[i]>=mi||a[i]>mi-2*a[i-1])
		{
			printf("-1");
			return 0;
		}
		mi*=2;
	}
	if(n!=0)
	{
		scanf("%d",&a[n]);
		if(a[n]>mi||a[n]>mi-2*a[n-1])
		{
			printf("-1");
			return 0;
		}
	}
	l[n]=a[n];
	mi/=2;  //第n-1层 
	ans+=l[n];
	for(i=n-1;i>=0;i--)
	{
		if(mi<=a[i]+l[i+1]&&mi<=mi-2*a[i-1])
		{
			l[i]=mi;
		}
		if(a[i]+l[i+1]<=mi&&a[i]+l[i+1]<=mi-2*a[i-1])
		{
			l[i]=a[i]+l[i+1];
		}
		if(mi-2*a[i-1]<=mi&&mi-2*a[i-1]<=a[i]+l[i+1])
		{
			l[i]=mi-2*a[i-1];
		}
		ans+=l[i];
		mi/=2;
	}
	printf("%d",ans);
	return 0;
}