Language: C++

#include<bits/stdc++.h>
#define reg register
typedef long long ll;
using namespace std;
const int MN=1e5+5;
int n,a[MN],node[MN];
ll pw[MN],cnt[MN];
ll tr[MN<<2],tag[MN<<2];
#define mid (l+r>>1)
#define ls (t<<1)
#define rs (t<<1|1)
void build(int t,int l,int r){
	if(l==r){tr[t]=cnt[l];return;}
	build(ls,l,mid);build(rs,mid+1,r);
	tr[t]=tr[ls]+tr[rs];
}
inline void pushtag(int t,int l,int r,ll v){tag[t]+=v;tr[t]+=v*(r-l+1);}
inline void pushdown(int t,int l,int r){
	if(!tag[t])return;
	pushtag(ls,l,mid,tag[t]);
	pushtag(rs,mid+1,r,tag[t]);
	tag[t]=0;
}
void update(int t,int l,int r,int a,int b,ll v){
	if(a<=l&&r<=b)return pushtag(t,l,r,v);
	pushdown(t,l,r);
	if(a<=mid)update(ls,l,mid,a,b,v);
	if(b>mid)update(rs,mid+1,r,a,b,v);
	tr[t]=tr[ls]+tr[rs];
}
ll query(int t,int l,int r,int pos){
	if(l==r)return tr[t];
	pushdown(t,l,r);
	if(!tr[t])return 0;
	if(pos<=mid)return query(ls,l,mid,pos);
	return query(rs,mid+1,r,pos);
}
int main(){
//	freopen("t.in","r",stdin);
	scanf("%d",&n);
	for(reg int i=0;i<=n;i++)scanf("%d",a+i);
	for(reg int i=n;i;i--){
		node[i]+=a[i];
		node[i-1]+=(node[i]+1)/2;
	}
	node[0]+=a[0];
	for(reg int i=0,base=1;i<31;i++,base*=2)
		if(node[i]>base)return 0*puts("-1");
	pw[0]=1;reg ll Ans=0;
	for(reg int i=0;i<=n;i++)Ans+=node[i];
	for(reg int i=0;i<=n;i++)cnt[i]=(node[i+1]&1);
	build(1,0,n);reg int cur=0;
	while(!query(1,0,n,cur)&cur<=n)cur++;
	for(reg int i=n;i;i--){
		reg int rem=min(a[i],node[i]-a[i]+(a[i]-node[i]+a[i])/2);
		while(rem&&cur<i){
			reg ll lim=min(rem,query(1,0,n,cur));
			Ans+=1ll*(i-cur-1)*lim;rem-=lim;
			update(1,0,n,cur,cur,-lim);
			update(1,0,n,cur+1,n,lim);
			if(!query(1,0,n,cur))cur++;
		}
		if(cur>=i)break;
	}
	printf("%lld\n",Ans);
	return 0;
}
/*
6
0 0 1 1 2 3 5
*/

Compiler output:
a.cc: In function 'int main()':
a.cc:41:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   41 |         for(reg int i=0;i<=n;i++)scanf("%d",a+i);
      |                     ^
a.cc:42:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   42 |         for(reg int i=n;i;i--){
      |                     ^
a.cc:47:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 |         for(reg int i=0,base=1;i<31;i++,base*=2)
      |                     ^
a.cc:47:25: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 |         for(reg int i=0,base=1;i<31;i++,base*=2)
      |                         ^~~~
a.cc:49:24: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   49 |         pw[0]=1;reg ll Ans=0;
      |                        ^~~
a.cc:50:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   50 |         for(reg int i=0;i<=n;i++)Ans+=node[i];
      |                     ^
a.cc:51:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   51 |         for(reg int i=0;i<=n;i++)cnt[i]=(node[i+1]&1);
      |                     ^
a.cc:52:30: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   52 |         build(1,0,n);reg int cur=0;
      |                              ^~~
a.cc:54:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   54 |         for(reg int i=n;i;i--){
      |                     ^
a.cc:55:25: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   55 |                 reg int rem=min(a[i],node[i]-a[i]+(a[i]-node[i]+a[i])/2);
      |                         ^~~
a.cc:57:39: error: no matching function for call to 'min(int&, ll)'
   57 |                         reg ll lim=min(rem,query(1,0,n,cur));
      |                                    ~~~^~~~~~~~~~~~~~~~~~~~~~
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:57:39: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
   57 |                         reg ll lim=min(rem,query(1,0,n,cur));
      |                                    ~~~^~~~~~~~~~~~~~~~~~~~~~
/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:57:39: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   57 |                         reg ll lim=min(rem,query(1,0,n,cur));
      |                                    ~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:57:32: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   57 |                         reg ll lim=min(rem,query(1,0,n,cur));
      |                                ^~~