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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<vector<ll>> p(n + 1, vector<ll>(2));
  for (ll i = 0; i < n; ++i) {
    ll v, w;
    scanf("%lld%lld", &v, &w);
    p[i + 1][0] = v;
    p[i + 1][1] = w;
  }
  int k = 1;
  while (n > ((1 << k) - 1))
    k++;
  k = min(k, 10);
  int K = 1 << k;
  vector<vector<ll>> dp(K, vector<ll>(100001, 0));
  for (int i = p[1][1]; i < 100001; ++i)
    dp[1][i] = p[1][0];
  for (int i = 2; i < min(n + 1, K); ++i) {
    for (int j = 0; j < 100001; ++j) {
      if (j >= p[i][1])
        dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - p[i][1]] + p[i][0]);
      else
        dp[i][j] = dp[i / 2][j];
    }
    // for (ll j = 0; j < 10; ++j) cout << dp[i][j];
    // cout << "\\n";
  }
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; ++q) {
    int x, l;
    scanf("%d%d", &x, &l);
    if (x < K) {
      printf("%lld\\n", dp[x][l]);
      continue;
    }
    int par = x;
    int cnt = 0;
    vector<int> pars(10);
    while (par >= K) {
      // cout << par << " ";
      pars[cnt] = par;
      par /= 2;
      cnt++;
    }
    // cout << par << "\\n";
    // cout << "\\n";
    ll ans = dp[par][l];
    vector<vector<ll>> dp2(1 << cnt, vector<ll>(2, 0));
    for (int i = 1; i < (1 << cnt); ++i) {
      int lsb = i & (-i);
      int lsb_ind = 0;
      while ((1 << lsb_ind) < lsb)
        lsb_ind++;
      dp2[i][0] = dp2[i - lsb][0] + p[pars[lsb_ind]][0];
      dp2[i][1] = dp2[i - lsb][1] + p[pars[lsb_ind]][1];
      if (dp2[i][1] <= l) {
        // cout << i << " " << dp2[i][0] << " " << dp2[i][1] << "\\n";
        ans = max(ans, dp2[i][0] + dp[par][l - dp2[i][1]]);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<vector<ll>> p(n + 1, vector<ll>(2));
  for (ll i = 0; i < n; ++i) {
    ll v, w;
    scanf("%lld%lld", &v, &w);
    p[i + 1][0] = v;
    p[i + 1][1] = w;
  }
  int k = 1;
  while (n > ((1 << k) - 1))
    k++;
  k = min(k, 10);
  int K = 1 << k;
  vector<vector<ll>> dp(K, vector<ll>(100001, 0));
  for (int i = p[1][1]; i < 100001; ++i)
    dp[1][i] = p[1][0];
  for (int i = 2; i < min(n + 1, K); ++i) {
    for (int j = 0; j < 100001; ++j) {
      if (j >= p[i][1])
        dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - p[i][1]] + p[i][0]);
      else
        dp[i][j] = dp[i / 2][j];
    }
    // for (ll j = 0; j < 10; ++j) cout << dp[i][j];
    // cout << "\\n";
  }
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; ++q) {
    int x, l;
    scanf("%d%d", &x, &l);
    if (x < K) {
      printf("%lld\\n", dp[x][l]);
      continue;
    }
    int par = x;
    int cnt = 0;
    unordered_map<ll, ll> pars;
    while (par >= K) {
      // cout << par << " ";
      pars[(1 << cnt)] = par;
      par /= 2;
      cnt++;
    }
    // cout << par << "\\n";
    // cout << "\\n";
    ll ans = dp[par][l];
    vector<vector<ll>> dp2(1 << cnt, vector<ll>(2, 0));
    for (int i = 1; i < (1 << cnt); ++i) {
      int lsb = i & (-i);
      dp2[i][0] = dp2[i - lsb][0] + p[pars[lsb]][0];
      dp2[i][1] = dp2[i - lsb][1] + p[pars[lsb]][1];
      if (dp2[i][1] <= l) {
        // cout << i << " " << dp2[i][0] << " " << dp2[i][1] << "\\n";
        ans = max(ans, dp2[i][0] + dp[par][l - dp2[i][1]]);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}