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

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

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

void solve(long long N, std::vector<long long> V, std::vector<long long> W,
           long long Q, std::vector<long long> v, std::vector<long long> L) {
  vector<vector<int>> dp(min<int>(pow(2, 10) + 1, N + 1),
                         vector<int>(1e5 + 1, 0));
  rep(i, 1, dp.size()) {
    int parent = i / 2;
    rep(j, 0, dp.front().size()) {
      dp[i][j] = dp[parent][j];
      if (j - W[i] >= 0)
        dp[i][j] = max<int>(dp[i][j], dp[parent][j - W[i]] + V[i]);
      if (j > 0)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  rep(ti, 0, Q) {
    if (v[ti] < dp.size()) {
      cout << dp[v[ti]][L[ti]] << endl;
      continue;
    }
    vector<pair<int, int>> items;
    int current = v[ti];
    while (current >= dp.size()) {
      items.emplace_back(W[current], V[current]);
      current /= 2;
    }
    int answer = 0;
    rep(i, 0, 1 << items.size()) {
      int weightSum = 0, valueSum = 0;
      rep(j, 0, items.size()) {
        if (i & (1 << j)) {
          weightSum += items[j].first;
          valueSum += items[j].second;
        }
        if (weightSum > L[ti])
          break;
      }
      if (weightSum <= L[ti]) {
        answer = max(answer, valueSum + dp[current][L[ti] - weightSum]);
      }
    }

    cout << answer << endl;
  }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> V(N + 1);
  std::vector<long long> W(N + 1);
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &V[i]);
    scanf("%lld", &W[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> v(Q);
  std::vector<long long> L(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &v[i]);
    scanf("%lld", &L[i]);
  }
  solve(N, std::move(V), std::move(W), Q, std::move(v), std::move(L));
  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

void solve(long long N, std::vector<long long> V, std::vector<long long> W,
           long long Q, std::vector<long long> v, std::vector<long long> L) {
  vector<vector<int>> dp(min<int>(pow(2, 9) + 1, N + 1),
                         vector<int>(1e5 + 1, 0));
  rep(i, 1, dp.size()) {
    int parent = i / 2;
    rep(j, 0, dp.front().size()) {
      dp[i][j] = dp[parent][j];
      if (j - W[i] >= 0)
        dp[i][j] = max<int>(dp[i][j], dp[parent][j - W[i]] + V[i]);
      if (j > 0)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  rep(ti, 0, Q) {
    if (v[ti] < dp.size()) {
      cout << dp[v[ti]][L[ti]] << endl;
      continue;
    }
    vector<pair<int, int>> items;
    int current = v[ti];
    while (current >= dp.size()) {
      items.emplace_back(W[current], V[current]);
      current /= 2;
    }
    int answer = 0;
    rep(i, 0, 1 << items.size()) {
      int weightSum = 0, valueSum = 0;
      rep(j, 0, items.size()) {
        if (i & (1 << j)) {
          weightSum += items[j].first;
          valueSum += items[j].second;
        }
        if (weightSum > L[ti])
          break;
      }
      if (weightSum <= L[ti]) {
        answer = max(answer, valueSum + dp[current][L[ti] - weightSum]);
      }
    }

    cout << answer << endl;
  }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> V(N + 1);
  std::vector<long long> W(N + 1);
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &V[i]);
    scanf("%lld", &W[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> v(Q);
  std::vector<long long> L(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &v[i]);
    scanf("%lld", &L[i]);
  }
  solve(N, std::move(V), std::move(W), Q, std::move(v), std::move(L));
  return 0;
}