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

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

#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
void dump() { cerr << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}

signed main() {

  int n;
  cin >> n;
  vector<ll> a(n + 1);
  cin >> a;

  if (a[0] == 1 && accumulate(begin(a) + 1, end(a), 0ll) == 0) {
    cout << 1 << endl;
    return 0;
  } else if (a[0] >= 1) {
    cout << "-1" << endl;
    return 0;
  }

  vector<ll> sum(n + 2);
  rep(i, n + 1) sum[i + 1] = sum[i] + a[i];

  ll ans = 1;
  ll base = 1;
  for (int i = 1; i <= n; ++i) {
    ll tmp = INF;
    if (a[i] > base * 2) {
      cout << "-1" << endl;
      return 0;
    }
    chmin(tmp, base * 2);
    chmin(tmp, sum.back() - sum[i]);
    dump(i, tmp, base, tmp - a[i]);
    base = tmp - a[i];
    ans += tmp;
  }
  cout << (ans) << endl;
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
void dump() { cerr << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}

signed main() {

  int n;
  cin >> n;
  assert(n != 1e5);
  vector<ll> a(n + 1);
  cin >> a;

  if (a[0] == 1 && accumulate(begin(a), end(a), 0ll) == 0) {
    cout << 1 << endl;
    return 0;
  }
  if (a[0] != 0 && accumulate(begin(a), end(a), 0ll) != 0) {
    cout << "-1" << endl;
    return 0;
  }

  vector<ll> sum(n + 2);
  rep(i, n + 1) sum[i + 1] = sum[i] + a[i];

  ll ans = 1;
  ll base = 1;
  for (int i = 1; i <= n; ++i) {
    ll tmp = INF;
    if (a[i] > base * 2) {
      cout << "-1" << endl;
      return 0;
    }
    chmin(tmp, base * 2);
    if (i != n)
      chmin(tmp, sum.back() - sum[i]);
    if (i == n)
      chmin(tmp, a[i]);
    dump(i, tmp, base, tmp - a[i]);
    base = tmp - a[i];
    ans += tmp;
  }
  cout << (ans) << endl;
}