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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef array<ll, 2> pii;
typedef vector<int> vi;

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int K = 9;
const int N = (1 << 18) + 123, M = 1e5 + 123;
int n, val[N], w[N], q, h[N], b[N];

int mv[(1 << 9) + 1], mw[(1 << 9) + 1];
int cnt, ind[N], opt[(1 << 9) + 1][M];

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  for (int i = 1; i < (1 << 9); i++) {
    for (int j = 0; j < 9; j++) {
      if (i >> j & 1) {
        b[i] = j;
        break;
      }
    }
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> w[i];
  }
  vi vec;
  for (int i = 1; i <= n; i++) {
    h[i] = h[i / 2] + 1;
    if (h[i] == K) {
      int v = i;
      vec.clear();
      while (v) {
        vec.pb(v);
        v /= 2;
      }
      int s = vec.size();
      ind[i] = cnt++;
      int k = ind[i];
      for (int j = 0; j < (1 << s); j++) {
        if (j > 0) {
          int el = vec[b[j]];
          mw[j] = mw[j ^ (1 << b[j])] + w[el];
          mv[j] = mv[j ^ (1 << b[j])] + val[el];
        }
        if (mw[j] < M) {
          opt[k][mw[j]] = max(opt[k][mw[j]], mv[j]);
        }
      }
      for (int j = 1; j < M; j++) {
        opt[k][j] = max(opt[k][j], opt[k][j - 1]);
      }
    }
  }
  cin >> q;
  while (q--) {
    int V, L;
    cin >> V >> L;
    if (h[V] <= K) {
      vec.clear();
      while (V) {
        vec.pb(V);
        V /= 2;
      }
      int s = vec.size(), res = 0;
      for (int i = 0; i < (1 << s); i++) {
        if (i > 0) {
          int el = vec[b[i]];
          mw[i] = mw[i ^ (1 << b[i])] + w[el];
          mv[i] = mv[i ^ (1 << b[i])] + val[el];
        }
        if (mw[i] <= L) {
          res = max(res, mv[i]);
        }
      }
      cout << res << '\\n';
    } else {
      vec.clear();
      while (h[V] > K) {
        vec.pb(V);
        V /= 2;
      }
      int k = ind[V];
      int s = vec.size(), res = 0;
      for (int i = 0; i < (1 << s); i++) {
        if (i > 0) {
          int el = vec[b[i]];
          mw[i] = mw[i ^ (1 << b[i])] + w[el];
          mv[i] = mv[i ^ (1 << b[i])] + val[el];
        }
        if (mw[i] <= L) {
          res = max(res, mv[i] + opt[k][L - mw[i]]);
        }
      }
      cout << res << '\\n';
    }
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef array<ll, 2> pii;
typedef vector<int> vi;

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int K = 3;
const int N = (1 << 18) + 123, M = 1e5 + 123;
int n, val[N], w[N], q, h[N], b[N];

int mv[(1 << 9) + 1], mw[(1 << 9) + 1];
int cnt, ind[N], opt[(1 << 9) + 1][M];

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  for (int i = 1; i < (1 << 9); i++) {
    for (int j = 0; j < 9; j++) {
      if (i >> j & 1) {
        b[i] = j;
        break;
      }
    }
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> w[i];
  }
  vi vec;
  for (int i = 1; i <= n; i++) {
    h[i] = h[i / 2] + 1;
    if (h[i] == K) {
      int v = i;
      vec.clear();
      while (v) {
        vec.pb(v);
        v /= 2;
      }
      int s = vec.size();
      ind[i] = cnt++;
      int k = ind[i];
      for (int j = 0; j < (1 << s); j++) {
        if (j > 0) {
          int el = vec[b[j]];
          mw[j] = mw[j ^ (1 << b[j])] + w[el];
          mv[j] = mv[j ^ (1 << b[j])] + val[el];
        }
        if (mw[j] < M) {
          opt[k][mw[j]] = max(opt[k][mw[j]], mv[j]);
        }
      }
      for (int j = 1; j < M; j++) {
        opt[k][j] = max(opt[k][j], opt[k][j - 1]);
      }
    }
  }
  cin >> q;
  while (q--) {
    int V, L;
    cin >> V >> L;
    if (h[V] <= K) {
      vec.clear();
      while (V) {
        vec.pb(V);
        V /= 2;
      }
      int s = vec.size(), res = 0;
      for (int i = 0; i < (1 << s); i++) {
        if (i > 0) {
          int el = vec[b[i]];
          mw[i] = mw[i ^ (1 << b[i])] + w[el];
          mv[i] = mv[i ^ (1 << b[i])] + val[el];
        }
        if (mw[i] <= L) {
          res = max(res, mv[i]);
        }
      }
      cout << res << '\\n';
    } else {
      vec.clear();
      while (h[V] > K) {
        vec.pb(V);
        V /= 2;
      }
      int k = ind[V];
      int s = vec.size(), res = 0;
      for (int i = 0; i < (1 << s); i++) {
        if (i > 0) {
          int el = vec[b[i]];
          mw[i] = mw[i ^ (1 << b[i])] + w[el];
          mv[i] = mv[i ^ (1 << b[i])] + val[el];
        }
        if (mw[i] <= L) {
          res = max(res, mv[i] + opt[k][L - mw[i]]);
        }
      }
      cout << res << '\\n';
    }
  }
}