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

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define print(x) cout << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int main() {
  int n;
  cin >> n;
  vl a(n + 1, 0), b(n + 2, 0);
  rep(i, n + 1) cin >> a[i];
  for (int i = n; i >= 0; i--)
    b[i] = b[i + 1] + a[i];
  if (a[0] == 1) {
    if (n == 0) {
      cout << 1 << endl;
    } else
      cout << -1 << endl;
    return 0;
  }
  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = 1, now = 1;
  for (int i = 1; i <= n; i++) {
    now = (now - a[i - 1]) * 2;
    if (now < 0 || now < a[i]) {
      cout << -1 << endl;
      return 0;
    }
    mins(now, b[i]);
    ans += now;
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define print(x) cout << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int main() {
  int n;
  cin >> n;
  if (n > 50000) {
    return -1;
  }
  vl a(n + 1, 0), b(n + 1, 0);
  rep(i, n + 1) cin >> a[i];
  if (a[0] == 1) {
    if (n == 0) {
      cout << 1 << endl;
    } else
      cout << -1 << endl;
    return 0;
  }
  b[0] = 1LL;
  rep(i, n) {
    b[i + 1] = 2LL * (b[i] - a[i]);
    if (b[i + 1] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll ans = 0LL;
  ll now = -1LL;
  for (int i = n; i >= 0; i--) {
    if (i < 100) {
      ll x = (1LL) << i;
      if (a[i] > x) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (now == -1LL) {
      now = a[i];
    } else {
      now = min(b[i], now + a[i]);
    }
    ans += now;
  }
  cout << ans << endl;
  return 0;
}