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

#ifdef __APPLE__
#include <algorithm>
#include <cmath>
#include <ctgmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
#define GFOR(i, a, n) for (i = a; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define RFOR(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<long long int> VLL;
typedef vector<pair<int, int>> VPI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head

int main() {
  io;
  int n;
  cin >> n;
  VLL a(n + 1);
  for (auto &x : a) {
    cin >> x;
  }

  vector<PLL> r(n + 1);
  if (a[0] > 1) {
    cout << -1 << "\\n";
    return 0;
  }
  if (a[0] == 1) {
    if (SZ(a) > 1) {
      cout << -1 << "\\n";
    } else {
      cout << 1 << "\\n";
    }
    return 0;
  }

  r[0] = {1, 1};
  for (int i = 1; i <= n; i++) {
    r[i] = {r[i - 1].fi, min(LLONG_MAX / 4, (ll)2ll * r[i - 1].se)};
    if (a[i] > r[i].se) {
      cout << -1 << "\\n";
      return 0;
    }
    if (i < n and a[i] == r[i].se) { // check
      cout << -1 << "\\n";
      return 0;
    }
    if (i != n)
      assert(r[i].fi > 0 and r[i].se > 0);
    r[i].se -= a[i];
    r[i].fi -= a[i];
    r[i].fi = max(r[i].fi, 1ll);
    if (i != n)
      assert(r[i].fi > 0 and r[i].se > 0);
  }

  ll ans = a[n];
  ll below = a[n];
  for (int i = n - 1; i >= 0; i--) {
    ll nl = min(r[i].se, below);
    below = nl + a[i];
    ans += below;
  }

  cout << ans << "\\n";
  return 0;
}


Error: -6
4c2f7af3-9714-43e2-b8c2-2412659c24c4.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02665/C++/s161033787.cpp:86: int main(): Assertion `r[i].fi > 0 and r[i].se > 0' failed.


Fixed submission:

#ifdef __APPLE__
#include <algorithm>
#include <cmath>
#include <ctgmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
#define GFOR(i, a, n) for (i = a; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define RFOR(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<long long int> VLL;
typedef vector<pair<int, int>> VPI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head

int main() {
  io;
  int n;
  cin >> n;
  VLL a(n + 1);
  for (auto &x : a) {
    cin >> x;
  }

  vector<PLL> r(n + 1);
  if (a[0] > 1) {
    cout << -1 << "\\n";
    return 0;
  }
  if (a[0] == 1) {
    if (SZ(a) > 1) {
      cout << -1 << "\\n";
    } else {
      cout << 1 << "\\n";
    }
    return 0;
  }

  r[0] = {1, 1};
  for (int i = 1; i <= n; i++) {
    r[i] = {r[i - 1].fi, min((ll)INT_MAX, (ll)2ll * r[i - 1].se)};
    if (a[i] > r[i].se) {
      cout << -1 << "\\n";
      return 0;
    }
    if (i < n and a[i] == r[i].se) { // check
      cout << -1 << "\\n";
      return 0;
    }
    assert(r[i].fi > 0 and r[i].se > 0);
    r[i].se -= a[i];
    r[i].fi -= a[i];
    r[i].fi = max(r[i].fi, 1ll);
    assert(r[i].fi > 0 and r[i].se > 0);
  }

  ll ans = a[n];
  ll below = a[n];
  for (int i = n - 1; i >= 0; i--) {
    ll nl = min(r[i].se, below);
    below = nl + a[i];
    ans += below;
  }

  cout << ans << "\\n";
  return 0;
}