Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  const int MaxL = 100000;
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> w(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  vector<int> a(q);
  vector<int> m(q);
  for (int i = 0; i < q; ++i) {
    cin >> a[i] >> m[i];
    --a[i];
  }
  int top = min(n, 1024);
  vector<vector<pair<int, int>>> vals(top);
  vals[0].emplace_back(0, 0);
  vals[0].emplace_back(w[0], v[0]);
  for (int i = 1; i < top; ++i) {
    vector<pair<int, int>> cur = vals[(i - 1) / 2];
    cur.reserve(2 * cur.size());
    for (auto p : vals[(i - 1) / 2]) {
      if (p.first + w[i] <= MaxL)
        cur.emplace_back(p.first + w[i], p.second + v[i]);
    }
    sort(cur.rbegin(), cur.rend());
    for (auto p : cur) {
      while (!vals[i].empty() && vals[i].back().second <= p.second)
        vals[i].pop_back();
      vals[i].push_back(p);
    }
    reverse(vals[i].begin(), vals[i].end());
  }
  for (int k = 0; k < q; ++k) {
    vector<pair<int, int>> cur_vals;
    cur_vals.emplace_back(0, 0);
    int z = a[k];
    while (z >= top) {
      int s = cur_vals.size();
      for (int i = 0; i < s; ++i) {
        if (w[z] + cur_vals[i].first <= m[k])
          cur_vals.emplace_back(w[z] + cur_vals[i].first,
                                v[z] + cur_vals[i].second);
      }
      z = (z - 1) / 2;
    }
    int best = 0;
    for (int i = 0; i < (int)cur_vals.size(); ++i) {
      auto p = lower_bound(vals[z].begin(), vals[z].end(),
                           make_pair(m[k] - cur_vals[i].first + 1, -1));
      if (p == vals[z].begin())
        continue;
      --p;
      best = max(best, cur_vals[i].second + (p)->second);
    }
    cout << best << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  const int MaxL = 100000;
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> w(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  vector<int> a(n);
  vector<int> m(n);
  for (int i = 0; i < q; ++i) {
    cin >> a[i] >> m[i];
    --a[i];
  }
  int top = min(n, 1024);
  vector<vector<pair<int, int>>> vals(top);
  vals[0].emplace_back(0, 0);
  vals[0].emplace_back(w[0], v[0]);
  for (int i = 1; i < top; ++i) {
    vector<pair<int, int>> cur = vals[(i - 1) / 2];
    cur.reserve(2 * cur.size());
    for (auto p : vals[(i - 1) / 2]) {
      if (p.first + w[i] <= MaxL)
        cur.emplace_back(p.first + w[i], p.second + v[i]);
    }
    sort(cur.rbegin(), cur.rend());
    for (auto p : cur) {
      while (!vals[i].empty() && vals[i].back().second <= p.second)
        vals[i].pop_back();
      vals[i].push_back(p);
    }
    reverse(vals[i].begin(), vals[i].end());
  }
  for (int k = 0; k < q; ++k) {
    vector<pair<int, int>> cur_vals;
    cur_vals.emplace_back(0, 0);
    int z = a[k];
    while (z >= top) {
      int s = cur_vals.size();
      for (int i = 0; i < s; ++i) {
        if (w[z] + cur_vals[i].first <= m[k])
          cur_vals.emplace_back(w[z] + cur_vals[i].first,
                                v[z] + cur_vals[i].second);
      }
      z = (z - 1) / 2;
    }
    int best = 0;
    for (int i = 0; i < cur_vals.size(); ++i) {
      auto p = lower_bound(vals[z].begin(), vals[z].end(),
                           make_pair(m[k] - cur_vals[i].first + 1, -1));
      if (p == vals[z].begin())
        continue;
      --p;
      best = max(best, cur_vals[i].second + (p)->second);
    }
    cout << best << "\\n";
  }
  return 0;
}