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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;
  vector<P> items(N);
  rep(i, N) cin >> items[i].first >> items[i].second;
  ll Q;
  cin >> Q;

  vector<vector<ll>> dp(min(N + 1, (1LL << 10) + 1), vector<ll>(100001, 0));
  rep2(i, 1, min(N + 1, (1LL << 10) + 1)) {
    rep(j, 100001) {
      ll par = i / 2;
      dp[i][j] = dp[par][j];
      if (j - items[i - 1].second >= 0) {
        dp[i][j] = max(dp[i][j],
                       dp[par][j - items[i - 1].second] + items[i - 1].first);
      }
    }
  }
  vector<ll> ans(Q);
  vector<P> query(Q);
  rep(q, Q) cin >> query[q].first >> query[q].second;
  rep(q, Q) {
    ll v = query[q].first;
    ll L = query[q].second;
    vector<ll> use;
    v--;
    ll u = v;
    use.push_back(u);
    while (u != 0) {
      u = (u - 1) / 2;
      use.push_back(u);
    }
    if (v < (1LL << 10)) {
      ans[q] = dp[v + 1][L];
      continue;
    }
    sort(use.begin(), use.end());
    ll n = use.size();
    vector<ll> use_p;
    ll last = -1;
    rep(i, n) {
      if (use[i] >= (1LL << 10)) {
        use_p.push_back(use[i]);
      } else {
        last = max(last, use[i]);
      }
    }
    ll a = 0;
    rep(i, 1LL << (ll)use_p.size()) {
      ll value = 0;
      ll weight = 0;
      rep(j, use_p.size()) {
        if (i & (1 << j)) {
          value += items[use_p[j]].first;
          weight += items[use_p[j]].second;
          if (weight > L)
            break;
        }
      }
      if (L - weight < 0)
        continue;
      a = max(a, dp[last + 1][L - weight] + value);
    }
    ans[q] = a;
  }
  rep(q, Q) { cout << ans[q] << '\\n'; }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;
  vector<P> items(N);
  rep(i, N) cin >> items[i].first >> items[i].second;
  ll Q;
  cin >> Q;

  vector<vector<ll>> dp(min(N + 1, (1LL << 9) + 1), vector<ll>(100001, 0));
  rep2(i, 1, min(N + 1, (1LL << 9) + 1)) {
    rep(j, 100001) {
      ll par = i / 2;
      dp[i][j] = dp[par][j];
      if (j - items[i - 1].second >= 0) {
        dp[i][j] = max(dp[i][j],
                       dp[par][j - items[i - 1].second] + items[i - 1].first);
      }
    }
  }
  vector<ll> ans(Q);
  vector<P> query(Q);
  rep(q, Q) cin >> query[q].first >> query[q].second;
  rep(q, Q) {
    ll v = query[q].first;
    ll L = query[q].second;
    vector<ll> use;
    v--;
    ll u = v;
    use.push_back(u);
    while (u != 0) {
      u = (u - 1) / 2;
      use.push_back(u);
    }
    if (v < (1LL << 9)) {
      ans[q] = dp[v + 1][L];
      continue;
    }
    sort(use.begin(), use.end());
    ll n = use.size();
    vector<ll> use_p;
    ll last = -1;
    rep(i, n) {
      if (use[i] >= (1LL << 9)) {
        use_p.push_back(use[i]);
      } else {
        last = max(last, use[i]);
      }
    }
    ll a = 0;
    rep(i, 1LL << (ll)use_p.size()) {
      ll value = 0;
      ll weight = 0;
      rep(j, use_p.size()) {
        if (i & (1 << j)) {
          value += items[use_p[j]].first;
          weight += items[use_p[j]].second;
          if (weight > L)
            break;
        }
      }
      if (L - weight < 0)
        continue;
      a = max(a, dp[last + 1][L - weight] + value);
    }
    ans[q] = a;
  }
  rep(q, Q) { cout << ans[q] << '\\n'; }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}