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

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 1000000000
#define INF2 100000000000
#define LLINF 1000000000000000
#define MOD 1000000007
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define LOOP(i, N) for (int i = 0; i < N; i++)
#define LOOP1(i, N) for (int i = 1; i <= N; i++)
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
#define int long long

signed main() {
  int n;
  cin >> n;
  n++;
  if (n == 1) {
    int a;
    cin >> a;
    if (a == 1) {
      cout << 1 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }

  int a[n];
  int mi[100002] = {};
  int ma[100002] = {};
  int can[100002] = {};
  LOOP(i, n) { cin >> a[i]; }
  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  can[0] = 1;
  LOOP1(i, n - 1) {
    can[i] = can[i - 1] * 2 - a[i];
    if (can[i] > LLINF) {
      can[i] = LLINF;
    }
    // cerr << can[i] << " ";
  }
  // cerr << endl;
  ma[n - 1] = a[n - 1];
  mi[n - 1] = a[n - 1];

  LOOP1(cnt, n - 1) {
    int i = n - cnt - 1;
    // cerr << i << "," << a[i] << ":";

    if (a[i] > 0) {
      mi[i] = mi[i + 1] / 2 + a[i];
    } else {
      mi[i] = (mi[i + 1] + 1) / 2;
    }

    ma[i] = ma[i + 1] + a[i];
    // cerr << mi[i] << "->" << ma[i] << endl;
  }
  int ans = 0;
  LOOP(i, n) {
    // cerr << mi[i] << "." << can[i]+a[i] << "," <<ma[i] << endl;
    if (mi[i] > (can[i] + a[i])) {
      cout << -1 << endl;
      return 0;
    }
    ans += min(ma[i], can[i] + a[i]);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 1000000000
#define INF2 100000000000
#define LLINF 1000000000000000
#define MOD 1000000007
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define LOOP(i, N) for (int i = 0; i < N; i++)
#define LOOP1(i, N) for (int i = 1; i <= N; i++)
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
#define int long long

signed main() {
  int n;
  cin >> n;
  n++;
  if (n == 0) {
    int a;
    cin >> a;
    if (a == 1) {
      cout << 1 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }

  int a[n];
  int mi[100002] = {};
  int ma[100002] = {};
  int can[100002] = {};
  LOOP(i, n) { cin >> a[i]; }
  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  can[0] = 1;
  LOOP1(i, n - 1) {
    can[i] = can[i - 1] * 2 - a[i];
    // if(can[i]>LLINF){
    //   can[i] = LLINF;
    // }
    // cerr << can[i] << " ";
  }
  // cerr << endl;
  ma[n - 1] = a[n - 1];
  mi[n - 1] = a[n - 1];

  LOOP1(cnt, n - 1) {
    int i = n - cnt - 1;
    // cerr << i << "," << a[i] << ":";

    if (a[i] > 0) {
      mi[i] = mi[i + 1] / 2 + a[i];
    } else {
      mi[i] = (mi[i + 1] + 1) / 2;
    }

    ma[i] = ma[i + 1] + a[i];
    // cerr << mi[i] << "->" << ma[i] << endl;
  }
  int ans = 0;
  LOOP(i, n) {
    if (can[i] < 0)
      return 1;
    // cerr << mi[i] << "." << can[i]+a[i] << "," <<ma[i] << endl;
    if (mi[i] > (can[i] + a[i])) {
      cout << -1 << endl;
      return 0;
    }
    ans += min(ma[i], can[i] + a[i]);
  }
  cout << ans << endl;
}