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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)

#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく

#define PB push_back // vectorヘの挿入
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

ll n;
vector<pair<ll, ll>> vw;
vector<vector<ll>> dp;
ll c1, c2;

void dfs(ll i) {
  if (i == 1) {
    dp[i - 1][vw[i - 1].S] = vw[i - 1].F;
  } else {
    ll j = i / 2;
    FORD(k, c2, 0) {
      if (dp[j - 1][k] != 0 or k == 0) {
        ll ne = k + vw[i - 1].S;
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1].F, dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= c1 and i * 2 <= n)
    dfs(i * 2);
  if (i * 2 + 1 <= c1 and i * 2 + 1 <= n)
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  vw.resize(n);
  REP(i, n) cin >> vw[i].F >> vw[i].S;
  c1 = 1 << 10;
  c2 = 100000;
  dp.resize(c1);
  REP(i, c1) dp[i].resize(c2 + 1);
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  ll q;
  cin >> q;
#if 1
  REP(i, q) {
    ll v, l;
    cin >> v >> l;
    vector<ll> cand;
    while (v > c1) {
      cand.PB(v);
      v = v / 2;
    }
    ll r = SIZE(cand);
    ll ans = 0;
    REP(j, 1 << r) {
      ll v_sub = 0;
      ll w_sub = 0;
      REP(k, r) {
        if ((j >> k) & 1) {
          v_sub += vw[cand[k] - 1].first;
          w_sub += vw[cand[k] - 1].second;
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
#endif
}


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


Fixed submission:

#include <bits/stdc++.h>
#define R(i, n) for (ll i; i < n; i++)
using namespace std;
typedef long long ll;
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define SIZE(x) ((ll)(x).size())
#define PB push_back
#define F first
#define S second

ll n;
vector<vector<ll>> vw;
vector<vector<ll>> dp;
ll c1, c2;

void dfs(ll i) {
  if (i == 1) {
    dp[i - 1][vw[i - 1][1]] = vw[i - 1][0];
  } else {
    ll j = i / 2;
    FORD(k, c2, 0) {
      if (dp[j - 1][k] != 0 or k == 0) {
        ll ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= c1 and i * 2 <= n)
    dfs(i * 2);
  if (i * 2 + 1 <= c1 and i * 2 + 1 <= n)
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  vw.resize(n);
  R(i, n) cin >> vw[i][0] >> vw[i][1];
  c1 = 1 << 10;
  c2 = 100000;
  dp.resize(c1);
  R(i, c1) dp[i].resize(c2 + 1);
  dfs(1); // cout << 1 << endl;
  R(i, c1) R(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  ll q;
  cin >> q;
  R(i, q) {
    ll v, l;
    cin >> v >> l;
    vector<ll> cand;
    while (v > c1) {
      cand.PB(v);
      v = v / 2;
    }
    ll r = SIZE(cand); // cout << r << endl;
    ll ans = 0;
    R(j, 1 << r) {
      ll v_sub = 0;
      ll w_sub = 0;
      R(k, r) {
        if ((j >> k) & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}