Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long n;
  cin >> n;
  vector<int> vs(n);
  vector<int> ws(n);
  for (int i = 0; i < n; i++) {
    cin >> vs[i] >> ws[i];
  }
  int q;
  cin >> q;

  vector<vector<pair<int, int>>> rek(n);
  int han = 9;
  int sikiri = (1 << han) - 1;
  for (int i = 0; i < n; i++) {
    vector<int> ta, ta2;
    int cur = i;
    for (int j = 0; j < han; j++) {
      ta.push_back(ws[cur]);
      ta2.push_back(vs[cur]);
      int nex = (cur - 1) / 2;
      if (cur == 0)
        break;
      cur = nex;
      if (i >= sikiri && nex < sikiri)
        break;
    }

    vector<pair<int, int>> zen(1, make_pair(0, 0));
    for (int j = 0; j < (int)ta.size(); j++) {
      vector<pair<int, int>> nzen = zen;
      for (int k = 0; k < (int)zen.size(); k++) {
        pair<int, int> p0 =
            make_pair(zen[k].first + ta[j], zen[k].second + ta2[j]);
        nzen.push_back(p0);
      }
      nzen.swap(zen);
    }
    sort(zen.begin(), zen.end());
    int mx = 0;
    for (int i = 0; i < (int)zen.size(); i++) {
      mx = max(mx, zen[i].second);
      zen[i].second = mx;
      // cout << zen[i].first << " " << zen[i].second << "\n";
    }
    // cout << "\n";
    rek[i] = zen;
  }

  for (int tc = 0; tc < q; tc++) {
    int v, L;
    cin >> v >> L;
    v--;

    vector<int> left;
    for (int i = 0; i < (int)rek[v].size(); i++) {
      left.push_back(rek[v][i].first);
    }
    int cur = v;
    bool bo = true;
    for (int j = 0; j < han; j++) {
      int nex = (cur - 1) / 2;
      if (cur == 0) {
        bo = false;
        break;
      }
      cur = nex;
      if (v >= sikiri && nex < sikiri)
        break;
    }
    if (!bo) {
      int ind = (int)(upper_bound(left.begin(), left.end(), L) - left.begin());
      if (ind == 0) {
        cout << 0 << "\n";
      } else {
        ind--;
        cout << rek[v][ind].second << "\n";
      }
      continue;
    }
    vector<int> right;
    for (int i = 0; i < (int)rek[cur].size(); i++) {
      right.push_back(rek[cur][i].first);
    }

    int res = 0;
    int rind = (int)right.size();
    rind--;
    for (int i = 0; i < (int)left.size(); i++) {
      if (left[i] > L)
        break;
      long long dif = L - left[i];
      while (rind >= 0 && right[rind] > dif)
        rind--;
      if (rind < 0)
        break;
      res = max(res, rek[v][i].second + rek[cur][rind].second);
    }
    cout << res << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long n;
  cin >> n;
  vector<int> vs(n);
  vector<int> ws(n);
  for (int i = 0; i < n; i++) {
    cin >> vs[i] >> ws[i];
  }
  int q;
  cin >> q;

  vector<vector<pair<int, int>>> rek(n);
  int han = 11;
  int sikiri = (1 << han) - 1;
  for (int i = 0; i < n; i++) {
    vector<int> ta, ta2;
    int cur = i;
    for (int j = 0; j < han; j++) {
      ta.push_back(ws[cur]);
      ta2.push_back(vs[cur]);
      int nex = (cur - 1) / 2;
      if (cur == 0)
        break;
      cur = nex;
      if (i >= sikiri && nex < sikiri)
        break;
    }

    vector<pair<int, int>> zen(1, make_pair(0, 0));
    for (int j = 0; j < (int)ta.size(); j++) {
      vector<pair<int, int>> nzen = zen;
      for (int k = 0; k < (int)zen.size(); k++) {
        pair<int, int> p0 =
            make_pair(zen[k].first + ta[j], zen[k].second + ta2[j]);
        nzen.push_back(p0);
      }
      nzen.swap(zen);
    }
    sort(zen.begin(), zen.end());
    int mx = 0;
    for (int i = 0; i < (int)zen.size(); i++) {
      mx = max(mx, zen[i].second);
      zen[i].second = mx;
      // cout << zen[i].first << " " << zen[i].second << "\n";
    }
    // cout << "\n";
    rek[i] = zen;
  }

  for (int tc = 0; tc < q; tc++) {
    int v, L;
    cin >> v >> L;
    v--;

    vector<int> left;
    for (int i = 0; i < (int)rek[v].size(); i++) {
      left.push_back(rek[v][i].first);
    }
    int cur = v;
    bool bo = true;
    for (int j = 0; j < han; j++) {
      int nex = (cur - 1) / 2;
      if (cur == 0) {
        bo = false;
        break;
      }
      cur = nex;
      if (v >= sikiri && nex < sikiri)
        break;
    }
    if (!bo) {
      int ind = (int)(upper_bound(left.begin(), left.end(), L) - left.begin());
      if (ind == 0) {
        cout << 0 << "\n";
      } else {
        ind--;
        cout << rek[v][ind].second << "\n";
      }
      continue;
    }
    vector<int> right;
    for (int i = 0; i < (int)rek[cur].size(); i++) {
      right.push_back(rek[cur][i].first);
    }

    int res = 0;
    int rind = (int)right.size();
    rind--;
    for (int i = 0; i < (int)left.size(); i++) {
      if (left[i] > L)
        break;
      long long dif = L - left[i];
      while (rind >= 0 && right[rind] > dif)
        rind--;
      if (rind < 0)
        break;
      res = max(res, rek[v][i].second + rek[cur][rind].second);
    }
    cout << res << "\n";
  }
  return 0;
}