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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
#pragma GCC optimize("Ofast")
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  int n;
  cin >> n;
  VI v(1010101, 0), w(1010101, 0);
  for (i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  int q;
  cin >> q;
  int m = 1 << 9;
  int ml = 100000;
  VVI r(m, VI(100001, 0));
  for (int l = m; l < m * 2; l++) {
    int u = l;
    VI av, aw;
    while (1) {
      av.pb(v[u]);
      aw.pb(w[u]);
      if (u == 1)
        break;
      u = u >> 1;
    }
    int y = av.size();
    for (i = 0; i < (1 << y); i++) {
      ll nv = 0, nw = 0;
      for (j = 0; j < y; j++) {
        if ((i >> j) & 1) {
          nv += av[j];
          nw += aw[j];
        }
      }
      if (nw <= ml)
        chmax(r[l - m][nw], nv);
    }
    ll h = 0;
    for (i = 0; i <= ml; i++) {
      chmax(h, r[l - m][i]);
      r[l - m][i] = h;
    }
  }
  while (q--) {
    int u, l;
    cin >> u >> l;
    if (u < m) {
      VI av, aw;
      while (1) {
        av.pb(v[u]);
        aw.pb(w[u]);
        if (u == 1)
          break;
        u = u >> 1;
      }
      int y = av.size();
      ll ans = 0;
      for (i = 0; i < (1 << y); i++) {
        ll nv = 0, nw = 0;
        for (j = 0; j < y; j++) {
          if ((i >> j) & 1) {
            nv += av[j];
            nw += aw[j];
          }
        }
        if (nw <= l)
          chmax(ans, nv);
      }
      cout << ans << endl;
    } else {
      VI av, aw;
      while (1) {
        if (u < 2 * m)
          break;
        av.pb(v[u]);
        aw.pb(w[u]);
        u = u >> 1;
      }
      int y = av.size();
      ll ans = 0;
      for (i = 0; i < (1 << y); i++) {
        ll nv = 0, nw = 0;
        for (j = 0; j < y; j++) {
          if ((i >> j) & 1) {
            nv += av[j];
            nw += aw[j];
          }
        }
        if (nw <= l)
          chmax(ans, nv + r[u - m][l - nw]);
      }
      cout << ans << endl;
    }
  }
}

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 long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  int n;
  cin >> n;
  VI v(1010101, 0), w(1010101, 0);
  for (i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  int q;
  cin >> q;
  int m = 1 << 9;
  VVI r(m, VI(100001, 0));
  for (int l = m; l < m * 2; l++) {
    int u = l;
    VI av, aw;
    while (1) {
      av.pb(v[u]);
      aw.pb(w[u]);
      if (u == 1)
        break;
      u = u >> 1;
    }
    int y = av.size();
    for (i = 0; i < (1 << y); i++) {
      ll nv = 0, nw = 0;
      for (j = 0; j < y; j++) {
        if ((i >> j) & 1) {
          nv += av[j];
          nw += aw[j];
        }
      }
      if (nw <= 100000)
        chmax(r[l - m][nw], nv);
    }
    ll h = 0;
    for (i = 0; i <= 100000; i++) {
      chmax(h, r[l - m][i]);
      r[l - m][i] = h;
    }
  }
  while (q--) {
    int u, l;
    cin >> u >> l;
    if (u < m) {
      VI av, aw;
      while (1) {
        av.pb(v[u]);
        aw.pb(w[u]);
        if (u == 1)
          break;
        u = u >> 1;
      }
      int y = av.size();
      ll ans = 0;
      for (i = 0; i < (1 << y); i++) {
        ll nv = 0, nw = 0;
        for (j = 0; j < y; j++) {
          if ((i >> j) & 1) {
            nv += av[j];
            nw += aw[j];
          }
        }
        if (nw <= l)
          chmax(ans, nv);
      }
      cout << ans << endl;
    } else {
      VI av, aw;
      while (1) {
        av.pb(v[u]);
        aw.pb(w[u]);
        if ((u >> 1) < m)
          break;
        u = u >> 1;
      }
      int y = av.size();
      ll ans = 0;
      for (i = 0; i < (1 << y); i++) {
        ll nv = 0, nw = 0;
        for (j = 0; j < y; j++) {
          if ((i >> j) & 1) {
            nv += av[j];
            nw += aw[j];
          }
        }
        chmax(ans, nv + r[u - m][l - nw]);
      }
      cout << ans << endl;
    }
  }
}