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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const ll INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MAX = 10000000;
/****** define constant end ********/
/****** define variable start ******/
ll n;
vl a;
/******* define variable end *******/
int solve() {
  cin >> n;
  a.resize(n + 1);
  ll ma = 0;
  ll not_node = 0;
  REP(i, n + 1) {
    cin >> a[i];
    if (a[i] > ma)
      ma = a[i];
    not_node += a[i];
  }
  ll ans = 0;
  ll node = 0;
  if (n != 0 && a[0] == 1) {
    cout << -1 << endl;
    return 0;
  }
  if (n == 0 && a[0] != 1) {
    cout << -1 << endl;
    return 0;
  }
  REP(i, n + 1) {
    not_node -= a[i];
    if (i != 0) {
      if ((node - a[i - 1]) * 2 < a[i]) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (i == 0) {
      ll one = 1;
      node = min(not_node + a[i], one);
    } else if (i != n) {
      node = min(not_node + a[i], (node - a[i - 1]) * 2);
    } else {
      node = a[i];
    }
    ans += node;
  }
  cout << ans << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const ll INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MAX = 10000000;
/****** define constant end ********/
/****** define variable start ******/
ll n;
vl a;
/******* define variable end *******/
int solve() {
  cin >> n;
  a.resize(n);
  ll ma = 0;
  ll not_node = 0;
  REP(i, n + 1) {
    cin >> a[i];
    ma = max(a[i], ma);
    not_node += a[i];
  }
  ll ans = 0;
  ll node = 0;
  REP(i, n + 1) {
    not_node -= a[i];
    if (i != 0 && (node - a[i - 1]) * 2 < a[i]) {
      cout << -1 << endl;
      return 0;
    }
    if (i == 0) {
      node = min(ma + a[i], (ll)1);
    } else if (i != n) {
      node = min(ma + (not_node - ma) + a[i], (node - a[i - 1]) * 2);
    } else {
      node = a[i];
    }
    ans += node;
  }
  cout << ans << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}