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

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
const int maxN = (1 << 18) + 10;
int n, q;
int v[maxN], w[maxN];
int V[18], W[18];
int SZ = 0;
int Ve[(1 << 12) + 10], We[(1 << 12) + 10];
const int BUBEN = 12;
vector<pair<int, int>> gg[(1 << (BUBEN + 1)) + 5];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> n;
  //    n = (1 << 18) - 1;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int ii = 1; ii <= min(n, (1 << 12) - 1); ii++) {
    int v = ii;
    SZ = 0;
    while (v) {
      V[SZ] = ::v[v];
      W[SZ] = ::w[v];
      v /= 2;
      SZ++;
    }
    assert(SZ <= 12);
    int h1 = SZ;
    memset(Ve, 0, sizeof Ve);
    memset(We, 0, sizeof We);
    for (int i = 0; i < h1; i++) {
      Ve[1 << i] = V[i];
      We[1 << i] = W[i];
    }
    for (int j = 0; j < h1; j++) {
      for (int i = 0; i < (1 << h1); i++) {
        if (i & (1 << j)) {
          Ve[i] += Ve[i ^ (1 << j)];
          We[i] += We[i ^ (1 << j)];
        }
      }
    }
    for (int i = 0; i < (1 << h1); i++) {
      gg[ii].emplace_back(We[i], Ve[i]);
    }
    sort(gg[ii].begin(), gg[ii].end());
    for (int i = 1; i < gg[ii].size(); i++) {
      gg[ii][i].second = max(gg[ii][i].second, gg[ii][i - 1].second);
    }
  }
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    vector<pair<int, int>> all;
    while (v > (1 << 12) - 1) {
      all.emplace_back(::w[v], ::v[v]);
      v /= 2;
    }
    int tot = all.size();
    int best = 0;
    for (int mask = 0; mask < (1 << tot); mask++) {
      int vv = 0;
      int ww = 0;
      for (int c = 0; c < tot; c++) {
        if (mask & (1 << c)) {
          vv += all[c].second;
          ww += all[c].first;
        }
      }
      if (ww > L)
        continue;
      assert(!gg[v].empty());
      auto it =
          upper_bound(gg[v].begin(), gg[v].end(), make_pair(L - ww, (int)1e9)) -
          gg[v].begin();
      assert(it > 0);
      best = max(best, vv + gg[v][it - 1].second);
    }
    cout << best << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
const int maxN = (1 << 18) + 10;
int n, q;
int v[maxN], w[maxN];
int V[18], W[18];
int SZ = 0;
int Ve[(1 << 12) + 10], We[(1 << 12) + 10];
const int BUBEN = 12;
vector<pair<int, int>> gg[(1 << (BUBEN + 1)) + 5];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> n;
  //    n = (1 << 18) - 1;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int ii = 1; ii <= min(n, (1 << 12) - 1); ii++) {
    int v = ii;
    SZ = 0;
    while (v) {
      V[SZ] = ::v[v];
      W[SZ] = ::w[v];
      v /= 2;
      SZ++;
    }
    assert(SZ <= 12);
    int h1 = SZ;
    memset(Ve, 0, sizeof Ve);
    memset(We, 0, sizeof We);
    for (int i = 0; i < h1; i++) {
      Ve[1 << i] = V[i];
      We[1 << i] = W[i];
    }
    for (int j = 0; j < h1; j++) {
      for (int i = 0; i < (1 << h1); i++) {
        if (i & (1 << j)) {
          Ve[i] += Ve[i ^ (1 << j)];
          We[i] += We[i ^ (1 << j)];
        }
      }
    }
    for (int i = 0; i < (1 << h1); i++) {
      gg[ii].emplace_back(We[i], Ve[i]);
    }
    sort(gg[ii].begin(), gg[ii].end());
    for (int i = 1; i < gg[ii].size(); i++) {
      gg[ii][i].second = max(gg[ii][i].second, gg[ii][i - 1].second);
    }
  }
  //    cout << " HERE " << endl;
  //    q = 1e5;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    //        v = 1e5;
    //        L = 1e5;
    SZ = 0;
    vector<pair<int, int>> all;
    while (gg[v].empty()) {
      all.emplace_back(::w[v], ::v[v]);
      v /= 2;
    }
    int tot = all.size();
    int best = 0;
    for (int mask = 0; mask < (1 << tot); mask++) {
      int vv = 0;
      int ww = 0;
      for (int c = 0; c < tot; c++) {
        if (mask & (1 << c)) {
          vv += all[c].second;
          ww += all[c].first;
        }
      }
      if (ww > L)
        continue;
      assert(!gg[v].empty());
      auto it =
          upper_bound(gg[v].begin(), gg[v].end(), make_pair(L - ww, (int)1e9)) -
          gg[v].begin();
      assert(it > 0);
      best = max(best, ww + gg[v][it - 1].second);
    }
    cout << best << '\\n';
  }
  return 0;
}