Language: C++

#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;

#define endl '\n'
#define ff first
#define ss second
#define sz(v) (int)v.size()
#define int long long
#define pb push_back
#define f(i,l,r) for(ll i=l;i<=r;i++)
#define rf(i,r,l) for(ll i=r;i>=l;i--)
#define Max 5000001
#define inf 2147483647
#define llinf 4223372036854775807
#define all(v) v.begin(),v.end()
#define bp(n) __builtin_popcountll(n)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

#define d0(a) cout<<a<<' ';
#define d1(a) cout<<a<<endl;
#define d2(a,b) cout<<a<<' '<<b<<endl;
#define d3(a,b,c) cout<<a<<' '<<b<<' '<<c<<endl;
#define d4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
#define d5(a,b,c,d,e) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<endl;
#define d6(a,b,c,d,e,f) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<' '<<f<<endl;

const ll N=1e5+5,mod=1e9+7,bit=61;

ll a[N],n,on[N];


signed main()
{
    fast;

    ll t=1;
    //cin>>t;
    while(t--)
    {
        cin>>n;
        f(i,0,n)
        {
            cin>>a[i];
        }
        if(n>0 and a[0]!=0)
        {
            cout<<-1<<endl;
            continue;
        }
        on[0]=1;
        f(i,1,n)
        {
            on[i]=min(llinf,2ll*(on[i-1]-a[i-1]));
        }
        ll bl=0,ans=0;
        rf(i,n,0)
        {
            ll l=(bl+1)/2;
            ll r=bl;
            l+=a[i];
            r+=a[i];
            ll my=min(on[i],r);
            if(my<l)
            {
                ans=-1;
                break;
            }
            bl=my;
            ans+=my;
        }
        cout<<ans<<endl;
    }
    return 0;
}


















Compiler output:
a.cc: In function 'int main()':
a.cc:58:22: error: no matching function for call to 'min(long int, long long int)'
   58 |             on[i]=min(llinf,2ll*(on[i-1]-a[i-1]));
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:58:22: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'long long int')
   58 |             on[i]=min(llinf,2ll*(on[i-1]-a[i-1]));
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:58:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   58 |             on[i]=min(llinf,2ll*(on[i-1]-a[i-1]));
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~