Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  REP(i, N + 1) cin >> A[i];
  if (N == 0) {
    if (A[0] == 1) {
      cout << 1 << "\n";
    } else {
      cout << -1 << "\n";
    }
  } else {
    bool able = 1;
    ll ablecount = 1;
    REP(i, N + 1) {
      ablecount -= A[i];
      if (ablecount < 0) {
        able = 0;
        break;
      }
      ablecount *= 2;
      if (ablecount > LLONG_MAX / 20) {
        able = 1;
        break;
      }
    }
    if (able) {
      return -1;
    } else {
      cout << -1 << "\n";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  REP(i, N + 1) cin >> A[i];
  if (N == 0) {
    if (A[0] == 1) {
      cout << 1 << "\n";
    } else {
      cout << -1 << "\n";
    }
  } else {
    bool able = 1;
    ll ablecount = 1;
    REP(i, N + 1) {
      ablecount -= A[i];
      if (ablecount < 0) {
        able = 0;
        break;
      }
      ablecount *= 2;
      if (ablecount > LLONG_MAX / 20) {
        able = 1;
        break;
      }
    }
    if (able) {
      ll maxi = 0;
      vector<ll> nowwant(N + 1);
      RREP(i, N + 1) {
        if (i != N) {
          nowwant[i] = nowwant[i + 1] + A[i];
        } else {
          nowwant[N] = A[i];
        }
        maxi += nowwant[i];
      }
      ll able = 1;
      REP(i, N + 1) {
        if (nowwant[i] - able >= 0) {
          maxi -= nowwant[i] - able;
          able -= A[i];
          able *= 2;
        } else {
          break;
        }
      }
      cout << maxi << "\n";
    } else {
      cout << -1 << "\n";
    }
  }
}