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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

const int L = 1e5 + 7;
int dp[1 << 10][L];

const int K = 9;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];
  vector<int> h(n);
  vector<int> par(n);
  function<void(int)> dfs = [&](int u) {
    if (h[u] <= K) {
      if (u == 0) {
        dp[u][w[u]] = v[u];
      } else {
        for (int i = 0; i < L; ++i) {
          dp[u][i] = dp[par[u]][i];
          if (i >= w[u]) {
            dp[u][i] = max(dp[u][i], dp[par[u]][i - w[u]] + v[u]);
          }
        }
      }
      for (int i = 1; i < L; ++i)
        dp[u][i] = max(dp[u][i], dp[u][i - 1]);
    }

    if (2 * u + 1 < n) {
      h[2 * u + 1] = h[u] + 1;
      par[2 * u + 1] = u;
      dfs(2 * u + 1);
    }
    if (2 * u + 2 < n) {
      h[2 * u + 2] = h[u] + 1;
      par[2 * u + 2] = u;
      dfs(2 * u + 2);
    }
  };
  dfs(0);
  par[0] = -1;
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int u, l;
    cin >> u >> l;
    --u;
    if (h[u] <= K) {
      cout << dp[u][l] << '\\n';
    } else {
      vector<int> who;
      while (h[u] > K) {
        who.push_back(u);
        u = par[u];
      }
      int ans = 0;
      function<void(int, int, int)> rec = [&](int pos, int sw, int sv) {
        if (pos == (int)who.size()) {
          if (sw <= l) {
            ans = max(ans, dp[u][l - sw] + sv);
          }
        } else {
          rec(pos + 1, sw, sv);
          rec(pos + 1, sw + w[who[pos]], sv + v[who[pos]]);
        }
      };
      rec(0, 0, 0);
      cout << ans << '\\n';
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

const int L = 1e5 + 7;
int dp[1 << 10][L];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];
  vector<int> h(n);
  vector<int> par(n);
  function<void(int)> dfs = [&](int u) {
    if (h[u] <= 8) {
      if (u == 0) {
        dp[u][w[u]] = v[u];
      } else {
        for (int i = 0; i < L; ++i) {
          dp[u][i] = dp[par[u]][i];
          if (i >= w[u]) {
            dp[u][i] = max(dp[u][i], dp[par[u]][i - w[u]] + v[u]);
          }
        }
      }
      for (int i = 1; i < L; ++i)
        dp[u][i] = max(dp[u][i], dp[u][i - 1]);
    }

    if (2 * u + 1 < n) {
      h[2 * u + 1] = h[u] + 1;
      par[2 * u + 1] = u;
      dfs(2 * u + 1);
    }
    if (2 * u + 2 < n) {
      h[2 * u + 2] = h[u] + 1;
      par[2 * u + 2] = u;
      dfs(2 * u + 2);
    }
  };
  dfs(0);
  par[0] = -1;
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int u, l;
    cin >> u >> l;
    --u;
    if (h[u] <= 8) {
      cout << dp[u][l] << '\\n';
    } else {
      vector<int> who;
      while (h[u] > 8) {
        who.push_back(u);
        u = par[u];
      }
      int ans = 0;
      for (int mask = 0; mask < (int)who.size(); ++i) {
        int sw = 0, sv = 0;
        for (int i = 0; i < (int)who.size(); ++i) {
          if (mask & (1 << i)) {
            sw += w[i];
            sv += v[i];
          }
        }
        if (sw <= l) {
          ans = max(ans, sv + dp[u][l - sw]);
        }
      }
      cout << ans << '\\n';
    }
  }
}