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

#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = int_fast64_t;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

int n;
vector<int> V, W;
vector<vector<int>> dp(550, vector<int>(100010, 0));
void init_dp() {
  int limit = min(n, (1 << 9) - 1);
  rep(i, limit) {
    int v = i + 1;
    rep(j, 100005) {
      dp[v][j] = dp[v / 2][j];
      if (W[i] <= j)
        chmax(dp[v][j], dp[v / 2][j - W[i]] + V[i]);
    }
  }
}
void solve(int v, int L) {
  int limit = (1 << 9) - 1;
  if (v <= limit) {
    int ans = 0;
    repc(i, L) chmax(ans, dp[v][i]);
    cout << ans << "\\n";
    return;
  }
  vector<int> vs;
  int v_ = v;
  while (v_ > limit) {
    vs.push_back(v_);
    v_ /= 2;
  }
  int sz = vs.size(), ans = 0;
  rep(i, 1 << sz) {
    int w = 0, v = 0;
    rep(j, sz) if ((i >> j) & 1) {
      w += W[vs[j] - 1];
      v += V[vs[j] - 1];
    }
    if (w <= L)
      chmax(ans, dp[v_][L - w] + v);
  }
  cout << ans << "\\n";
}
void answer() {
  cin >> n;
  V.assign(n, 0);
  W.assign(n, 0);
  rep(i, n) cin >> V[i] >> W[i];
  init_dp();
  int Q;
  cin >> Q;
  rep(i, Q) {
    int v, L;
    cin >> v >> L;
    solve(v, L);
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = int_fast64_t;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

void answer() {
  int n;
  cin >> n;
  vector<int> V(n), W(n);
  rep(i, n) cin >> V[i] >> W[i];
  map<int, vector<int>> memo;
  map<int, bool> memoized;
  int Q;
  cin >> Q;
  rep(q_, Q) {
    int v_, L;
    cin >> v_ >> L;
    vector<int> vs;
    while (1) {
      vs.emplace_back(v_);
      if (v_ == 1)
        break;
      else
        v_ /= 2;
    }
    reverse(ALL(vs));
    int sz = vs.size();
    vector<vector<int>> dp(20, vector<int>(100010, 0));
    rep(i, sz) {
      int v = vs[i] - 1;
      if (memoized[v]) {
        dp[i + 1] = memo[v];
      } else {
        repc(j, 100005) {
          dp[i + 1][j] = dp[i][j];
          if (W[v] <= j)
            chmax(dp[i + 1][j], dp[i][j - W[v]] + V[v]);
        }
        memoized[v] = true;
        memo[v] = dp[i + 1];
      }
    }
    int ans = 0;
    repc(j, L) chmax(ans, dp[sz][j]);
    cout << ans << "\\n";
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}