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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

const int maxl = 112345;
const int maxn = (1 << 18) + 5;

int n, q;
int f[(1 << 9) + 1][maxl], v[maxn], w[maxn];

void dfs(int u) {
  FOR(i, 1, w[u] - 1) f[u][i] = max(f[u][i - 1], f[u >> 1][i]);
  FOR(i, w[u], int(1e5))
  f[u][i] = max(f[u][i - 1], max(f[u >> 1][i], f[u >> 1][i - w[u]] + v[u]));
  if ((u << 1) <= min(n, (1 << 9) - 1))
    dfs(u << 1);
  if ((u << 1 | 1) <= min(n, (1 << 9) - 1))
    dfs(u << 1 | 1);
}

int main() {
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d%d", v + i, w + i);
  dfs(1);
  scanf("%d", &q);
  while (q--) {
    int u, L;
    scanf("%d%d", &u, &L);
    if (u < 1 << 9) {
      printf("%d\\n", f[u][L]);
      continue;
    }
    vector<pii> tmp;
    while (u >= 1 << 9) {
      tmp.eb(v[u], w[u]);
      u >>= 1;
    }
    vector<int> V(1 << tmp.size()), W(1 << tmp.size());
    FOR(mask, 1, int(V.size()) - 1) {
      V[mask] = V[mask & (mask - 1)] + tmp[__builtin_ctz(mask)]._1;
      W[mask] = W[mask & (mask - 1)] + tmp[__builtin_ctz(mask)]._2;
    }
    int ans = 0;
    REP(mask, int(V.size()))
    if (W[mask] <= L) chkmax(ans, f[u][L - W[mask]] + V[mask]);
    printf("%d\\n", ans);
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

const int maxl = 112345;
const int maxn = (1 << 18) + 5;

int n, q;
int f[(1 << 9) + 1][maxl], v[maxn], w[maxn];

void dfs(int u) {
  FOR(i, 1, w[u] - 1) f[u][i] = max(f[u][i - 1], f[u >> 1][i]);
  FOR(i, w[u], int(1e5))
  f[u][i] = max(f[u][i - 1], max(f[u >> 1][i], f[u >> 1][i - w[u]] + v[u]));
  if ((u << 1) <= max(n, (1 << 9) - 1))
    dfs(u << 1);
  if ((u << 1 | 1) <= max(n, (1 << 9) - 1))
    dfs(u << 1 | 1);
}

int main() {
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d%d", v + i, w + i);
  dfs(1);
  scanf("%d", &q);
  while (q--) {
    int u, L;
    scanf("%d%d", &u, &L);
    if (u < 1 << 9) {
      printf("%d\\n", f[u][L]);
      continue;
    }
    vector<pii> tmp;
    while (u >= 1 << 9) {
      tmp.eb(v[u], w[u]);
      u >>= 1;
    }
    vector<int> V(1 << tmp.size()), W(1 << tmp.size());
    FOR(mask, 1, int(V.size()) - 1) {
      V[mask] = V[mask & (mask - 1)] + tmp[__builtin_ctz(mask)]._1;
      W[mask] = W[mask & (mask - 1)] + tmp[__builtin_ctz(mask)]._2;
    }
    int ans = 0;
    REP(mask, int(V.size()))
    if (W[mask] <= L) chkmax(ans, f[u][L - W[mask]] + V[mask]);
    printf("%d\\n", ans);
  }
}