Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
const int P = 1 << 2;
const int INF = 1 << 22;
int main() {
  int N;
  cin >> N;
  vector<int> V(N), W(N);
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }
  int Q;
  cin >> Q;
  vector<int> query_v(Q);
  vector<int> query_L(Q);
  set<int> qv;
  for (int i = 0; i < Q; i++) {
    cin >> query_v[i] >> query_L[i];
    qv.insert(query_v[i]);
  }
  vector<vector<pair<int, int>>> t2(N);
  for (int i = 0; i < N; i++) {
    vector<int> V2, W2;
    int v = i + 1;
    V2.push_back(V[i]);
    W2.push_back(W[i]);
    if (v < P) {
      while (v > 1) {
        v /= 2;
        V2.push_back(V[v - 1]);
        W2.push_back(W[v - 1]);
      }
    } else {
      if (!qv.count(v)) {
        continue;
      }
      while (v >= P * 2) {
        v /= 2;
        V2.push_back(V[v - 1]);
        W2.push_back(W[v - 1]);
      }
    }
    int cnt = V2.size();
    vector<pair<int, int>> t;
    for (int j = 0; j < (1 << cnt); j++) {
      int v_t = 0;
      int w_t = 0;
      for (int k = 0; k < cnt; k++) {
        if ((j >> k) & 1) {
          v_t += V2[k];
          w_t += W2[k];
        }
      }
      t.emplace_back(w_t, v_t);
    }
    sort(t.begin(), t.end());
    t2[i].push_back(t[0]);
    int cnt2 = t.size();
    for (int j = 1; j < cnt2; j++) {
      if (t2[i].back().first == t[j].first) {
        t2[i].back().second = t[j].second;
      } else {
        if (t2[i].back().second < t[j].second) {
          t2[i].push_back(t[j]);
        }
      }
    }
  }
  /*
  for (int i = 0; i < N; i++){
    cout << "i = " << i << endl;
    for (auto p : t2[i]){
      cout << "(" << p.first << "," << p.second << ") ";
    }
    cout << endl;
  }
  */
  for (int i = 0; i < Q; i++) {
    int v = query_v[i];
    int L = query_L[i];
    if (v < P) {
      auto itr =
          upper_bound(t2[v - 1].begin(), t2[v - 1].end(), make_pair(L, INF)) -
          1;
      cout << (*itr).second << endl;
    } else {
      int w = v;
      while (w >= P) {
        w /= 2;
      }
      int ans = 0;
      for (auto p : t2[v - 1]) {
        if (L - p.first >= 0) {
          auto itr = upper_bound(t2[w - 1].begin(), t2[w - 1].end(),
                                 make_pair(L - p.first, INF)) -
                     1;
          ans = max(ans, p.second + (*itr).second);
        }
      }
      cout << ans << endl;
    }
  }
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
const int P = 1 << 11;
const int INF = 1 << 22;
int main() {
  int N;
  cin >> N;
  vector<int> V(N), W(N);
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }
  int Q;
  cin >> Q;
  vector<int> query_v(Q);
  vector<int> query_L(Q);
  set<int> qv;
  for (int i = 0; i < Q; i++) {
    cin >> query_v[i] >> query_L[i];
    qv.insert(query_v[i]);
  }
  vector<vector<pair<int, int>>> t2(N);
  for (int i = 0; i < N; i++) {
    vector<int> V2, W2;
    int v = i + 1;
    V2.push_back(V[i]);
    W2.push_back(W[i]);
    if (v < P) {
      while (v > 1) {
        v /= 2;
        V2.push_back(V[v - 1]);
        W2.push_back(W[v - 1]);
      }
    } else {
      if (!qv.count(v)) {
        continue;
      }
      while (v >= P * 2) {
        v /= 2;
        V2.push_back(V[v - 1]);
        W2.push_back(W[v - 1]);
      }
    }
    int cnt = V2.size();
    vector<pair<int, int>> t;
    for (int j = 0; j < (1 << cnt); j++) {
      int v_t = 0;
      int w_t = 0;
      for (int k = 0; k < cnt; k++) {
        if ((j >> k) & 1) {
          v_t += V2[k];
          w_t += W2[k];
        }
      }
      t.emplace_back(w_t, v_t);
    }
    sort(t.begin(), t.end());
    t2[i].push_back(t[0]);
    int cnt2 = t.size();
    for (int j = 1; j < cnt2; j++) {
      if (t2[i].back().first == t[j].first) {
        t2[i].back().second = t[j].second;
      } else {
        if (t2[i].back().second < t[j].second) {
          t2[i].push_back(t[j]);
        }
      }
    }
  }
  /*
  for (int i = 0; i < N; i++){
    cout << "i = " << i << endl;
    for (auto p : t2[i]){
      cout << "(" << p.first << "," << p.second << ") ";
    }
    cout << endl;
  }
  */
  for (int i = 0; i < Q; i++) {
    int v = query_v[i];
    int L = query_L[i];
    if (v < P) {
      auto itr =
          upper_bound(t2[v - 1].begin(), t2[v - 1].end(), make_pair(L, INF)) -
          1;
      cout << (*itr).second << endl;
    } else {
      int w = v;
      while (w >= P) {
        w /= 2;
      }
      int ans = 0;
      for (auto p : t2[v - 1]) {
        if (L - p.first >= 0) {
          auto itr = upper_bound(t2[w - 1].begin(), t2[w - 1].end(),
                                 make_pair(L - p.first, INF)) -
                     1;
          ans = max(ans, p.second + (*itr).second);
        }
      }
      cout << ans << endl;
    }
  }
}