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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<pair<int, int>> item(n);
  rep(i, n) cin >> item[i].first >> item[i].second;
  vector<vector<int>> dp(min(1 << 9, n), vector<int>(100001));
  for (int j = 0; j < 100001; j++) {
    dp[0][j] = (j >= item[0].second ? item[0].first : 0);
  }
  for (int i = 1; i < min(1 << 9, n); i++) {
    int p = (i - 1) / 2;
    for (int j = 0; j < 100001; j++) {
      dp[i][j] = dp[p][j];
      if (j >= item[i].second) {
        dp[i][j] = max(dp[i][j], dp[p][j - item[i].second] + item[i].first);
      }
    }
  }
  int q;
  cin >> q;
  rep(_, q) {
    int v, l;
    cin >> v >> l;
    --v;
    vector<int> knap1;
    while (v >= (1 << 9)) {
      knap1.push_back(v);
      v = (v - 1) / 2;
    }
    map<int, int> mp1;
    ll ans = 0;
    rep(i, 1 << ((int)knap1.size())) {
      ll val, wei;
      val = wei = 0;
      rep(j, knap1.size()) {
        if (i & 1 << j) {
          val += item[knap1[j]].first;
          wei += item[knap1[j]].second;
        }
      }
      if (wei > l)
        continue;
      ans = max(ans, val + dp[v][l - wei]);
    }
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<pair<int, int>> item(n);
  rep(i, n) cin >> item[i].first >> item[i].second;

  map<int, int> mp[(1 << 9) + 1];
  rep1(i, min(1 << 9, n)) {
    int v = i;
    vector<int> knap1;
    while (v > 0) {
      int ind = v - 1;
      knap1.push_back(ind);
      v /= 2;
    }
    rep(l, 1 << ((int)knap1.size())) {
      ll val, wei;
      val = wei = 0;
      rep(j, knap1.size()) {
        if (l & 1 << j) {
          val += item[knap1[j]].first;
          wei += item[knap1[j]].second;
        }
      }
      // cout << "wei val " << wei << " " << val << endl;
      if (wei > 100000)
        continue;
      if (mp[i][wei])
        mp[i][wei] = max(mp[i][wei], ((int)val));
      else
        mp[i][wei] = val;
    }
  }

  int q;
  cin >> q;
  rep(_, q) {
    int v, l;
    cin >> v >> l;
    vector<int> knap1;
    while (v > (1 << 9)) {
      int ind = v - 1;
      knap1.push_back(ind);
      v /= 2;
    }
    map<int, int> mp1;
    rep(i, 1 << ((int)knap1.size())) {
      ll val, wei;
      val = wei = 0;
      rep(j, knap1.size()) {
        if (i & 1 << j) {
          val += item[knap1[j]].first;
          wei += item[knap1[j]].second;
        }
      }
      if (wei > l)
        continue;
      mp1[wei] = max(mp1[wei], ((int)val));
    }
    auto itr1 = mp[v].end();
    --itr1;
    auto itr2 = mp1.begin();
    int mval2 = 0;
    int ans = 0;
    for (;; itr1--) {
      if ((*itr1).first > l)
        continue;
      while (itr2 != mp1.end() && (*itr1).first + (*itr2).first <= l) {
        mval2 = max(mval2, (*itr2).second);
        itr2++;
      }
      // cout << ans << endl;
      // cout << mval2  << " " << (*itr1).second  << endl;
      ans = max(ans, mval2 + (*itr1).second);
      if (itr1 == mp[v].begin())
        break;
    }
    cout << ans << endl;
  }

  return 0;
}