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

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define reps(i, a, b) for (int i = a; i < b; ++i)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];
  int k = 1;
  int p = 2;
  while (n >= p) {
    k++;
    p <<= 1;
  }
  int k2 = k / 2;
  const int maxl = 1e5;
  auto dp = make_vec<int>(1 << k2, maxl + 1);
  rep(i, 1 << k2) {
    rep(j, maxl + 1) {
      chmax(dp[i][j], dp[i / 2][j]);
      if (j != 0)
        chmax(dp[i][j], dp[i][j - 1]);
      if (j + w[i] <= maxl)
        chmax(dp[i][j + w[i]], dp[i / 2][j] + v[i]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int u, W;
    cin >> u >> W;
    int res = 0;
    rep(bit, 1 << (k - k2)) {
      int s = u;
      int noww = 0, nowv = 0;
      rep(i, k - k2) {
        if (bit >> i & 1) {
          noww += w[s];
          nowv += v[s];
        }
        s >>= 1;
      }
      if (noww <= W) {
        nowv += dp[s][W - noww];
        chmax(res, nowv);
      }
    }
    cout << res << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define reps(i, a, b) for (int i = a; i < b; ++i)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];
  int q;
  cin >> q;
  vector<vector<int>> dp(n + 1, vector<int>(100001));
  vector<bool> seen(n + 1);
  seen[0] = true;
  while (q--) {
    int u, W;
    cin >> u >> W;
    int s = u;
    vector<int> child;
    while (!seen[s]) {
      child.pb(s);
      s /= 2;
    }
    for (int i = sz(child) - 1; i >= 0; --i) {
      int k = child[i];
      rep(j, 100001) {
        chmax(dp[k][j], dp[k / 2][j]);
        if (j != 0)
          chmax(dp[k][j], dp[k][j - 1]);
        if (j + w[k] <= 100000)
          chmax(dp[k][j + w[k]], dp[k / 2][j] + v[k]);
      }
      seen[k] = true;
    }
    cout << dp[u][W] << endl;
  }
}