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

// IOI 2021
#include <bits/stdc++.h>
using namespace std;

#define endl '\\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ends << H;
  debug_out(T...);
}
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int K = 9, N = (1 << (K << 1)) + 5, L = 1e5 + 5;

int n, V[N], W[N], SumV[1 << K], SumW[1 << K], DP[2 << K][L];
vector<int> P;

void DFS(int v) {
  if (v > n || v >= (2 << K))
    return;
  int p = v >> 1;
  for (int i = 0; i < L; i++) {
    DP[v][i] = DP[p][i];
    if (i >= W[v])
      DP[v][i] = max(DP[v][i], DP[p][i - W[v]] + V[v]);
  }
  DFS(v << 1), DFS((v << 1) | 1);
}

int main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  mt19937 Rnd(time(0));

  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> V[i] >> W[i];
  DFS(1);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    P.clear();
    while (v >= (2 << K))
      P.push_back(v), v >>= 1;
    int ans = DP[v][l];
    for (int mask = 1; mask < (1 << sz(P)); mask++) {
      SumV[mask] = SumV[mask - (mask & -mask)] + V[P[__builtin_ctz(mask)]];
      SumW[mask] = SumW[mask - (mask & -mask)] + W[P[__builtin_ctz(mask)]];
      if (SumW[mask] <= l)
        ans = max(ans, SumV[mask] + DP[v][l - SumW[mask]]);
    }
    cout << ans << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

// IOI 2021
#include <bits/stdc++.h>
using namespace std;

#define endl '\\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ends << H;
  debug_out(T...);
}
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int K = 9, N = (4 << K) + 5, M = (1 << (K + 1)) + 5, L = 1e5 + 5;

int n, V[N], W[N], SumV[1 << K], SumW[1 << K], DP[M][L];
vector<int> P;

void DFS(int v) {
  if (v > n || v >= (1 << (K + 1)))
    return;
  P.push_back(v);
  for (int mask = 1; mask < (1 << sz(P)); mask++) {
    SumV[mask] = SumV[mask - (mask & -mask)] + V[P[__builtin_ctz(mask)]];
    SumW[mask] = SumW[mask - (mask & -mask)] + W[P[__builtin_ctz(mask)]];
    if (SumW[mask] < L)
      DP[v][SumW[mask]] = max(DP[v][SumW[mask]], SumV[mask]);
  }
  for (int i = 1; i < N; i++)
    DP[v][i] = max(DP[v][i], DP[v][i - 1]);
  DFS(v << 1), DFS((v << 1) | 1);
  P.pop_back();
}

int main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  mt19937 Rnd(time(0));

  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> V[i] >> W[i];
  DFS(1);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    P.clear();
    while (v >= (1 << (K + 1)))
      P.push_back(v), v >>= 1;
    int ans = DP[v][l];
    for (int mask = 1; mask < (1 << sz(P)); mask++) {
      SumV[mask] = SumV[mask - (mask & -mask)] + V[P[__builtin_ctz(mask)]];
      SumW[mask] = SumW[mask - (mask & -mask)] + W[P[__builtin_ctz(mask)]];
      if (SumW[mask] <= l)
        ans = max(ans, SumV[mask] + DP[v][l - SumW[mask]]);
    }
    cout << ans << endl;
  }

  return 0;
}