Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> Prepare(const vector<pair<int, int>> &a, int bound) {
  const int n = static_cast<int>(a.size());
  vector<pair<int, int>> result;
  for (int i = 0; i < (1 << n); ++i) {
    int x = 0, y = 0;
    for (int j = 0; j < n; ++j) {
      if (~i & (1 << j))
        continue;
      x += a[j].first;
      y += a[j].second;
    }

    if (x > bound)
      continue;
    result.emplace_back(x, y);
  }

  sort(result.begin(), result.end());
  int size = 1;
  for (int i = 1; i < (int)result.size(); ++i) {
    if (result[i].second <= result[size - 1].second)
      continue;

    result[size] = result[i];
    ++size;
  }

  result.resize(size);
  return result;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;

  vector<pair<int, int>> a(n);
  for (auto &e : a)
    cin >> e.second >> e.first;

  int s = min(512, n);
  vector<vector<pair<int, int>>> memo(s + 5);
  for (int i = 1; i < s; ++i) {
    int v = i;
    vector<pair<int, int>> b;
    while (v > 0) {
      b.push_back(a[v - 1]);
      v /= 2;
    }

    memo[i] = Prepare(b, 100000);
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int v, bound;
    cin >> v >> bound;
    vector<pair<int, int>> b, x, y;
    while (v > 0) {
      if (v < s) {
        y = memo[v];
        break;
      }
      b.push_back(a[v - 1]);
      v /= 2;
    }

    x = Prepare(b, bound);
    while (y.back().first > bound)
      y.pop_back();
    const int n = static_cast<int>(x.size());
    const int m = static_cast<int>(y.size());
    int result = max(x.back().second, y.back().second);
    for (int i = 0, j = m - 1; i < n; ++i) {
      while (j >= 0 && x[i].first + y[j].first > bound)
        --j;
      if (j >= 0)
        result = max(result, x[i].second + y[j].second);
    }

    cout << result << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> Prepare(const vector<pair<int, int>> &a, int bound) {
  const int n = static_cast<int>(a.size());
  vector<pair<int, int>> result;
  for (int i = 0; i < (1 << n); ++i) {
    int x = 0, y = 0;
    for (int j = 0; j < n; ++j) {
      if (~i & (1 << j))
        continue;
      x += a[j].first;
      y += a[j].second;
    }

    if (x > bound)
      continue;
    result.emplace_back(x, y);
  }

  sort(result.begin(), result.end());
  int size = 1;
  for (int i = 1; i < (int)result.size(); ++i) {
    if (result[i].second <= result[size - 1].second)
      continue;

    result[size] = result[i];
    ++size;
  }

  result.resize(size);
  return result;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;

  vector<pair<int, int>> a(n);
  for (auto &e : a)
    cin >> e.second >> e.first;

  int s = min(1024, n + 1);
  vector<vector<pair<int, int>>> memo(s + 5);
  for (int i = 1; i < s; ++i) {
    int v = i;
    vector<pair<int, int>> b;
    while (v > 0) {
      b.push_back(a[v - 1]);
      v /= 2;
    }

    memo[i] = Prepare(b, 100000);
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int v, bound;
    cin >> v >> bound;
    vector<pair<int, int>> b, x, y;
    while (v > 0) {
      if (v < s) {
        y = memo[v];
        break;
      }
      b.push_back(a[v - 1]);
      v /= 2;
    }

    x = Prepare(b, bound);
    while (y.back().first > bound)
      y.pop_back();
    const int n = static_cast<int>(x.size());
    const int m = static_cast<int>(y.size());
    int result = max(x.back().second, y.back().second);
    for (int i = 0, j = m - 1; i < n; ++i) {
      while (j >= 0 && x[i].first + y[j].first > bound)
        --j;
      if (j >= 0)
        result = max(result, x[i].second + y[j].second);
    }

    cout << result << '\n';
  }
}