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

#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (100006)
ll n, A[MAXN], ans, num[MAXN], p2[MAXN];
int main() {
  FAST cin >> n;
  FOR(i, 0, n) cin >> A[i];
  auto no = [&]() {
    cout << "-1\\n";
    exit(0);
  };
  FOR(i, 0, min(40ll, n - 1)) if (A[i] >= (1ll << i)) no();
  if (n <= 40 && (1ll << n) < A[n])
    no();
  p2[0] = 1;
  FOR(i, 1, n - 1) p2[i] = min(ll(2e18), p2[i - 1] * 2);
  DEC(i, n - 1, 0) { num[i] = min(num[i + 1] + A[i + 1], p2[i] - A[i]); }
  if (n)
    assert(num[0] == 1);
  FOR(i, 1, n - 1) {
    num[i] = min(num[i - 1] * 2 - A[i], num[i]);
    if (num[i] < 0)
      no();
  }
  if (n && num[n - 1] * 2 < A[n])
    no();
  FOR(i, 0, n) ans += num[i] + A[i];
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (100006)
ll n, A[MAXN], ans, num[MAXN];
int main() {
  FAST cin >> n;
  assert(n <= 5);
  FOR(i, 0, n) cin >> A[i];
  auto no = [&]() {
    cout << "-1\\n";
    exit(0);
  };
  FOR(i, 0, min(40ll, n - 1)) if (A[i] >= (1ll << i)) no();
  if (n <= 40 && (1ll << n) < A[n])
    no();
  num[0] = 1;
  FOR(i, 1, n) {
    num[i] = num[i - 1] * 2, num[i] = min(num[i], ll(2e18));
    num[i] -= A[i];
    if (num[i] < 0) {
      no();
    }
  }
  DEC(i, n - 1, 0) num[i] = min(num[i], (num[i + 1] + A[i + 1] + 1) / 2);
  FOR(i, 0, n - 1) ans += num[i];
  FOR(i, 0, n) ans += A[i];
  ll left = num[n];
  DEC(i, n - 1, 0) {
    if (num[i] - ((num[i + 1] + A[i + 1]) & 1) >= left) {
      left = 0;
      break;
    } else {
      // i == 0?
      if (i == 0)
        no();
      left = (left - num[i]);
      if ((num[i + 1] + A[i + 1]) & 1)
        ++left;
      assert(left <= num[i]);
      ans -= left;
    }
  }
  cout << ans << '\\n';
}