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

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
using ll = long long;

int main() {
  int N, Q;
  cin >> N;
  vector<int> V(N), W(N);
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }
  cin >> Q;
  vector<int> v(Q), L(Q);
  for (int i = 0; i < Q; i++) {
    cin >> v[i] >> L[i];
    v[i]--;
  }

  vector<vector<int>> dp(min(N, (1 << 10) - 1), vector<int>(100001, 0));
  for (int i = 0; i < min(N, (1 << 10) - 1); i++) {
    if (i == 0) {
      for (int j = 100000; j - W.at(i) >= 0; j--) {
        dp[i][j] = max(dp[i][j], dp[i][j - W[i]] + V[i]);
      }
    } else {
      int x = (i - 1) / 2;
      for (int j = 0; j <= 100000; j++) {
        dp[i][j] = dp[x][j];
        if (j - W[i] >= 0) {
          dp[i][j] = max(dp[i][j], dp[x][j - W[i]] + V[i]);
        }
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    if (v[i] < ((1 << 10) - 1)) {
      cout << dp[v[i]][L[i]] << endl;
    } else {
      vector<int> pre;
      int x = v[i];
      while (x >= ((1 << 10) - 1)) {
        pre.push_back(x);
        x = (x - 1) / 2;
      }
      int ans = dp[x][L[i]];
      int n = pre.size();
      for (int j = 0; j < (1 << n); j++) {
        int value = 0;
        int weight = 0;
        for (int k = 0; k < n; k++) {
          if (((j >> k) & 1) == 1) {
            value += V[pre[k]];
            weight += W[pre[k]];
          }
          if (weight <= L[i]) {
            ans = max(ans, value + dp[x][L[i] - weight]);
          }
        }
      }
      cout << ans << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
using ll = long long;

int main() {
  int N, Q;
  cin >> N;
  vector<int> V(N), W(N);
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }
  cin >> Q;
  vector<int> v(Q), L(Q);
  for (int i = 0; i < Q; i++) {
    cin >> v[i] >> L[i];
    v[i]--;
  }

  vector<vector<int>> dp(min(N, (1 << 9) - 1), vector<int>(100001, 0));
  for (int i = 0; i < min(N, (1 << 9) - 1); i++) {
    if (i == 0) {
      for (int j = 100000; j - W.at(i) >= 0; j--) {
        dp[i][j] = max(dp[i][j], dp[i][j - W[i]] + V[i]);
      }
    } else {
      int x = (i - 1) / 2;
      for (int j = 0; j <= 100000; j++) {
        dp[i][j] = dp[x][j];
        if (j - W[i] >= 0) {
          dp[i][j] = max(dp[i][j], dp[x][j - W[i]] + V[i]);
        }
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    if (v[i] < ((1 << 9) - 1)) {
      cout << dp[v[i]][L[i]] << endl;
    } else {
      vector<int> pre;
      int x = v[i];
      while (x >= ((1 << 9) - 1)) {
        pre.push_back(x);
        x = (x - 1) / 2;
      }
      int ans = dp[x][L[i]];
      int n = pre.size();
      for (int j = 0; j < (1 << n); j++) {
        int value = 0;
        int weight = 0;
        for (int k = 0; k < n; k++) {
          if (((j >> k) & 1) == 1) {
            value += V[pre[k]];
            weight += W[pre[k]];
          }
          if (weight <= L[i]) {
            ans = max(ans, value + dp[x][L[i] - weight]);
          }
        }
      }
      cout << ans << endl;
    }
  }
}