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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
void chmax(int &a, int b) {
  if (a < b)
    a = b;
}

using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vvec<int> g(N);
  vec<int> dep(N);
  vec<int> par(N, -1);
  for (int i = 0; i < N; i++) {
    int a = 0;
    par[i] = (i - 1) / 2;
    for (int k = 0; k < 18; k++)
      if (i >= (1 << k) - 1)
        a = k;
    dep[i] = a;
    if (2 * i + 1 < N)
      g[i].push_back(2 * i + 1);
    if (2 * i + 2 < N)
      g[i].push_back(2 * i + 2);
  }
  vec<int> V(N), W(N);
  int H = 9;
  for (int i = 0; i < N; i++)
    cin >> V[i] >> W[i];
  int M = 1e5;
  int inf = 1e9;
  vvec<int> dp((1 << H) - 1, vec<int>(M + 1, -inf));
  dp[0][0] = 0;
  dp[0][W[0]] = V[0];
  auto dfs = [&](auto &&self, int cur) -> void {
    for (auto &to : g[cur]) {
      for (int i = 0; i <= M; i++) {
        chmax(dp[to][i], dp[cur][i]);
        if (i + W[to] <= M)
          chmax(dp[to][i + W[to]], dp[cur][i] + V[to]);
      }
      if (dep[to] + 1 < H)
        self(self, to);
    }
  };
  dfs(dfs, 0);
  for (int i = 0; i < (1 << H) - 1; i++)
    for (int j = 0; j < M; j++)
      chmax(dp[i][j + 1], dp[i][j]);
  auto calc = [&](int v, int l) {
    vec<P> A = {{0, 0}};
    int now = v;
    while (dep[now] >= H) {
      vec<P> ne;
      for (auto &x : A) {
        ne.push_back(x);
        if (x.first + W[now] <= l)
          ne.push_back({x.first + W[now], x.second + V[now]});
      }
      A.swap(ne);
      now = par[now];
    }
    int res = dp[now][l];
    for (auto &x : A) {
      res = max(res, x.second + dp[now][l - x.first]);
      res = max(res, x.second);
    }
    return res;
  };

  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    v--;
    if (dep[v] < H)
      cout << dp[v][L] << "\\n";
    else
      cout << calc(v, L) << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
void chmax(int &a, int b) {
  if (a < b)
    a = b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vvec<int> g(N);
  vec<int> dep(N);
  vec<int> par(N, -1);
  for (int i = 0; i < N; i++) {
    int a = 0;
    par[i] = (i - 1) / 2;
    for (int k = 0; k < 18; k++)
      if (i >= (1 << k) - 1)
        a = k;
    dep[i] = a;
    if (2 * i + 1 < N)
      g[i].push_back(2 * i + 1);
    if (2 * i + 2 < N)
      g[i].push_back(2 * i + 2);
  }
  vec<int> V(N), W(N);
  int H = 11;
  for (int i = 0; i < N; i++)
    cin >> V[i] >> W[i];
  int M = 1e5;
  int inf = 1e9;
  vvec<int> dp((1 << H) - 1, vec<int>(M + 1, -inf));
  dp[0][0] = 0;
  dp[0][W[0]] = V[0];
  auto dfs = [&](auto &&self, int cur) -> void {
    for (auto &to : g[cur]) {
      for (int i = 0; i <= M; i++) {
        chmax(dp[to][i], dp[cur][i]);
        if (i + W[to] <= M)
          chmax(dp[to][i + W[to]], dp[cur][i] + V[to]);
      }
      if (dep[to] + 1 < H)
        self(self, to);
    }
  };
  dfs(dfs, 0);
  for (int i = 0; i < (1 << H) - 1; i++)
    for (int j = 0; j < M; j++)
      chmax(dp[i][j + 1], dp[i][j]);
  auto calc = [&](int v, int l) {
    unordered_map<int, int> mp;
    int now = v;
    mp[0] = 0;
    while (dep[now] >= H) {
      unordered_map<int, int> nmp;
      for (auto &x : mp) {
        chmax(nmp[x.first], x.second);
        if (x.first + W[now] <= l)
          chmax(nmp[x.first + W[now]], x.second + V[now]);
      }
      mp.swap(nmp);
      now = par[now];
    }
    int res = dp[now][l];
    for (auto &x : mp) {
      res = max(res, x.second + dp[now][l - x.first]);
      res = max(res, x.second);
    }
    return res;
  };

  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    v--;
    if (dep[v] < H)
      cout << dp[v][L] << "\\n";
    else
      cout << calc(v, L) << "\\n";
  }
}