Language: C++

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
typedef pair<ll,ll> pi;
typedef vector<ll> vi;
ll n;
ll leave[100005];
ll answer = 0;
pi minmax[100005];
const ll INF = 1000000000;

ll power(ll a, ll b){
	if (b == 0) return 1;
	if (b == 1) return a;
	ll half = power(a, b/2);
	half *= half;
	if (b % 2) half *= a;
	return half;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	REP(i, 0, n + 1){
		cin >> leave[i];
	}
	if (n == 0 && leave[0] == 1){
		cout << 1 << endl;
		return 0;
	} else if (n == 0){
		cout << -1 << endl;
		return 0;
	}
	for (int i = 0; i <= n; i++){
		if (i == 0){
			minmax[i] = make_pair(1, 1);
			continue;
		}
		
		minmax[i] = MP(max(minmax[i - 1].F - leave[i - 1], (ll)0), min(INF, 2*(minmax[i - 1].S - leave[i - 1])));
		if (minmax[i].S < leave[i]){
			cout << -1 << endl;
			return 0;
		}
	}
	ll node;
	for (int i = n; i >= 0; i--){
		if (i == n){
			answer += leave[i];
			node = leave[i];
			continue;
		}
		node += leave[i];
		node = min(node, minmax[i].S);
		answer += node;
	}
	cout << answer << endl;
	

}

Compiler output:
a.cc: In function 'int main()':
a.cc:42:25: error: reference to 'minmax' is ambiguous
   42 |                         minmax[i] = make_pair(1, 1);
      |                         ^~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:14:4: note:                 'pi minmax [100005]'
   14 | pi minmax[100005];
      |    ^~~~~~
a.cc:46:17: error: reference to 'minmax' is ambiguous
   46 |                 minmax[i] = MP(max(minmax[i - 1].F - leave[i - 1], (ll)0), min(INF, 2*(minmax[i - 1].S - leave[i - 1])));
      |                 ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:14:4: note:                 'pi minmax [100005]'
   14 | pi minmax[100005];
      |    ^~~~~~
a.cc:46:36: error: reference to 'minmax' is ambiguous
   46 |                 minmax[i] = MP(max(minmax[i - 1].F - leave[i - 1], (ll)0), min(INF, 2*(minmax[i - 1].S - leave[i - 1])));
      |                                    ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:14:4: note:                 'pi minmax [100005]'
   14 | pi minmax[100005];
      |    ^~~~~~
a.cc:46:88: error: reference to 'minmax' is ambiguous
   46 |                 minmax[i] = MP(max(minmax[i - 1].F - leave[i - 1], (ll)0), min(INF, 2*(minmax[i - 1].S - leave[i - 1])));
      |                                                                                        ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:14:4: note:                 'pi minmax [100005]'
   14 | pi minmax[100005];
      |    ^~~~~~
a.cc:47:21: error: reference to 'minmax' is ambiguous
   47 |                 if (minmax[i].S < leave[i]){
      |                     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:14:4: note:                 'pi minmax [100005]'
   14 | pi minmax[100005];
      |    ^~~~~~
a.cc:60:34: error: reference to 'minmax' is ambiguous
   60 |                 node = min(node, minmax[i].S);
      |                                  ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:14:4: note:                 'pi minmax [100005]'
   14 | pi minmax[100005];
      |    ^~~~~~