Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
const int L = 101010;
int n, q;
vector<vector<pair<int, int>>> query;
vector<int> ans, v, w;
vector<vector<int>> dp;
vector<int> vals, weights;
void dfs(int cur, int d) {
  if (cur > n)
    return;
  if (d <= 8) {
    vector<int> nxt(L);
    for (int i = 0; i < L; i++) {
      if (i - w[cur] >= 0)
        nxt[i] = max(nxt[i], dp.back()[i - w[cur]] + v[cur]);
      nxt[i] = max(nxt[i], dp.back()[i]);
      if (i > 0)
        nxt[i] = max(nxt[i], nxt[i - 1]);
    }
    dp.push_back(nxt);
    for (auto &p : query[cur]) {
      auto &id = p.second;
      auto &l = p.first;
      ans[id] = max(ans[id], dp.back()[l]);
    }
    dfs(cur * 2 + 1, d + 1);
    dfs(cur * 2, d + 1);
    dp.pop_back();
  } else {
    vals.push_back(v[cur]);
    weights.push_back(w[cur]);
    int k = vals.size();
    for (auto &p : query[cur]) {
      auto &id = p.second;
      auto &l = p.first;
      for (int mask = 0; mask < (1 << k); mask++) {
        int ww = 0;
        int vv = 0;
        for (int i = 0; i < k; i++)
          if ((1 << i) & mask) {
            ww += weights[i];
            vv += vals[i];
          }
        int rem = l - ww;
        if (rem < 0)
          continue;
        ans[id] = max(ans[id], dp.back()[rem] + vv);
      }
    }
    dfs(cur * 2 + 1, d + 1);
    dfs(cur * 2, d + 1);
    vals.pop_back();
    weights.pop_back();
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  query = vector<vector<pair<int, int>>>(n + 1);
  v = vector<int>(n + 1);
  w = v;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  cin >> q;
  ans = vector<int>(q + 1);
  dp.push_back(vector<int>(L));
  for (int i = 1; i <= q; i++) {
    int v, l;
    cin >> v >> l;
    query[v].emplace_back(l, i);
  }
  dfs(1, 1);
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
const int L = 101010;
int n, q;
vector<vector<pair<int, int>>> query;
vector<int> ans, v, w;
vector<vector<int>> dp;
vector<int> vals, weights;
void dfs(int cur, int d) {
  if (cur > n)
    return;
  if (d <= 9) {
    vector<int> nxt(L);
    for (int i = 0; i < L; i++) {
      if (i - w[cur] >= 0)
        nxt[i] = max(nxt[i], dp.back()[i - w[cur]] + v[cur]);
      nxt[i] = max(nxt[i], dp.back()[i]);
      if (i > 0)
        nxt[i] = max(nxt[i], nxt[i - 1]);
    }
    dp.push_back(nxt);
    for (auto &p : query[cur]) {
      auto &id = p.second;
      auto &l = p.first;
      ans[id] = max(ans[id], dp.back()[l]);
    }
    dfs(cur * 2 + 1, d + 1);
    dfs(cur * 2, d + 1);
    dp.pop_back();
  } else {
    vals.push_back(v[cur]);
    weights.push_back(w[cur]);
    int k = vals.size();
    for (auto &p : query[cur]) {
      auto &id = p.second;
      auto &l = p.first;
      for (int mask = 0; mask < (1 << k); mask++) {
        int ww = 0;
        int vv = 0;
        for (int i = 0; i < k; i++)
          if ((1 << i) & mask) {
            ww += weights[i];
            vv += vals[i];
          }
        int rem = l - ww;
        if (rem < 0)
          continue;
        ans[id] = max(ans[id], dp.back()[rem] + vv);
      }
    }
    dfs(cur * 2 + 1, d + 1);
    dfs(cur * 2, d + 1);
    vals.pop_back();
    weights.pop_back();
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  query = vector<vector<pair<int, int>>>(n + 1);
  v = vector<int>(n + 1);
  w = v;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  cin >> q;
  ans = vector<int>(q + 1);
  dp.push_back(vector<int>(L));
  for (int i = 1; i <= q; i++) {
    int v, l;
    cin >> v >> l;
    query[v].emplace_back(l, i);
  }
  dfs(1, 1);
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}