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

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
// typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

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;
}

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
  for (ll i = 0; i < h; i++) {
    cerr << v[i][0];
    for (ll j = 1; j < w; j++)
      cerr spa v[i][j];
    cerr << endl;
  }
};
template <typename T> void debug(vector<T> &v, ll n) {
  if (n != 0)
    cerr << v[0];
  for (ll i = 1; i < n; i++)
    cerr spa v[i];
  cerr << endl;
};

string num2bit(ll num, ll len) {
  string bit = "";
  REP(i, len) { bit += char('0' + (num >> i & 1)); }
  return bit;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  REP(i, N) cin >> V[i + 1] >> W[i + 1];

  const ll K = 9;
  const ll H = (1ll << K);
  const ll LMA = 100005;

  vector<vector<int>> dp(H, vector<int>(LMA, 0));
  REP(i, H) {
    REP(j, 2) {
      if (i == 0 and j == 0)
        continue;

      ll ni = 2 * i + j;
      if (ni >= H or ni > N)
        continue;
      REP(k, LMA) {
        chmax(dp[ni][k], dp[i][k]);
        if (k + W[ni] < LMA)
          chmax(dp[ni][k + W[ni]], dp[i][k] + V[ni]);
      }
    }
  }

  // debug(dp, 5, 10);

  ll Q;
  cin >> Q;
  while (Q--) {
    ll v, L;
    cin >> v >> L;

    vector<pll> reku;
    reku.push_back({0, 0});
    while (v >= H) {
      ll sz = reku.size();
      REP(i, sz) {
        pll y;
        y.first = reku[i].first + V[v];
        y.second = reku[i].second + W[v];
        reku.push_back(y);
      }
      v /= 2;
    }
    ll ma = 0;
    for (auto x : reku) {
      if (L - x.second >= 0)
        chmax(ma, x.first + dp[v][L - x.second]);
    }
    cout << ma << "\\n";
  }

  return 0;
}

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


Fixed submission:

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
// typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

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;
}

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
  for (ll i = 0; i < h; i++) {
    cerr << v[i][0];
    for (ll j = 1; j < w; j++)
      cerr spa v[i][j];
    cerr << endl;
  }
};
template <typename T> void debug(vector<T> &v, ll n) {
  if (n != 0)
    cerr << v[0];
  for (ll i = 1; i < n; i++)
    cerr spa v[i];
  cerr << endl;
};

string num2bit(ll num, ll len) {
  string bit = "";
  REP(i, len) { bit += char('0' + (num >> i & 1)); }
  return bit;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  REP(i, N) cin >> V[i + 1] >> W[i + 1];

  const ll K = 9;
  const ll H = (1ll << K);
  const ll LMA = 100005;

  vector<vector<int>> dp(H, vector<int>(LMA, 0));
  REP(i, H) {
    REP(j, 2) {
      if (i == 0 and j == 0)
        continue;

      ll ni = 2 * i + j;
      if (ni >= H or ni > N)
        continue;
      REP(k, LMA) {
        chmax(dp[ni][k], dp[i][k]);
        if (k + W[ni] < LMA)
          chmax(dp[ni][k + W[ni]], dp[i][k] + V[ni]);
      }
    }
  }

  // debug(dp, 5, 10);

  ll Q;
  cin >> Q;
  while (Q--) {
    ll v, L;
    cin >> v >> L;

    vector<pll> reku;
    reku.push_back({0, 0});
    while (v >= H) {
      ll sz = reku.size();
      REP(i, sz) {
        pll y;
        y.first = reku[i].first + V[v];
        y.second = reku[i].second + W[v];
        reku.push_back(y);
      }
      v /= 2;
    }
    ll ma = 0;
    for (auto x : reku) {
      chmax(ma, x.first + dp[v][L - x.second]);
    }
    cout << ma << "\\n";
  }

  return 0;
}