Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int i = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

#define H 9

int dp[1 << H][100001];

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> VW(N);
  REP(i, N) cin >> VW[i].first >> VW[i].second;
  int Q;
  cin >> Q;
  vector<pair<int, int>> vL(N);
  REP(i, Q) {
    cin >> vL[i].first >> vL[i].second;
    vL[i].first--;
  }

  dp[0][VW[0].second] = VW[0].first;
  for (int i = 1; i < min(N, 1 << H); i++) {
    int par = (i - 1) / 2;

    for (int j = 0; j <= 100000; j++)
      dp[i][j] = dp[par][j];

    for (int j = 0; j <= 100000; j++) {
      int w = j + VW[i].second;
      int v = dp[par][j] + VW[i].first;
      if (w > 100000)
        break;
      dp[i][w] = max(dp[i][w], v);
    }
  }

  for (int i = 0; i < (1 << H); i++)
    for (int j = 1; j <= 100000; j++)
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);

  vector<int> anss(Q);
  REP(q, Q) {
    int v = vL[q].first;
    int L = vL[q].second;
    vector<pair<int, int>> z;

    while (true) {
      if (v < 1 << H)
        break;
      z.push_back(VW[v]);
      v = (v - 1) / 2;
    }

    int sz = z.size();

    int ans = 0;
    for (int i = 0; i < (1 << sz); i++) {
      int Vtot = 0;
      int Wtot = 0;
      for (int j = 0; j < sz; j++) {
        if (i & (1 << j)) {
          Vtot += z[j].first;
          Wtot += z[j].second;
        }
      }
      if (Wtot > L)
        continue;
      ans = max(ans, Vtot + dp[v][L - Wtot]);
    }
    anss[q] = ans;
  }
  for (auto x : anss)
    cout << x << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int i = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

#define H 9

int dp[1 << H][100001];

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> VW(N);
  REP(i, N) cin >> VW[i].first >> VW[i].second;
  int Q;
  cin >> Q;
  vector<pair<int, int>> vL(Q);
  REP(i, Q) {
    cin >> vL[i].first >> vL[i].second;
    vL[i].first--;
  }

  dp[0][VW[0].second] = VW[0].first;
  for (int i = 1; i < min(N, 1 << H); i++) {
    int par = (i - 1) / 2;

    for (int j = 0; j <= 100000; j++)
      dp[i][j] = dp[par][j];

    for (int j = 0; j <= 100000; j++) {
      int w = j + VW[i].second;
      int v = dp[par][j] + VW[i].first;
      if (w > 100000)
        break;
      dp[i][w] = max(dp[i][w], v);
    }
  }

  for (int i = 0; i < (1 << H); i++)
    for (int j = 1; j <= 100000; j++)
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);

  vector<int> anss(Q);
  REP(q, Q) {
    int v = vL[q].first;
    int L = vL[q].second;
    vector<pair<int, int>> z;

    while (true) {
      if (v < 1 << H)
        break;
      z.push_back(VW[v]);
      v = (v - 1) / 2;
    }

    int sz = z.size();

    int ans = 0;
    for (int i = 0; i < (1 << sz); i++) {
      int Vtot = 0;
      int Wtot = 0;
      for (int j = 0; j < sz; j++) {
        if (i & (1 << j)) {
          Vtot += z[j].first;
          Wtot += z[j].second;
        }
      }
      if (Wtot > L)
        continue;
      ans = max(ans, Vtot + dp[v][L - Wtot]);
    }
    anss[q] = ans;
  }
  for (auto x : anss)
    cout << x << endl;
  return 0;
}