Language: C++

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define print(A,n) for(ll i=0;i<n;++i)cout<<A[i]<<' ';cout<<endl;
#define take(A,n) for(ll i=0;i<n;++i)cin>>A[i];
//    for(auto& it : m)
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);}
//  priority_queue<ll int> pq;
//  priority_queue <ll int, vector<ll int>, greater<ll int> > pq;
//  vector<ll int> v;
//  vector< pair<ll int,ll int> > v;
//  set<ll int>::iterator it=s.begin();
//  unordered_map<  pair<ll int,ll int>, ll int> mp;

 

ll int power(ll int x, ll int y, ll int p)  
{  
    ll int res = 1;     // Initialize result  
  
    x = x % p; // Update x if it is more than or  
                // equal to p 
   
    if (x == 0) return 0; // In case x is divisible by p; 
  
    while (y > 0)  
    {  
        // If y is odd, multiply x with result  
        if (y & 1)  
            res = (res*x) % p;  
  
        // y must be even now  
        y = y>>1; // y = y/2  
        x = (x*x) % p;  
    }  
    return res;  
}  

void computeLPSArray(string pat, int M, int* lps) 
{ 
    // length of the previous longest prefix suffix 
    int len = 0; 
  
    lps[0] = 0; // lps[0] is always 0 
  
    // the loop calculates lps[i] for i = 1 to M-1 
    int i = 1; 
    while (i < M) { 
        if (pat[i] == pat[len]) { 
            len++; 
            lps[i] = len; 
            i++; 
        } 
        else // (pat[i] != pat[len]) 
        { 
            // This is tricky. Consider the example. 
            // AAACAAAA and i = 7. The idea is similar 
            // to search step. 
            if (len != 0) { 
                len = lps[len - 1]; 
  
                // Also, note that we do not increment 
                // i here 
            } 
            else // if (len == 0) 
            { 
                lps[i] = 0; 
                i++; 
            } 
        } 
    } 
}


int main()
{
  fastio();

 
ll int T;
T=1;
while(T--)
{	
ll int n;
cin>>n;
ll int a[n+1],ans=0,i;
for(i=0;i<=n;i++)
{

	cin>>a[i];


}
ll int tot=1,ans1=0;ll int cnt[n+1]={0};
if(a[0]>1)
{cout<<-1;return 0;}
if(a[0]==1)
	{cnt[0]=1;

		for(i=1;i<=n;i++)
			if(a[i]>0)
			{
				cout<<-1;return 0;
			}
			cout<<1;
			return 0;
	}

cnt[0]=1;
for(i=1;i<=n;i++)
{
	
	if(a[i]>2*(cnt[i-1]))
		{cout<<-1;return 0;}

cnt[i]=2*(cnt[i-1])-a[i];
ans+=cnt[i];
//cout<<cnt[i]<<endl;

//tot=2*(tot-a[i]);


}
tot=a[n];
 ans=a[n];
for(i=n-1;i>=0;i--)
{

if(tot>2*(cnt[i]-a[i])){
	cout<<-1;return 0;
}	
ans+=a[i]+min(tot,cnt[i]);
	//ans+=a[i]+min(cnt[i]-a[i],tot);
	tot=a[i]+min(tot,cnt[i]);
}
//ans++;
cout<<ans<<endl;

}
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define print(A,n) for(ll i=0;i<n;++i)cout<<A[i]<<' ';cout<<endl;
#define take(A,n) for(ll i=0;i<n;++i)cin>>A[i];
//    for(auto& it : m)
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);}
//  priority_queue<ll int> pq;
//  priority_queue <ll int, vector<ll int>, greater<ll int> > pq;
//  vector<ll int> v;
//  vector< pair<ll int,ll int> > v;
//  set<ll int>::iterator it=s.begin();
//  unordered_map<  pair<ll int,ll int>, ll int> mp;

 

