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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

int dp[512][100001];

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

  cin >> N;
  vector<int> v(N + 1);
  vector<int> w(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  vector<int> m(1024);
  for (int i = 1; i < 1024; i++) {
    for (int j = 0; j < 10; j++) {
      if ((i >> j) & 1) {
        m[i] = j;
        break;
      }
    }
  }
  M = min(511, (int)N);
  for (int i = 1; i <= M; i++) {
    for (int j = 100000; j >= 0; j--) {
      dp[i][j] = dp[i / 2][j];
      if (j >= w[i])
        dp[i][j] = max(dp[i / 2][j - w[i]] + v[i], dp[i][j]);
    }
    for (int j = 1; j <= 100000; j++) {
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  pair<int, int> a[1024];
  cin >> K;
  while (K--) {
    cin >> L >> R;
    vector<int> V;
    vector<int> W;
    int box = L;
    while (box > M) {
      V.emplace_back(v[box]);
      W.emplace_back(w[box]);
      box >>= 1;
    }
    int ans = dp[box][R];
    for (int i = 1; i < 1 << V.size(); i++) {
      a[i] = a[i ^ (1 << m[i])];
      a[i].second += V[m[i]];
      a[i].first += W[m[i]];
      if (R - a[i].first >= 0)
        ans = max(ans, dp[box][R - a[i].first] + a[i].second);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

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

  cin >> N;
  vector<int> v(N + 1);
  vector<int> w(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  vector<int> m(1024);
  for (int i = 1; i < 1024; i++) {
    for (int j = 0; j < 10; j++) {
      if ((i >> j) & 1) {
        m[i] = j;
        break;
      }
    }
  }
  cin >> K;
  while (K--) {
    cin >> L >> R;
    vector<int> V;
    vector<int> W;
    int box = L;
    while (box) {
      V.emplace_back(v[box]);
      W.emplace_back(w[box]);
      box >>= 1;
    }
    int mid = V.size() / 2;
    vector<pair<int, int>> a(1 << mid);
    vector<pair<int, int>> b(1 << (V.size() - mid));
    for (int i = 1; i < 1 << mid; i++) {
      a[i] = a[i ^ (1 << m[i])];
      a[i].second += V[m[i]];
      a[i].first += W[m[i]];
    }
    for (int i = 1; i < 1 << (V.size() - mid); i++) {
      b[i] = b[i ^ (1 << m[i])];
      b[i].second += V[m[i] + mid];
      b[i].first += W[m[i] + mid];
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    vector<pair<int, int>> c;
    int mx = -1;
    for (auto i : b) {
      if (i.second > mx) {
        c.emplace_back(i);
        mx = i.second;
      }
    }
    reverse(c.begin(), c.end());
    int cnt = 0;
    int ans = 0;
    for (auto i : a) {
      while (cnt + 1 < c.size() && i.first + c[cnt].first > R)
        cnt++;
      if (i.first + c[cnt].first > R)
        break;
      ans = max(ans, i.second + c[cnt].second);
    }
    cout << ans << endl;
  }
  return 0;
}