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

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N, Q;
VI V, W;
VVI item;
int dp[(1 << 9) + 1][100001];

void dfs(int v) {
  if (v >= N)
    return;
  int par = (v - 1) / 2;
  if (v > 0)
    item[v] = VI(item[par]);
  item[v].push_back(v);
  dfs(2 * v + 1);
  dfs(2 * v + 2);
  return;
}

void dfs2(int v) {
  if (v >= N || item[v].size() > 9)
    return;
  int par = v == 0 ? 0 : (v - 1) / 2 + 1;

  REP(i, 100001) {
    chmax(dp[v + 1][i], dp[par][i]);
    if (i + W[v] <= 100000)
      chmax(dp[v + 1][i + W[v]], dp[par][i] + V[v]);
  }

  dfs2(2 * v + 1);
  dfs2(2 * v + 2);
  return;
}

int solve(int v, int L) {
  int K = item[v].size();
  if (K <= 9)
    return dp[v + 1][L];

  int par = (v - 1) / 2;
  while (item[par].size() > 9) {
    par = (par - 1) / 2;
  }
  par++;

  int N = K - 9;
  int ans = 0;
  REP(i, 1 << N) {
    int vv = 0, ww = 0;
    REP(j, N) if (i & (1 << j)) {
      vv += V[item[v][9 + j]];
      ww += W[item[v][9 + j]];
    }
    if (ww > L)
      continue;
    vv += dp[par][L - ww];
    chmax(ans, vv);
  }
  return ans;
}

void Main() {
  cin >> N;
  V.resize(N);
  W.resize(N);
  REP(i, N) cin >> V[i] >> W[i];

  cin >> Q;
  item.resize(N);
  dfs(0);
  dfs2(0);
  REP(i, Q) {
    int v, L;
    cin >> v >> L;
    v--;
    int ans = solve(v, L);
    cout << ans << en;
  }

  return;
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  int t = 1; // cin>>t;
  REP(_, t) Main();
  return 0;
}

Error: -11

Fixed submission:

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N, Q;
VI V, W;
VVI item;
int dp[(1 << 9) + 1][100001];

void dfs(int v) {
  if (v >= N)
    return;
  int par = (v - 1) / 2;
  if (v > 0)
    item[v] = VI(item[par]);
  item[v].push_back(v);
  dfs(2 * v + 1);
  dfs(2 * v + 2);
  return;
}

void dfs2(int v) {
  if (v >= N || item[v].size() > 9)
    return;
  int par = v == 0 ? 0 : (v - 1) / 2 + 1;

  REP(i, 100000 - W[v] + 1) {
    chmax(dp[v + 1][i], dp[par][i]);
    chmax(dp[v + 1][i + W[v]], dp[par][i] + V[v]);
  }

  dfs2(2 * v + 1);
  dfs2(2 * v + 2);
  return;
}

int solve(int v, int L) {
  int K = item[v].size();
  if (K <= 9)
    return dp[v + 1][L];

  int N = K - 9;
  int ans = 0;
  REP(i, 1 << N) {
    int vv = 0, ww = 0;
    REP(j, N) if (i & (1 << j)) {
      vv += V[item[v][9 + j]];
      ww += W[item[v][9 + j]];
    }
    if (ww > L)
      continue;
    vv += dp[v + 1][L - ww];
    chmax(ans, vv);
  }
  return ans;
}

void Main() {
  cin >> N;
  V.resize(N);
  W.resize(N);
  REP(i, N) cin >> V[i] >> W[i];

  cin >> Q;
  item.resize(N);
  dfs(0);
  dfs2(0);
  REP(i, Q) {
    int v, L;
    cin >> v >> L;
    v--;
    int ans = solve(v, L);
    cout << ans << en;
  }

  return;
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  int t = 1; // cin>>t;
  REP(_, t) Main();
  return 0;
}