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

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repp(i, n, m) for (int(i) = (n); (i) < (m); (i)++)
#define repn(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long
#define ulint unsigned long long
#define ldou long double
#define fi first
#define se second
#define setpre(x) std::cout << fixed << setprecision(x)
#define ii(x)                                                                  \\
  int x;                                                                       \\
  cin >> (x)
#define ii2(x, y)                                                              \\
  int x, y;                                                                    \\
  cin >> (x) >> (y)
#define ii3(x, y, z)                                                           \\
  int x, y, z;                                                                 \\
  cin >> (x) >> (y) >> (z)
#define out(x) cout << (x) << endl
#define outs(x) cout << (x) << " "
#define yn(x) cout << ((x) ? ("Yes") : ("No")) << endl
#define YN(x) cout << ((x) ? ("YES") : ("NO")) << endl
#define bit_c(x) __builtin_popcountll(x)

inline void logger() { std::cout << " [LOGGER] " << endl; }
template <typename A, typename... B> void logger(const A &a, const B &...b) {
  cout << a << " , ";
  logger(b...);
}

typedef pair<lint, lint> P;
const lint MOD = 1000000007;
const lint MOD9 = 998244353;
const lint INF = MOD * MOD;
const int MAX = 100005;

/* ...o(^-^)o... */

int dp[1 << 10][MAX];

int main() {
  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<int> u(q), l(q);
  rep(i, q) cin >> u[i] >> l[i];

  int k = 0;
  while (n > 0) {
    n /= 2;
    k++;
  }
  k /= 2;
  repn(i, 1 << k) dp[i][0] = 0;
  rep(x, MAX) dp[0][x] = 0;

  repn(i, 1 << k) repn(x, 100000) {
    dp[i][x] = max(dp[i / 2][x], dp[i][x - 1]);
    if (x - w[i] >= 0)
      dp[i][x] = max(dp[i][x], dp[i / 2][x - w[i]] + v[i]);
  }

  rep(i, q) {
    vector<int> s;
    while (u[i] >= (1 << k)) {
      s.push_back(u[i]);
      u[i] /= 2;
    }
    int res = 0;
    rep(j, 1 << (s.size())) {
      int sv = 0, sw = 0;
      rep(d, s.size()) {
        if (j & (1 << d)) {
          sv += v[s[d]];
          sw += w[s[d]];
        }
      }
      if (sw > l[i])
        continue;
      res = max(res, sv + dp[u[i]][l[i] - sw]);
    }
    out(res);
  }
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repp(i, n, m) for (int(i) = (n); (i) < (m); (i)++)
#define repn(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long
#define ulint unsigned long long
#define ldou long double
#define fi first
#define se second
#define setpre(x) std::cout << fixed << setprecision(x)
#define ii(x)                                                                  \\
  int x;                                                                       \\
  cin >> (x)
#define ii2(x, y)                                                              \\
  int x, y;                                                                    \\
  cin >> (x) >> (y)
#define ii3(x, y, z)                                                           \\
  int x, y, z;                                                                 \\
  cin >> (x) >> (y) >> (z)
#define out(x) cout << (x) << endl
#define outs(x) cout << (x) << " "
#define yn(x) cout << ((x) ? ("Yes") : ("No")) << endl
#define YN(x) cout << ((x) ? ("YES") : ("NO")) << endl
#define bit_c(x) __builtin_popcountll(x)

inline void logger() { std::cout << " [LOGGER] " << endl; }
template <typename A, typename... B> void logger(const A &a, const B &...b) {
  cout << a << " , ";
  logger(b...);
}

typedef pair<lint, lint> P;
const lint MOD = 1000000007;
const lint MOD9 = 998244353;
const lint INF = MOD * MOD;
const int MAX = 100005;

/* ...o(^-^)o... */

int main() {
  int n;
  cin >> n;
  int k = 1;
  while (k < n)
    k *= 2;
  k /= 2;

  vector<int> v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];

  int q;
  cin >> q;
  vector<int> u(q), l(q);
  rep(i, q) cin >> u[i] >> l[i];

  vector<vector<int>> dp(1 << k, vector<int>(MAX));
  rep(i, 1 << k) repn(x, 100000) {
    dp[i][x] = max(dp[(i - 1) / 2][x], dp[i][x - 1]);
    if (x - w[i] >= 0)
      dp[i][x] = max(dp[i][x], dp[(i - 1) / 2][x - w[i]] + v[i]);
  }

  rep(i, q) {
    vector<int> s;
    while (u[i] >= (1 << k)) {
      s.push_back(u[i]);
      u[i] /= 2;
    }
    int res = 0;
    rep(j, 1 << (s.size())) {
      int sv = 0, sw = 0;
      rep(d, s.size()) {
        if (j & (1 << d)) {
          sv += v[s[j]];
          sw += w[s[j]];
        }
      }
      if (sw > l[i])
        continue;
      res = max(res, sv + dp[u[i]][l[i] - sw]);
    }
    out(res);
  }
}