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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

#define MAX 100000

ll dp[(1 << 9) + 1][2][MAX + 1];

signed main() {
  int n;
  cin >> n;
  int d = 0;
  while ((1 << d) <= n) {
    d++;
  }
  int V[n + 1], W[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> V[i] >> W[i];
  }
  int q;
  cin >> q;
  int v[q], l[q];
  rep(i, q) { cin >> v[i] >> l[i]; }
  int h = d / 2;
  int depth[1 << h];
  for (int i = 1; i < (1 << h); i++) {
    int j = 0;
    int cur = i;
    while (cur > 0) {
      for (int k = 0; k <= MAX; k++) {
        dp[i][(j + 1) % 2][k] = max(dp[i][(j + 1) % 2][k], dp[i][j % 2][k]);
        if (k - W[cur] < 0)
          continue;
        dp[i][(j + 1) % 2][k] =
            max(dp[i][(j + 1) % 2][k], dp[i][j % 2][k - W[cur]] + V[cur]);
      }
      j++;
      cur /= 2;
    }
    depth[i] = j % 2;
  }
  int vt[10], wt[10];
  rep(i, q) {
    if (v[i] < (1 << h)) {
      cout << dp[v[i]][depth[v[i]]][l[i]] << endl;
    } else {
      int cur = v[i];
      int sz = 0;
      while ((1 << h) <= cur) {
        vt[sz] = V[cur];
        wt[sz] = W[cur];
        cur /= 2;
        sz++;
      }
      ll ans = 0;
      for (int bit = 0; bit < (1 << sz); bit++) {
        ll V = 0, W = l[i];
        rep(i, sz) {
          if (bit & (1 << i)) {
            V += vt[i];
            W -= wt[i];
          }
        }
        if (W < 0)
          continue;
        ans = max(ans, dp[cur][h % 2][W] + V);
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

#define MAX 100000

ll dp[(1 << 9) + 1][2][MAX + 1];

signed main() {
  int n;
  cin >> n;
  int d = 0;
  while ((1 << d) <= n) {
    d++;
  }
  int V[n + 1], W[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> V[i] >> W[i];
  }
  int q;
  cin >> q;
  int v[q], l[q];
  rep(i, q) { cin >> v[i] >> l[i]; }
  int h = d / 2;
  int depth[1 << h];
  for (int i = 1; i < (1 << h); i++) {
    int j = 0;
    int cur = i;
    while (cur > 0) {
      for (int k = 0; k <= MAX; k++) {
        dp[i][(j + 1) % 2][k] = max(dp[i][(j + 1) % 2][k], dp[i][j % 2][k]);
        if (k - W[cur] < 0)
          continue;
        dp[i][(j + 1) % 2][k] =
            max(dp[i][(j + 1) % 2][k], dp[i][j % 2][k - W[cur]] + V[cur]);
      }
      j++;
      cur /= 2;
    }
    depth[i] = j % 2;
  }
  int vt[10], wt[10];
  rep(i, q) {
    if (v[i] < (1 << h)) {
      cout << dp[v[i]][depth[v[i]]][l[i]] << endl;
    } else {
      int cur = v[i];
      int sz = 0;
      while ((1 << h) <= cur) {
        vt[sz] = V[cur];
        wt[sz] = W[cur];
        cur /= 2;
        sz++;
      }
      ll ans = 0;
      for (int bit = 0; bit < (1 << sz); bit++) {
        ll V = 0, W = l[i];
        rep(i, sz) {
          if (bit & (1 << i)) {
            V += vt[i];
            W -= wt[i];
          }
        }
        if (W < 0)
          continue;
        ans = max(ans, dp[cur][h % 2][W] + V);
      }
      cout << ans << endl;
    }
  }
  return 0;
}