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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
typedef vector<int> vi;
typedef long long lint;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
typedef unsigned long long ulint;
#define endl '\\n'
#define fst first
#define sed second
#define pb push_back
#define mp make_pair
#define rint register int
#define SZ(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define reveal(x) cerr << #x << " = " << (x) << endl
#define rep(it, f, e) for (rint it = (f); it <= (e); ++it)
#define per(it, f, e) for (rint it = (f); it >= (e); --it)
#define repe(it, x) for (auto it = (x).begin(); it != (x).end(); ++it)
const int INF = 1e5;
const int MAXN = (1 << 18) + 20;
int V[MAXN], W[MAXN];
map<int, lint> state[(1 << 9) + 20];
lint dp[(1 << 9) + 20][INF + 5];
inline void update(int i, int w, lint v) {
  if (w > INF)
    return;
  state[i][w] = max(state[i][w], v);
}
inline void dfs(int u, int lim) {
  for (auto &st : state[u]) {
    dp[u][st.fst] = max(dp[u][st.fst], st.sed);
  }
  rep(v, u * 2, u * 2 + 1) {
    if (v > lim)
      continue;
    for (auto &st : state[u]) {
      update(v, st.fst, st.sed);
      update(v, st.fst + W[v], st.sed + V[v]);
    }
    dfs(v, lim);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int n;
  cin >> n;
  rep(i, 1, n) { cin >> V[i] >> W[i]; }
  state[1][0] = 0;
  state[1][W[1]] = V[1];
  int lim = min(n, (1 << 9) - 1);
  dfs(1, lim);
  rep(i, 1, lim) rep(j, 1, INF) { dp[i][j] = max(dp[i][j], dp[i][j - 1]); }
  int q;
  cin >> q;
  while (q--) {
    int p, l;
    cin >> p >> l;
    lint res = 0;
    function<void(int, int, int)> solve;
    solve = [&](int u, int w, int v) {
      if (w > l)
        return;
      if (u <= lim) {
        res = max(res, v + dp[u][l - w]);
        return;
      }
      solve(u >> 1, w, v);
      solve(u >> 1, w + W[u], v + V[u]);
    };
    solve(p, 0, 0);
    cout << res << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
typedef vector<int> vi;
typedef long long lint;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
typedef unsigned long long ulint;
#define endl '\\n'
#define fst first
#define sed second
#define pb push_back
#define mp make_pair
#define rint register int
#define SZ(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define reveal(x) cerr << #x << " = " << (x) << endl
#define rep(it, f, e) for (rint it = (f); it <= (e); ++it)
#define per(it, f, e) for (rint it = (f); it >= (e); --it)
#define repe(it, x) for (auto it = (x).begin(); it != (x).end(); ++it)
const int INF = 1e5;
const int MAXN = (1 << 18) + 20;
int V[MAXN], W[MAXN];
map<int, lint> state[(1 << 9) + 20];
lint dp[(1 << 9) + 20][INF + 5];
inline void update(int i, int w, lint v) {
  if (w > INF)
    return;
  state[i][w] = max(state[i][w], v);
}
inline void dfs(int u, int lim) {
  for (auto &st : state[u]) {
    dp[u][st.fst] = max(dp[u][st.fst], st.sed);
  }
  rep(v, u * 2, u * 2 + 1) {
    if (v > lim)
      continue;
    for (auto &st : state[u]) {
      update(v, st.fst, st.sed);
      update(v, st.fst + W[v], st.sed + V[v]);
    }
    dfs(v, lim);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int n;
  cin >> n;
  rep(i, 1, n) { cin >> V[i] >> W[i]; }
  state[1][0] = 0;
  state[1][W[1]] = V[1];
  int lim = min(n, (1 << 9) - 1);
  dfs(1, lim);
  rep(i, 1, lim) rep(j, 1, INF) { dp[i][j] = max(dp[i][j], dp[i][j - 1]); }
  int q;
  cin >> q;
  while (q--) {
    int p, l;
    cin >> p >> l;
    lint res = 0;
    function<void(int, int, int)> solve;
    solve = [&](int u, int w, int v) {
      if (w > l)
        return;
      if (u <= lim) {
        res = max(res, v + dp[u][l - w]);
        return;
      }
      solve(u << 1, w, v);
      solve(u << 1, w + W[u], v + V[u]);
    };
    solve(p, 0, 0);
    cout << res << endl;
  }
  return 0;
}