ll int power(ll int x, ll int y, ll int p)  
{  
    ll int res = 1;     // Initialize result  
  
    x = x % p; // Update x if it is more than or  
                // equal to p 
   
    if (x == 0) return 0; // In case x is divisible by p; 
  
    while (y > 0)  
    {  
        // If y is odd, multiply x with result  
        if (y & 1)  
            res = (res*x) % p;  
  
        // y must be even now  
        y = y>>1; // y = y/2  
        x = (x*x) % p;  
    }  
    return res;  
}  

void computeLPSArray(string pat, int M, int* lps) 
{ 
    // length of the previous longest prefix suffix 
    int len = 0; 
  
    lps[0] = 0; // lps[0] is always 0 
  
    // the loop calculates lps[i] for i = 1 to M-1 
    int i = 1; 
    while (i < M) { 
        if (pat[i] == pat[len]) { 
            len++; 
            lps[i] = len; 
            i++; 
        } 
        else // (pat[i] != pat[len]) 
        { 
            // This is tricky. Consider the example. 
            // AAACAAAA and i = 7. The idea is similar 
            // to search step. 
            if (len != 0) { 
                len = lps[len - 1]; 
  
                // Also, note that we do not increment 
                // i here 
            } 
            else // if (len == 0) 
            { 
                lps[i] = 0; 
                i++; 
            } 
        } 
    } 
}


int main()
{
  fastio();

 
ll int T;
T=1;
while(T--)
{	
ll int n;
cin>>n;
ll int a[n+1],ans=0,i;
for(i=0;i<=n;i++)
{

	cin>>a[i];


}
ll int tot=1,ans1=0;ll int cnt[n+1]={0};
if(a[0]>1)
{cout<<-1;return 0;}
if(a[0]==1)
	{cnt[0]=1;

		for(i=1;i<=n;i++)
			if(a[i]>0)
			{
				cout<<-1;return 0;
			}
			cout<<1;
			return 0;
	}

cnt[0]=1;
for(i=1;i<=n;i++)
{
	
	if(a[i]>2*(cnt[i-1]))
		{cout<<-1;return 0;}

cnt[i]=2*(cnt[i-1])-a[i];
ans+=cnt[i];
//cout<<cnt[i]<<endl;

//tot=2*(tot-a[i]);


}
tot=a[n];
 ans=a[n];
for(i=n-1;i>=0;i--)
{

if(tot>2*(cnt[i]-a[i])){
	cout<<-1;return 0;
}	
ans+=a[i]+min(tot,cnt[i]);
	//ans+=a[i]+min(cnt[i]-a[i],tot);
	tot=a[i]+min(tot,cnt[i]);
}
//ans++;
cout<<ans<<endl;

}
    return 0;
}


Compiler output:
a.cc:150:6: error: redefinition of 'void fastio()'
  150 | void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);}
      |      ^~~~~~
a.cc:8:6: note: 'void fastio()' previously defined here
    8 | void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);}
      |      ^~~~~~
a.cc:160:8: error: redefinition of 'long long int power(long long int, long long int, long long int)'
  160 | ll int power(ll int x, ll int y, ll int p)
      |        ^~~~~
a.cc:18:8: note: 'long long int power(long long int, long long int, long long int)' previously defined here
   18 | ll int power(ll int x, ll int y, ll int p)
      |        ^~~~~
a.cc:182:6: error: redefinition of 'void computeLPSArray(std::string, int, int*)'
  182 | void computeLPSArray(string pat, int M, int* lps)
      |      ^~~~~~~~~~~~~~~
a.cc:40:6: note: 'void computeLPSArray(std::string, int, int*)' previously defined here
   40 | void computeLPSArray(string pat, int M, int* lps)
      |      ^~~~~~~~~~~~~~~
a.cc:218:5: error: redefinition of 'int main()'
  218 | int main()
      |     ^~~~
a.cc:76:5: note: 'int main()' previously defined here
   76 | int main()
      |     ^~~~