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

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
using P = pair<int, int>;
int n;
vector<int> v, w;
vector<set<P>> mem;

int f(int x, int l) {
  if (x <= (1 << 9)) {
    auto itr = mem[x].upper_bound(P(l, 1000000));
    itr--;
    return itr->second;
  }
  int res = 0;
  if (l - w[x] >= 0)
    res = f(x / 2, l - w[x]) + v[x];
  return max(res, f(x / 2, l));
}

void ins(int id, int val, int wei) {
  auto itr = mem[id].lower_bound(P(wei, 0));
  if (itr != mem[id].begin()) {
    itr--;
    if (itr->second > val)
      return;
  }
  for (;;) {
    auto itr = mem[id].lower_bound(P(wei, 0));
    if (itr == mem[id].end())
      break;
    if (itr->first == wei) {
      if (itr->second > val)
        return;
      mem[id].erase(itr);
    } else {
      if (itr->second > val)
        break;
      // cout << "era: " << id << " " << (itr->second) << " " << (itr->first) <<
      // endl;
      mem[id].erase(itr);
    }
  }
  mem[id].insert(P(wei, val));
  // cout << "ins: " << id << " " << wei << " " << val << endl;
}

int main(void) {
  cin >> n;
  v.assign(n + 1, 0);
  w.assign(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  mem.assign((1 << 9) + 1, set<P>());
  mem[0].insert(P(0, 0));
  for (int i = 1; i <= (1 << 9); i++) {
    if (i > n)
      break;
    for (P p : mem[i / 2]) {
      ins(i, p.second, p.first);
      if (p.first + w[i] <= 100000)
        ins(i, p.second + v[i], p.first + w[i]);
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, l;
    cin >> x >> l;
    cout << f(x, l) << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
using P = pair<int, int>;
int n;
vector<int> v, w;
vector<set<P>> mem;

int f(int x, int l) {
  if (x <= 9) {
    auto itr = mem[x].upper_bound(P(l, 1000000));
    itr--;
    return itr->second;
  }
  int res = 0;
  if (l - w[x] >= 0)
    res = f(x / 2, l - w[x]) + v[x];
  return max(res, f(x / 2, l));
}

void ins(int id, int val, int wei) {
  auto itr = mem[id].lower_bound(P(wei, 0));
  if (itr != mem[id].begin()) {
    itr--;
    if (itr->second > val)
      return;
  }
  for (;;) {
    auto itr = mem[id].lower_bound(P(wei, 0));
    if (itr == mem[id].end())
      break;
    if (itr->first == wei) {
      if (itr->second > val)
        return;
      mem[id].erase(itr);
    } else {
      if (itr->second > val)
        break;
      // cout << "era: " << id << " " << (itr->second) << " " << (itr->first) <<
      // endl;
      mem[id].erase(itr);
    }
  }
  mem[id].insert(P(wei, val));
  // cout << "ins: " << id << " " << wei << " " << val << endl;
}

int main(void) {
  cin >> n;
  v.assign(n + 1, 0);
  w.assign(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  mem.assign((1 << (9)), set<P>());
  mem[0].insert(P(0, 0));
  for (int i = 1; i <= (1 << 9); i++) {
    if (i > n)
      break;
    for (P p : mem[i / 2]) {
      ins(i, p.second, p.first);
      if (p.first + w[i] <= 100000)
        ins(i, p.second + v[i], p.first + w[i]);
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, l;
    cin >> x >> l;
    cout << f(x, l) << endl;
  }
}