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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 923372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

int N;
VI G[1 << 18];
int V[1 << 18];
int W[1 << 18];
int dp[1 << 9][100010];

int solve(int now, int L) {
  if (now < (1 << 9))
    return dp[now][L];

  int pa = (now + 1) / 2;
  pa--;
  int res = -INF;
  chmax(res, solve(pa, L));
  if (L - W[now] >= 0)
    chmax(res, solve(pa, L - W[now]) + V[now]);
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;

  REP(i, N) cin >> V[i] >> W[i];

  eREP(i, 100000) if (i >= W[0]) dp[0][i] = V[0];
  FOR(i, 1, min(N, 1 << 9)) {
    int pa = (i + 1) / 2;
    pa--;
    // cout << i << " " << pa << endl;
    eREP(j, 100000) {
      if (j != 0)
        chmax(dp[i][j], dp[i][j - 1]);
      if (j - W[i] >= 0 && i != 0) {
        chmax(dp[i][j], dp[pa][j - W[i]] + V[i]);
      }
      if (i != 0)
        chmax(dp[i][j], dp[pa][j]);
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    v--;
    cout << solve(v, L) << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 923372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

int N;
VI G[1 << 18];
int V[1 << 18];
int W[1 << 18];
int dp[1030][100010];

int solve(int now, int L) {
  if (now < (1 << 9))
    return dp[now][L];

  int pa = (now + 1) / 2;
  pa--;
  int res = -INF;
  chmax(res, solve(pa, L));
  if (L - W[now] >= 0)
    chmax(res, solve(pa, L - W[now]) + V[now]);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;

  REP(i, N) cin >> V[i] >> W[i];

  eREP(i, 100000) if (i >= W[0]) dp[0][i] = V[0];
  FOR(i, 1, min(N, 1 << 9)) {
    int pa = (i + 1) / 2;
    pa--;
    // cout << i << " " << pa << endl;
    eREP(j, 100000) {
      if (j != 0)
        chmax(dp[i][j], dp[i][j - 1]);
      if (j - W[i] >= 0 && i != 0) {
        chmax(dp[i][j], dp[pa][j - W[i]] + V[i]);
      }
      if (i != 0)
        chmax(dp[i][j], dp[pa][j]);
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    v--;
    cout << solve(v, L) << endl;
  }

  return 0;
}