Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10, mod = 1e9 + 7, M = 1e5;

vector<pii> Q[N], vals, vals2;
int w[N], val[N], n;
ll dp[M + 10], ans[N];

void dfs(int v, int h) {
  int num = 0;
  if (h < 9) {
    int n = vals.size();
    vals.push_back({w[v], val[v]});
    for (int i = 0; i < n; i++)
      vals.push_back({vals[i].first + w[v], vals[i].second + val[v]});
    num = n + 1;
  } else {
    int n = vals2.size();
    vals2.push_back({w[v], val[v]});
    for (int i = 0; i < n; i++)
      vals2.push_back({vals2[i].first + w[v], vals2[i].second + val[v]});
    num = n + 1;
  }
  if (h == 8) {
    vector<pii> cpy;
    cpy = vals;
    sort(cpy.begin(), cpy.end());
    cpy.resize(unique(cpy.begin(), cpy.end()) - cpy.begin());
    int lst = 0;
    int cur = 0;
    for (int i = 0; i <= M; i++) {
      while (cur < cpy.size() && cpy[cur].first == i) {
        lst = max(lst, cpy[cur].second);
        cur++;
      }
      dp[i] = lst;
    }
  }
  if (h < 9) {
    for (pii x : Q[v])
      for (pii y : vals)
        if (y.first <= x.first)
          ans[x.second] = max(ans[x.second], 1ll * y.second);
  } else {
    for (pii x : Q[v]) {
      ans[x.second] = dp[x.first];
      for (pii y : vals2)
        if (y.first <= x.first)
          ans[x.second] = max(ans[x.second], y.second + dp[x.first - y.first]);
    }
  }
  if (2 * v <= n)
    dfs(2 * v, h + 1);
  if (2 * v + 1 <= n)
    dfs(2 * v + 1, h + 1);
  if (h < 9) {
    while (num) {
      num--;
      vals.pop_back();
    }
  } else {
    while (num) {
      num--;
      vals2.pop_back();
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> val[i] >> w[i];
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    Q[v].push_back({l, i});
  }
  dfs(1, 0);
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\n";
  return 0;
}


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

typedef long long ll;
typedef pair<int, int> pii;

const int N = 3e5 + 10, mod = 1e9 + 7, M = 1e5;

vector<pii> Q[N], vals, vals2;
int w[N], val[N], n;
ll dp[M + 10], ans[N];

void dfs(int v, int h) {
  int num = 0;
  if (h < 9) {
    int n = vals.size();
    vals.push_back({w[v], val[v]});
    for (int i = 0; i < n; i++)
      vals.push_back({vals[i].first + w[v], vals[i].second + val[v]});
    num = n + 1;
  } else {
    int n = vals2.size();
    vals2.push_back({w[v], val[v]});
    for (int i = 0; i < n; i++)
      vals2.push_back({vals2[i].first + w[v], vals2[i].second + val[v]});
    num = n + 1;
  }
  if (h == 8) {
    vector<pii> cpy;
    cpy = vals;
    sort(cpy.begin(), cpy.end());
    cpy.resize(unique(cpy.begin(), cpy.end()) - cpy.begin());
    int lst = 0;
    int cur = 0;
    for (int i = 0; i <= M; i++) {
      while (cur < cpy.size() && cpy[cur].first == i) {
        lst = max(lst, cpy[cur].second);
        cur++;
      }
      dp[i] = lst;
    }
  }
  if (h < 9) {
    for (pii x : Q[v])
      for (pii y : vals)
        if (y.first <= x.first)
          ans[x.second] = max(ans[x.second], 1ll * y.second);
  } else {
    for (pii x : Q[v]) {
      ans[x.second] = dp[x.first];
      for (pii y : vals2)
        if (y.first <= x.first)
          ans[x.second] = max(ans[x.second], y.second + dp[x.first - y.first]);
    }
  }
  if (2 * v <= n)
    dfs(2 * v, h + 1);
  if (2 * v + 1 <= n)
    dfs(2 * v + 1, h + 1);
  if (h < 9) {
    while (num) {
      num--;
      vals.pop_back();
    }
  } else {
    while (num) {
      num--;
      vals2.pop_back();
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> val[i] >> w[i];
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    Q[v].push_back({l, i});
  }
  dfs(1, 0);
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\n";
  return 0;
}