Language: C++

#pragma GCC optimize("O3", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define int long long

signed main(void){
	int N; cin >> N;
	vector<int> A(N + 1);
	for (int i = 0; i <= N; ++i)
		cin >> A[i];
	
	if (N == 0){
		if (A[0] == 1)
			cout << 1 << endl;
		else
			cout << -1 << endl;
		return 0;
	}

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

	vector<int> ls(N + 1), rs(N + 1);

	int l, r; l = r = 1;
	ls[0] = rs[0] = 1;
	for (int i = 1; i <= N; ++i){
		int l_nx = l - A[i - 1];
		l_nx = max({1, A[i], l_nx});
		int r_nx = (r - A[i - 1]) * 2;
		l = l_nx, r = r_nx;
		if (r_nx < l_nx){
			cout << -1 << endl;
			return 0;
		}
		ls[i] = l, rs[i] = r;
	}

	if (ls[N] > A[N] || rs[N] < A[N]){
		cout << -1 << endl;
		return 0;
	}

	int c = A[N];
	ll res = A[N];

	for (int i = N - 1; i >= 0; --i){
		l = (c + 1) / 2 + A[i];
		l = max({1, A[i], l});
		r = c + A[i];
		c = min(rs[i], r);
		if (!(l <= c && c <= r)){
			cout << -1 << endl;
			return 0;
		}
		if (!(ls[i] <= c && c <= rs[i])){
			cout << -1 << endl;
			return 0;
		}
		res += c;
	}

	cout << res << endl;

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:36:27: error: no matching function for call to 'max(<brace-enclosed initializer list>)'
   36 |                 l_nx = max({1, A[i], l_nx});
      |                        ~~~^~~~~~~~~~~~~~~~~
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:3:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 1 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   template argument deduction/substitution failed:
a.cc:36:27: note:   deduced conflicting types for parameter '_Tp' ('int' and 'long long int')
   36 |                 l_nx = max({1, A[i], l_nx});
      |                        ~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 1 provided
a.cc:56:24: error: no matching function for call to 'max(<brace-enclosed initializer list>)'
   56 |                 l = max({1, A[i], l});
      |                     ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   template argument deduction/substitution failed:
a.cc:56:24: note:   deduced conflicting types for parameter '_Tp' ('int' and 'long long int')
   56 |                 l = max({1, A[i], l});
      |                     ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 1 provided