Language: C++

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long int;
#define min(x,y)        if(x>y) return y; return x;
#define INF             1e14
#define dd              double
#define MOD             1000000007
#define my_sizeof(type) ((char *)(&type+1)-(char*)(&type)) 
#define nl              cout<<endl
#define fill(a,val)     memset(a,val,sizeof(a))
#define mp              make_pair
#define endl            "\n"
#define pb              push_back
#define ff              first    
#define ss              second
#define SIZE            200005
#define all(v)          v.begin(),v.end()
#define s(ar,n)         sort(ar,ar+n)
#define rs(ar,n)        sort(ar, ar+n, greater<ll>()) 
#define oa(a,n)         for(ll i=0;i<n;i++)cout<<a[i]<<" ";nl
#define cn(a,n)         for(ll i=0;i<n;i++)cin>>a[i];
#define maxa(ar,N)      *max_element(ar,ar+N)
#define mina(ar,N)      *min_element(ar,ar+N)
#define fastio()        ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define printclock      cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n";
typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ll power(ll x, ll y){
    ll res=1;x=x;
    while(y>0){
        if(y&1)
            res = (res*x);
        y = y>>1;
        x = (x*x);
    }
    return res;
}  
ll logtwo(ll n){
    if(n==1)
        return 0;
    return logtwo(n/2)+1;
}
signed main()
{
	fastio();   
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
	int t=1;
	//cin>>t;
	while(t--)
	{
		ll N;
		cin>>N;
		N++;
		ll ar[N];
		cn(ar,N);
		pair<ll,ll> dp[N]={};
		dp[N-1]={ar[N-1],ar[N-1]};
		for(ll i=N-2;i>=61;i--)
			dp[i]={(dp[i+1].ff+1)/2+ar[i],dp[i+1].ss+ar[i]};
		for(ll i=min(N-2,60ll);i>=0;i--)
		{
			ll pmx=power(2,i);
			ll mi=(dp[i+1].ff+1)/2+ar[i];
			ll mx=min(pmx,dp[i+1].ss+ar[i]);
			if(mi>pmx)
			{
				cout<<-1<<endl;
				return 0;
			}
			dp[i]={mi,mx};
		}
		ll cur[N]={};
		cur[0]=1;
		ll ans=1;
		for(ll i=1;i<N;i++)
		{
			ll k=min(dp[i].ss,(cur[i-1]-ar[i-1])*2);
			cur[i]=k;
			ans+=k;
		}
		cout<<ans<<endl;
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:6:25: error: expected primary-expression before 'if'
    6 | #define min(x,y)        if(x>y) return y; return x;
      |                         ^~
a.cc:64:26: note: in expansion of macro 'min'
   64 |                 for(ll i=min(N-2,60ll);i>=0;i--)
      |                          ^~~
a.cc:6:43: error: expected primary-expression before 'return'
    6 | #define min(x,y)        if(x>y) return y; return x;
      |                                           ^~~~~~
a.cc:64:26: note: in expansion of macro 'min'
   64 |                 for(ll i=min(N-2,60ll);i>=0;i--)
      |                          ^~~
a.cc:6:43: error: expected ';' before 'return'
    6 | #define min(x,y)        if(x>y) return y; return x;
      |                                           ^~~~~~
a.cc:64:26: note: in expansion of macro 'min'
   64 |                 for(ll i=min(N-2,60ll);i>=0;i--)
      |                          ^~~
a.cc:6:43: error: expected primary-expression before 'return'
    6 | #define min(x,y)        if(x>y) return y; return x;
      |                                           ^~~~~~
a.cc:64:26: note: in expansion of macro 'min'
   64 |                 for(ll i=min(N-2,60ll);i>=0;i--)
      |                          ^~~
a.cc:6:43: error: expected ')' before 'return'
    6 | #define min(x,y)        if(x>y) return y; return x;
      |                                           ^~~~~~
a.cc:64:26: note: in expansion of macro 'min'
   64 |                 for(ll i=min(N-2,60ll);i>=0;i--)
      |                          ^~~
a.cc:64:20: note: to match this '('
   64 |                 for(ll i=min(N-2,60ll);i>=0;i--)
      |                    ^
a.cc:64:40: error: 'i' was not declared in this scope
   64 |                 for(ll i=min(N-2,60ll);i>=0;i--)
      |                                        ^
a.cc:6:25: error: expected primary-expression before 'if'
    6 | #define min(x,y)        if(x>y) return y; return x;
      |                         ^~
a.cc:81:30: note: in expansion of macro 'min'
   81 |                         ll k=min(dp[i].ss,(cur[i-1]-ar[i-1])*2);
      |                              ^~~