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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
const int M = 3e5 + 5;
int val[M], wt[M];
pair<int, int> res[2][M];
vector<pair<int, int>> vec[M];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> wt[i];
  }
  vec[0].pb({0, 0});
  for (int i = 1; i < (1 << 10); i++) {
    int p = i / 2;
    for (int j = 0; j < sz(vec[p]); j++) {
      vec[i].pb(vec[p][j]);
      vec[i].pb({vec[p][j].ff + wt[i], vec[p][j].ss + val[i]});
    }
    sort(all(vec[i]));
    for (int j = 1; j < sz(vec[i]); j++)
      vec[i][j].ss = max(vec[i][j].ss, vec[i][j - 1].ss);
  }
  int q;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    vector<int> vt;
    while (v >= (1 << 10)) {
      vt.pb(v);
      v /= 2;
    }
    vector<pair<int, int>> res;
    for (int i = 0; i < (1 << sz(vt)); i++) {
      int x = 0, y = 0;
      for (int j = 0; j < sz(vt); j++) {
        if ((i & (1 << j)) == 0)
          continue;
        x += val[vt[j]];
        y += wt[vt[j]];
      }
      res.pb({y, x});
    }
    sort(all(res));
    int curr = sz(vec[v]) - 1;
    int ans = 0;
    for (int i = 0; i < sz(res); i++) {
      while (curr >= 0) {
        if (res[i].ff + vec[v][curr].ff > L)
          curr--;
        else
          break;
      }
      if (curr < 0)
        break;
      ans = max(ans, res[i].ss + vec[v][curr].ss);
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
const int M = 3e5 + 5;
int val[M], wt[M];
pair<int, int> res[2][M];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> wt[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    vector<int> vec;
    while (v) {
      vec.pb(v);
      v /= 2;
    }
    if (sz(vec) == 1) {
      if (wt[vec[0]] <= L)
        cout << val[vec[0]] << "\\n";
      else
        cout << "0\\n";
      continue;
    }
    int m = sz(vec) / 2;
    for (int i = 0; i < (1 << m); i++) {
      int x = 0, y = 0;
      for (int j = 0; j < m; j++) {
        if ((i & (1 << j))) {
          x += val[vec[j]];
          y += wt[vec[j]];
        }
      }
      res[0][i] = {y, x};
    }
    for (int i = 0; i < (1 << (sz(vec) - m)); i++) {
      int x = 0, y = 0;
      for (int j = 0; j < (int)sz(vec) - m; j++) {
        if ((i & (1 << j))) {
          x += val[vec[j + m]];
          y += wt[vec[j + m]];
        }
      }
      res[1][i] = {y, x};
    }
    int ans = 0;
    sort(res[0], res[0] + (1 << m));
    sort(res[1], res[1] + (1 << (sz(vec) - m)));
    for (int j = 1; j < (1 << (sz(vec) - m)); j++) {
      res[1][j].ss = max(res[1][j].ss, res[1][j - 1].ss);
    }
    int cur = (1 << (sz(vec) - m)) - 1;
    for (int i = 0; i < (1 << m); i++) {
      while (cur >= 0) {
        if (res[0][i].ff + res[1][cur].ff > L)
          cur--;
        else
          break;
      }
      if (cur < 0)
        break;
      ans = max(ans, res[0][i].ss + res[1][cur].ss);
    }
    cout << ans << "\\n";
  }
  return 0;
}