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

#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
// ll const mod=998244353;
ll const mod = 1e9 + 7;
const ll MAX = 300000;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const ll LINF = 1e18 + 1;

vi v, w;
vvl dp;
//////////////////////////////////////

void get_dp() {
  dp[0][0] = 0;
  arep(i, 1, 1030) {
    rep(j, 100003) {
      dp[i][j] = dp[i / 2][j];
      if (j >= w[i])
        chmax(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
    }
  }
}

ll rec(int c, int L) {
  if (L < 0)
    return -LINF;
  if (c <= 1024)
    return dp[c][L];
  return max(rec(c / 2, L), rec(c / 2, L - w[c]) + v[c]);
}

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

  v.resize(max(n + 1, 1030), 0);
  w.resize(max(n + 1, 1030), 0);
  dp.resize(1030, vl(100005, 0));

  rep(i, n) { cin >> v[i + 1] >> w[i + 1]; }
  get_dp();
  vl ans;
  int q;
  cin >> q;
  rep(i, q) {
    int c, L;
    cin >> c >> L;
    ans.push_back(rec(c, L));
  }
  rep(i, ans.size()) cout << ans[i] << endl;
  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;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
// ll const mod=998244353;
ll const mod = 1e9 + 7;
const ll MAX = 300000;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vi v, w;
vvl dp;
//////////////////////////////////////

void get_dp() {
  dp[0][0] = 0;
  arep(i, 1, 1024) {
    rep(j, 100003) {
      dp[i][j] = dp[i / 2][j];
      if (j >= w[i])
        chmax(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
    }
  }
}

ll rec(int c, int L) {
  if (L < 0)
    return -INF;
  if (c <= 1024)
    return dp[c][L];
  return max(rec(c / 2, L), rec(c / 2, L - w[c]) + v[c]);
}

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

  v.resize(n + 1, 0);
  w.resize(n + 1, 0);
  dp.resize(1030, vl(100005, 0));

  rep(i, n) { cin >> v[i + 1] >> w[i + 1]; }
  get_dp();
  vl ans;
  int q;
  cin >> q;
  rep(i, q) {
    int c, L;
    cin >> c >> L;
    ans.push_back(rec(c, L));
  }
  rep(i, ans.size()) cout << ans[i] << endl;
  return 0;
}