Language: C++

#include <iostream>
using namespace std;

typedef long long ll;

ll a[100005];
ll size[100005],sum[100005];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i <= n; i++)
	{
		cin >> a[i];
		if( i != 0 ) sum[i] = sum[i-1] + a[i];
	}
	if( a[0] != 0 )
	{
		cout << -1 << '\n';
		return 0;
	}
	ll ans = 1;
	ll temp = 0;
	size[0] = 2;
	int index = 0;
	ll help = 0;
	for (int i = 1; i <= n; )
	{
		if( a[i] == 0 )
		{
			i ++;
			continue;
		}
		ll t = min(a[i],size[index]);
		if( t == 0 )
		{
			cout << -1 << '\n';
			return 0;
		}
		ans += t * (i - index);
		help += t;
		a[i] -= t;
		size[index] -= t;
		if( size[index] == 0 )
		{
			index ++;
			size[index] = help - sum[index];
		}
		if( a[i] == 0 ) i++;
	}
	cout << ans << '\n';
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:27:9: error: reference to 'size' is ambiguous
   27 |         size[0] = 2;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:4: note:                 'll size [100005]'
    7 | ll size[100005],sum[100005];
      |    ^~~~
a.cc:37:33: error: reference to 'size' is ambiguous
   37 |                 ll t = min(a[i],size[index]);
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:4: note:                 'll size [100005]'
    7 | ll size[100005],sum[100005];
      |    ^~~~
a.cc:46:17: error: reference to 'size' is ambiguous
   46 |                 size[index] -= t;
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:4: note:                 'll size [100005]'
    7 | ll size[100005],sum[100005];
      |    ^~~~
a.cc:47:21: error: reference to 'size' is ambiguous
   47 |                 if( size[index] == 0 )
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:4: note:                 'll size [100005]'
    7 | ll size[100005],sum[100005];
      |    ^~~~
a.cc:50:25: error: reference to 'size' is ambiguous
   50 |                         size[index] = help - sum[index];
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:4: note:                 'll size [100005]'
    7 | ll size[100005],sum[100005];
      |    ^~~~