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

#include <bits/stdc++.h>

using namespace std;

inline int ctoi(char c) {
  if (c < '0' || '9' < c)
    throw invalid_argument("ctoi error");
  return c - '0';
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

template <typename T> inline T gcd(T x, T y) {
  if (x <= 0 || y <= 0)
    throw invalid_argument("gcd error: x <= 0 or y <= 0");

  if (x < y)
    swap(x, y);
  T r = x % y;

  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }

  return y;
}
template <typename T> inline T lcm(T x, T y) {
  if (x <= 0 || y <= 0)
    throw invalid_argument("lcm error: x <= 0 or y <= 0");

  return x * y / gcd(x, y);
}

int main() {
  int n;
  cin >> n;

  vector<int> input;
  vector<pair<long long int, long long int>> tree(n + 1);

  rep(i, n + 1) {
    int a;
    cin >> a;
    input.push_back(a);
  }

  tree[n] = make_pair(input[n], input[n]);

  for (int i = n - 1; 0 <= i; i--) {
    if (input[i] == 0) {
      tree[i] = make_pair((tree[i + 1].first + 1) / 2, tree[i + 1].second);
    } else {
      tree[i] = make_pair((tree[i + 1].first + 1) / 2 + input[i],
                          tree[i + 1].second + input[i]);
    }
  }
  /*
        rep(i, n + 1){
                cout << i << " : " << tree[i].first << " , " << tree[i].second
     << endl;
        }
        */

  if (1 < tree[0].first || tree[0].second < 1) {
    cout << -1;
    return 0;
  }

  if (tree[0].second != 1) {
    tree[0].second = 1;

    REP(i, 1, n) {
      long long int t = tree[i - 1].second - input[i - 1];

      if (t * 2 < tree[i].second) {
        tree[i].second = t * 2;
      }
    }
  }
  /*	cout << endl;
        rep(i, n + 1){
                cout << i << " : " << tree[i].first << " , " << tree[i].second
     << endl;
        }
        */

  long long int ans = 0;
  rep(i, n + 1) { ans += tree[i].second; }

  cout << ans;

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bits/stdc++.h> // isupper, islower, isdigit, toupper, tolower
#include <bitset>        // bitset
#include <cmath>         // sqrt
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#include <iostream>      // cout, endl, cin
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> Pl;
typedef pair<int, int> P;
typedef tuple<ll, ll, string> Tlls;

ll N;
ll ans = 0;
ll MAX = 100000000;
vector<ll> A(MAX);
vector<ll> B(MAX);   // 使えるノード
vector<ll> ANS(MAX); // 　すべての頂点数
bool dfs(ll n) {

  if (n == 0) {
    if (A.at(0) == 0) {
      ANS.at(n) = 1;
      B.at(n) = 1;
      return true;
    } else {
      return false;
    }
  }
  if (ANS.at(N) % 2 != 0) {
    return false;
  } else {
    B.at(n) = B.at(n + 1) / 2 + A.at(n);
    ANS.at(n) = B.at(n + 1) / 2 + A.at(n);
    // cout << ANS.at(n) << " " << B.at(n) << endl;
  }

  if (A.at(n) > pow(2, n)) {
    return false;
  }
  if (n >= 1 && B.at(n) * 2 < A.at(n + 1)) {
    return false;
  }
  dfs(n - 1);
  return true;
}

int main() {
  cin >> N;
  A.resize(N + 1);
  B.resize(N + 1);
  ANS.resize(N + 1);
  rep(i, N + 1) { cin >> A.at(i); }
  B.at(N) = A.at(N);
  ANS.at(N) = A.at(N);
  if (dfs(N - 1)) {
    rep(i, N + 1) {
      ans += ANS.at(i);
      // cout << ANS.at(i) << " " << B.at(i) << endl;
    }
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}