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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

const Int Nmax = 1 << 9;

int main() {

  Int N;
  cin >> N;
  vector<Int> V(N), W(N);
  rep(i, N) cin >> V[i] >> W[i];
  Int Q;
  cin >> Q;
  vector<Int> v(Q), L(Q);
  Int Lmax = 0;
  rep(i, Q) {
    cin >> v[i] >> L[i];
    v[i]--;
    Lmax = max(Lmax, L[i]);
  }
  Lmax++;

  Int N2 = min(Nmax, N);
  vector<vector<Int>> dp(N2, vector<Int>(Lmax, 0));

  rep(i, N2) rep(j, Lmax) {
    if (i == 0) {
      if (j >= W[0])
        dp[i][j] = V[0];
    } else {
      int k = (i + 1) / 2 - 1;
      if (j >= W[i])
        dp[i][j] = max(dp[k][j], dp[k][j - W[i]] + V[i]);
      else
        dp[i][j] = dp[k][j];
    }
  }

  rep(i, Q) {
    if (v[i] < N2)
      cout << dp[v[i]][L[i]] << endl;
    else {
      vector<Int> seq(0);
      seq.push_back(v[i]);
      Int now = v[i];
      while (true) {
        now = (now + 1) / 2 - 1;
        seq.push_back(now);
        if (now < N2)
          break;
      }
      reverse(seq.begin(), seq.end());
      Int res = seq.size() - 1;
      Int ans = dp[seq[0]][L[i]];
      for (int bit = 1; bit < (1 << res); bit++) {
        Int sum = 0, val = 0;
        rep(j, res) {
          if (bit & (1 << j)) {
            sum += W[seq[j + 1]];
            val += V[seq[j + 1]];
          }
        }
        if (L[i] >= sum) {
          ans = max(ans, dp[seq[0]][L[i] - sum] + val);
        }
      }
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

const Int Nmax = 1 << 9;

int main() {

  Int N;
  cin >> N;
  vector<Int> V(N), W(N);
  rep(i, N) cin >> V[i] >> W[i];
  Int Q;
  cin >> Q;
  vector<Int> v(Q), L(Q);
  Int Lmax = 0;
  rep(i, Q) {
    cin >> v[i] >> L[i];
    v[i]--;
    Lmax = max(Lmax, L[i]);
  }
  Lmax++;

  Int N2 = min(Nmax, N);
  vector<vector<Int>> dp(N2, vector<Int>(Lmax, 0));

  rep(i, N2) rep(j, Lmax) {
    if (i == 0) {
      if (j >= W[0])
        dp[i][j] = V[0];
    } else {
      int k = (i + 1) / 2 - 1;
      if (j >= W[i])
        dp[i][j] = max(dp[k][j], dp[k][j - W[i]] + V[i]);
    }
  }

  rep(i, Q) {
    if (v[i] < N2)
      cout << dp[v[i]][L[i]] << endl;
    else {
      vector<Int> seq;
      seq.push_back(v[i]);
      Int now = v[i];
      while (true) {
        now = (now + 1) / 2 - 1;
        if (now < N2)
          break;
        seq.push_back(now);
      }
      Int res = seq.size();
      vector<vector<Int>> dp2(res, vector<Int>(Lmax, 0));
      rep(j, res) rep(k, Lmax) {
        if (j == 0) {
          int l = (seq[res - 1] + 1) / 2 - 1;
          if (k >= W[seq[res - 1]])
            dp2[0][k] =
                max(dp[l][j], dp[l][k - W[seq[res - 1]]] + V[seq[res - 1]]);
        } else {
          if (k >= W[seq[res - 1 - j]])
            dp2[j][k] = max(dp2[j - 1][k],
                            dp2[j - 1][k - W[seq[res - j]]] + V[seq[res - j]]);
        }
      }
      cout << dp2[res - 1][L[seq[0]]] << endl;
    }
  }
}