Problem p02665 (C++) — buggy submission, verdict: Runtime Error

// #pragma GCC optimize("Ofast",3,"inline")
// #include<bits/stdc++.h>
// using namespace std;
// #define fir first
// #define sec second
// #define m_p make_pair
// #define y1 ygftgfgcdtfgxffgx
// #define y2 yfdsesgvtyghftfvv
// #define x1 xvyr6cf6fgcfgf676
// #define x2 xcr6rfc5r66y6r6fr
// #define up_bound upper_bound
// #define low_bound lower_bound
// #define next_per next_permutation
// #define pb push_back
// #define i_to_s to_string
// typedef priority_queue<int> p_queue;
// typedef priority_queue<int, vector<int>, greater<int> > min_p_queue;
// typedef long long ll;
// typedef long double ld;
// typedef pair<int,int> pii;
// typedef pair<ll,ll> pll;
// int mon[20]={31,28,31,30,31,30,31,31,30,31,30,31};
// ll gcd(ll x,ll y){return ((y==0)?x:gcd(y,x%y));}
// ll n,a[100010];
// ll num[100010];
// ll ans,last;
// int main(){
//	ios_base::sync_with_stdio(false);
//	cin.tie(0);
////	freopen(".in","r",stdin);
////	freopen(".out","w",stdout);
//	cin>>n;
//	for(int i=0;i<=n;i++)cin>>a[i];
//	if(n==0){if(a[0]==0)cout<<-1;if(a[0]==1)cout<<1;if(a[0]>1)cout<<-1;return
//0;}num[0]=1; 	if(a[0]==1){cout<<-1;return 0;} 	for(int i=1;i<n;i++){
//		num[i]=num[i-1]*2-a[i];
////		cout<<num[i]<<'\\n';
//		if(num[i]<=0){
//			cout<<-1;
//			return 0;
//		}
//	}
////	cout<<'\\n';
//	num[n]=num[n-1]*2;
//	if(num[n]<a[n]){
//		cout<<-1;
//		return 0;
//	}
//	ans=a[n],last=a[n];
//	for(int i=n-1;i>=0;i--){
////		cout<<ans<<'\\n';
//		last=min(last,num[i])+a[i];
//		ans+=last;
//	}
//	cout<<ans;
//	return 0;
//}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 4000000000000000000;
ll a[100005];
ll b[100005];
ll c[100005];
ll mxbc[100005];
ll ans = 0;
int n;
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  mxbc[0] = 1 - a[0];
  for (int i = 1; i <= n; i++) {
    mxbc[i] = min(2 * mxbc[i - 1] - a[i], INF);
    if (mxbc[i] < 0ll) {
      printf("-1");
      return 0;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    /// b + c <= mxbc
    /// b + 2*c = sum
    /// c >= sum - mxbc
    ll sum = a[i + 1] + b[i + 1] + c[i + 1];
    ll C = max(0ll, sum - mxbc[i]);
    ll B = sum - 2 * C;
    b[i] = B, c[i] = C;
    if (B < 0ll || B + 2 * C != sum) {
      printf("-1\\n");
      return 0;
    }
    ans += a[i] + B + C;
  }
  ans += a[n];
  if (n == 0 && a[0] != 1)
    printf("-1\\n");
  else
    printf("%lld", ans);
}


Error: WA

Fixed submission:

#pragma GCC optimize("Ofast", 3, "inline")
#include <bits/stdc++.h>
using namespace std;
#define fir first
#define sec second
#define m_p make_pair
#define y1 ygftgfgcdtfgxffgx
#define y2 yfdsesgvtyghftfvv
#define x1 xvyr6cf6fgcfgf676
#define x2 xcr6rfc5r66y6r6fr
#define up_bound upper_bound
#define low_bound lower_bound
#define next_per next_permutation
#define pb push_back
#define i_to_s to_string
typedef priority_queue<int> p_queue;
typedef priority_queue<int, vector<int>, greater<int>> min_p_queue;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int mon[20] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll gcd(ll x, ll y) { return ((y == 0) ? x : gcd(y, x % y)); }
ll n, a[100010];
ll num[100010];
ll ans, last;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  cin >> n;
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  if (n == 0) {
    cout << 0;
    return 1;
  }
  num[0] = 1;
  if (a[0] == 1) {
    cout << -1;
    return 0;
  }
  for (int i = 1; i < n; i++) {
    num[i] = num[i - 1] * 2 - a[i];
    //		cout<<num[i]<<'\\n';
    if (num[i] <= 0) {
      cout << -1;
      return 0;
    }
  }
  //	cout<<'\\n';
  num[n] = num[n - 1] * 2;
  if (num[n] < a[n]) {
    cout << -1;
    return 0;
  }
  ans = a[n], last = a[n];
  for (int i = n - 1; i >= 0; i--) {
    //		cout<<ans<<'\\n';
    last = min(last, num[i]) + a[i];
    ans += last;
  }
  cout << ans;
  return 0;
}