Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<int, ll>;

constexpr char newl = '\\n';
constexpr int MAXL = 100000;

array<ll, MAXL + 1> dp;
vector<P> cand = {{0, 0}};

void solve(int cur, int depth, int n, const vector<int> &v,
           const vector<int> &w, const vector<vector<int>> &qs,
           const vector<int> &L, vector<ll> &ans) {
  if (depth < 9) {
    array<ll, MAXL + 1> tmp_dp = dp;
    for (int i = MAXL; i >= w[cur]; i--) {
      dp[i] = max(dp[i], dp[i - w[cur]] + v[cur]);
    }

    for (int i : qs[cur]) {
      ans[i] = dp[L[i]];
    }

    if (2 * cur + 1 < n)
      solve(2 * cur + 1, depth + 1, n, v, w, qs, L, ans);
    if (2 * cur + 2 < n)
      solve(2 * cur + 2, depth + 1, n, v, w, qs, L, ans);
    dp = move(tmp_dp);
  } else {
    int hoge = 1 << (depth - 9);
    for (int i = 0; i < hoge; i++) {
      cand.emplace_back(cand[i].first + w[cur], cand[i].second + v[cur]);
    }

    for (P &p : cand) {
      for (int j : qs[cur]) {
        if (p.first > L[j])
          continue;
        ans[j] = max(ans[j], dp[L[j] - p.first] + p.second);
      }
    }

    if (2 * cur + 1 < n)
      solve(2 * cur + 1, depth + 1, n, v, w, qs, L, ans);
    if (2 * cur + 2 < n)
      solve(2 * cur + 2, depth + 1, n, v, w, qs, L, ans);

    for (int i = 0; i < hoge; i++) {
      cand.pop_back();
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }

  int q;
  cin >> q;

  vector<int> L(q);
  vector<vector<int>> qs(n);
  for (int i = 0; i < q; i++) {
    int u;
    cin >> u >> L[i];
    --u;

    qs[u].push_back(i);
  }

  vector<ll> ans(q, 0);
  solve(0, 0, n, v, w, qs, L, ans);

  for (int i = 0; i < q; i++) {
    cout << ans[i] << newl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\\n';
constexpr int MAXL = 100000;

array<ll, MAXL + 1> dp;
vector<int> cand;

void solve(int cur, int depth, int n, const vector<int> &v,
           const vector<int> &w, const vector<vector<int>> &qs,
           const vector<int> &L, vector<ll> &ans) {
  if (depth < 7) {
    array<ll, MAXL + 1> tmp_dp = dp;
    for (int i = MAXL; i >= w[cur]; i--) {
      dp[i] = max(dp[i], dp[i - w[cur]] + v[cur]);
    }

    for (int i : qs[cur]) {
      ans[i] = dp[L[i]];
    }

    if (2 * cur + 1 < n)
      solve(2 * cur + 1, depth + 1, n, v, w, qs, L, ans);
    if (2 * cur + 2 < n)
      solve(2 * cur + 2, depth + 1, n, v, w, qs, L, ans);
    dp = move(tmp_dp);
  } else {
    cand.push_back(cur);

    int sz = cand.size();
    for (int i = 0; i < (1 << sz); i++) {
      int W = 0;
      ll V = 0;
      for (int j = 0; j < sz; j++) {
        if (i >> j & 1) {
          W += w[cand[j]];
          V += v[cand[j]];
        }
      }

      for (int j : qs[cur]) {
        if (W > L[j])
          continue;
        ans[j] = max(ans[j], dp[L[j] - W] + V);
      }
    }

    if (2 * cur + 1 < n)
      solve(2 * cur + 1, depth + 1, n, v, w, qs, L, ans);
    if (2 * cur + 2 < n)
      solve(2 * cur + 2, depth + 1, n, v, w, qs, L, ans);
    cand.pop_back();
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }

  int q;
  cin >> q;

  vector<int> L(q);
  vector<vector<int>> qs(n);
  for (int i = 0; i < q; i++) {
    int u;
    cin >> u >> L[i];
    --u;

    qs[u].push_back(i);
  }

  vector<ll> ans(q, 0);
  solve(0, 0, n, v, w, qs, L, ans);

  for (int i = 0; i < q; i++) {
    cout << ans[i] << newl;
  }
  return 0;
}