Language: C++

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007
#define llMAX 9223372036854775807
#define llMIN -9223372036854775808
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,k,x,y;

int main(){
  ll ans=0;
  cin >> n ;
  vi a(n+1),b(n+1),c(n+1),d(n+1);
  REP(i,n+1) cin>>a[i];


  if (a[0]!=0){
    if (n!=0){
      cout<<-1<<endl;
      return 0;
    }else{
      if (a[0]!=1){
        cout<<-1<<endl;
        return 0;
      }
      cout<<1<<endl;
      return 0;
    }
  }



  if (n==0){
    if (a[0]==0){
      cout<<1<<endl;
      return 0;
    }
  }







  
  ll nown=1;
  b[0]=1;
  c[0]=1;
  for(ll i=1;i<n+1;i++){
    nown=nown*2;
    b[i]=min(nown,llMAX/2);
    nown-=a[i];
    c[i]=nown;
    if (nown<0){
      cout<<-1<<endl;
      return 0;
    }
  }




  ll lasti=-1;
  d[n]=a[n];
  for(ll i=n-1;i>=0;i--){
    d[i]=d[i+1]+a[i];

    if (lasti==-1){
      if (b[i]<d[i]){
        lasti=i;
      }
    }
  }



  //cout<<"!!!!"<<lasti<<endl;
  lasti++;
    //この時点のnownとしたから数えたやつを比較
  b[lasti]=d[lasti];
  c[lasti]=d[lasti]-a[lasti];
  for(ll j=lasti+1;j<n+1;j++){
    b[j]=d[j];
  }
    







  int flag=0;
  for(ll i=0;i<n+1;i++){
    ans+=b[i];
    //cout<<b[i]<<" "<<c[i]<<endl;
    flag+=(b[i]<=0);
  }
  if (flag!=0){
    cout<<-1<<endl;
    return 0;
  }



  cout<<ans<<endl;
  return 0;
}


/*
4
0 1 0 0 8
*/

Compiler output:
a.cc: In function 'int main()':
a.cc:55:13: error: no matching function for call to 'min(long long int&, long int)'
   55 |     b[i]=min(nown,llMAX/2);
      |          ~~~^~~~~~~~~~~~~~
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:55:13: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long int')
   55 |     b[i]=min(nown,llMAX/2);
      |          ~~~^~~~~~~~~~~~~~
/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:55:13: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   55 |     b[i]=min(nown,llMAX/2);
      |          ~~~^~~~~~~~~~~~~~