Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define equals(a, b) (fabs((a) - (b)) < EPS)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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;
}

template <typename T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

template <typename T> void print_array(const T &ary, int size) {
  REP(i, size) { cout << ary[i] << " "; }
  cout << endl;
}

const int mod = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(12);

  int N;
  cin >> N;
  VL V(N + 10), W(N + 10);
  REP(i, N) cin >> V[i + 1] >> W[i + 1];

  VVL dp((1 << 9), VL(100010));
  FOR(j, W[1], 100010) dp[1][j] = V[1];
  FOR(i, 1, (1 << 8) + 1) {
    if (i > N)
      break;
    REP(j, 100010) {
      if (i * 2 <= N) {
        if (j - W[i * 2] >= 0)
          dp[i * 2][j] = max(dp[i][j], dp[i][j - W[i * 2]] + V[i * 2]);
        else
          dp[i * 2][j] = dp[i][j];
      }
      if (i * 2 + 1 <= N) {
        if (j - W[i * 2 + 1] >= 0)
          dp[i * 2 + 1][j] =
              max(dp[i][j], dp[i][j - W[i * 2 + 1]] + V[i * 2 + 1]);
        else
          dp[i * 2 + 1][j] = dp[i][j];
      }
    }
  }

  // cout << "dp[2]" << endl;
  // REP(i,300){
  //     // dp[2][*]
  //     cout << i << " " << dp[2][i] << endl;
  // }
  // cout << "dp[4]" << endl;
  // REP(i,300){
  //     // dp[4][*]
  //     cout << i << " " << dp[4][i] << endl;
  // }

  // cout << "dp[9]" << endl;
  // REP(i,300){
  //     // dp[9][*]
  //     cout << i << " " << dp[9][i] << endl;
  // }

  int Q;
  cin >> Q;
  REP(_, Q) {
    LL v, L;
    cin >> v >> L;
    if (v < (1 << 9)) {
      cout << dp[v][L] << "\n";
    } else {
      VL tv, tw;
      while (v >= (1 << 9)) {
        tv.eb(V[v]);
        tw.eb(W[v]);
        v /= 2;
      }
      int sz = SZ(tv);
      LL ans = 0;
      for (int mask = 0; mask < (1 << sz); mask++) {
        LL nv = 0, nw = 0;
        REP(i, sz) {
          if ((mask >> i) & 1) {
            nv += tv[i];
            nw += tw[i];
          }
        }
        if (nw > L)
          continue;
        chmax(ans, nv + dp[v][L - nw]);
      }
      cout << ans << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define equals(a, b) (fabs((a) - (b)) < EPS)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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;
}

template <typename T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

template <typename T> void print_array(const T &ary, int size) {
  REP(i, size) { cout << ary[i] << " "; }
  cout << endl;
}

const int mod = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(12);

  int N;
  cin >> N;
  VL V(N + 10), W(N + 10);
  REP(i, N) cin >> V[i + 1] >> W[i + 1];

  VVL dp((1 << 9), VL(100010));
  FOR(j, W[1], 100010) dp[1][j] = V[1];
  FOR(i, 1, (1 << 8)) {
    if (i > N)
      break;
    REP(j, 100010) {
      if (i * 2 <= N) {
        if (j - W[i * 2] >= 0)
          dp[i * 2][j] = max(dp[i][j], dp[i][j - W[i * 2]] + V[i * 2]);
        else
          dp[i * 2][j] = dp[i][j];
      }
      if (i * 2 + 1 <= N) {
        if (j - W[i * 2 + 1] >= 0)
          dp[i * 2 + 1][j] =
              max(dp[i][j], dp[i][j - W[i * 2 + 1]] + V[i * 2 + 1]);
        else
          dp[i * 2 + 1][j] = dp[i][j];
      }
    }
  }

  // cout << "dp[2]" << endl;
  // REP(i,300){
  //     // dp[2][*]
  //     cout << i << " " << dp[2][i] << endl;
  // }
  // cout << "dp[4]" << endl;
  // REP(i,300){
  //     // dp[4][*]
  //     cout << i << " " << dp[4][i] << endl;
  // }

  // cout << "dp[9]" << endl;
  // REP(i,300){
  //     // dp[9][*]
  //     cout << i << " " << dp[9][i] << endl;
  // }

  int Q;
  cin >> Q;
  REP(_, Q) {
    LL v, L;
    cin >> v >> L;
    if (v < (1 << 9)) {
      cout << dp[v][L] << "\n";
    } else {
      VL tv, tw;
      while (v >= (1 << 9)) {
        tv.eb(V[v]);
        tw.eb(W[v]);
        v /= 2;
      }
      int sz = SZ(tv);
      LL ans = 0;
      for (int mask = 0; mask < (1 << sz); mask++) {
        LL nv = 0, nw = 0;
        REP(i, sz) {
          if ((mask >> i) & 1) {
            nv += tv[i];
            nw += tw[i];
          }
        }
        if (nw > L)
          continue;
        chmax(ans, nv + dp[v][L - nw]);
      }
      cout << ans << endl;
    }
  }
}