Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;

struct item {
  int v, w;
  bool operator<(const item &r) const { return w < r.w; }
  void operator+=(const item &r) { w += r.w, v += r.v; }
};

vector<item> enum_powerset(const vector<item> &is) {
  vector<item> a = {{0, 0}};
  vector<item> c;
  c.reserve(1 << sz(is));
  for (auto &i : is) {
    auto b = a;
    for (auto &e : b)
      e += i;
    merge(all(a), all(b), back_inserter(c));
    swap(a, c);
    c.clear();
  }
  return a;
}

int solve(const vector<item> &a, const vector<item> &b, int W) {
  int ans = 0;
  int vbmax = -INF;
  auto itr = b.begin();
  for (auto &[va, wa] : a) {
    while (itr != b.end() && wa + itr->w <= W) {
      chmax(vbmax, itr->v);
      ++itr;
    }
    chmax(ans, va + vbmax);
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<item> is(n);
  for (auto &[v, w] : is)
    cin >> v >> w;
  is.insert(is.begin(), {0, 0});

  const int m = 10;
  int M = 1 << m;
  chmin(M, n + 1);
  vector<vector<item>> pre(M);
  pre[0] = {{0, 0}};
  rep2(i, 1, M) {
    auto a = pre[i / 2];
    for (auto &e : a)
      e += is[i];
    merge(all(pre[i / 2]), all(a), back_inserter(pre[i]));
  }
  rep(i, M) reverse(all(pre[i]));

  int q;
  cin >> q;
  rep(_, q) {
    int v, l;
    cin >> v >> l;
    vector<item> qi;
    while (v > M) {
      qi.push_back(is[v]);
      v /= 2;
    }
    cout << solve(pre[v], enum_powerset(qi), l) << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;

struct item {
  int v, w;
  bool operator<(const item &r) const { return w < r.w; }
  void operator+=(const item &r) { w += r.w, v += r.v; }
};

vector<item> enum_powerset(const vector<item> &is) {
  vector<item> a = {{0, 0}};
  vector<item> c;
  c.reserve(1 << sz(is));
  for (auto &i : is) {
    auto b = a;
    for (auto &e : b)
      e += i;
    merge(all(a), all(b), back_inserter(c));
    swap(a, c);
    c.clear();
  }
  return a;
}

int solve(const vector<item> &a, const vector<item> &b, int W) {
  int ans = 0;
  int vbmax = -INF;
  auto itr = b.begin();
  for (auto &[va, wa] : a) {
    while (itr != b.end() && wa + itr->w <= W) {
      chmax(vbmax, itr->v);
      ++itr;
    }
    chmax(ans, va + vbmax);
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<item> is(n);
  for (auto &[v, w] : is)
    cin >> v >> w;
  is.insert(is.begin(), {0, 0});

  const int m = 10;
  int M = 1 << m;
  chmin(M, n + 1);
  vector<vector<item>> pre(M);
  pre[0] = {{0, 0}};
  rep2(i, 1, M) {
    auto a = pre[i / 2];
    for (auto &e : a)
      e += is[i];
    merge(all(pre[i / 2]), all(a), back_inserter(pre[i]));
  }
  rep(i, M) reverse(all(pre[i]));

  int q;
  cin >> q;
  rep(_, q) {
    int v, l;
    cin >> v >> l;
    vector<item> qi;
    while (v >= M) {
      qi.push_back(is[v]);
      v /= 2;
    }
    cout << solve(pre[v], enum_powerset(qi), l) << '\n';
  }
}