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

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
  if (p == 0 || n == 1) {
    return 1;
  } else {
    ll ret = pow2(n * n % mod, p / 2, mod);
    if (p % 2 == 1) {
      ret *= n;
    }
    return ret % mod;
  }
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  ll N;
  std::cin >> N;

  vll A(N + 1);
  repeq(i, 0, N) { std::cin >> A[i]; }

  vll sum(N + 2, 0);
  for (ll i = N; i >= 0; i--) {
    sum[i] = sum[i + 1] + A[i];
  }

  ll ans = 0;
  ll cnt = 1;
  repeq(i, 0, N) {

    if (cnt < A[i]) {
      std::cout << -1 << endl;
      return 0;
    }

    ans += cnt;
    cnt -= A[i];
    cnt = min(2 * cnt, sum[i + 1]);
  }
  std::cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
  if (p == 0 || n == 1) {
    return 1;
  } else {
    ll ret = pow2(n * n % mod, p / 2, mod);
    if (p % 2 == 1) {
      ret *= n;
    }
    return ret % mod;
  }
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  ll N;
  std::cin >> N;

  vll max_num(N + 1, 1);
  vll A(N + 1);
  std::cin >> A[0];
  repeq(i, 1, N) {
    std::cin >> A[i];
    max_num[i] = max_num[i - 1] * 2;
  }

  if (A[N] > max_num[N]) {
    std::cout << -1 << endl;
    return 0;
  }

  stack<tuple<ll, ll, ll>> st;
  st.push({N, A[N], A[N]});

  while (!st.empty()) {
    ll d = get<0>(st.top()) - 1;
    ll ch = get<1>(st.top());
    ll ans = get<2>(st.top());
    st.pop();
    if (d < 0) {
      std::cout << ans << endl;
      return 0;
    }
    for (ll i = ch / 2; i >= 0; i--) {
      ll tmp = ch - i + A[d];
      if (tmp <= max_num[d]) {
        st.push({d, tmp, ans + tmp});
      }
    }
  }

  throw "aaaa0";
  std::cout << -1 << endl;
  return 0;
}