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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;

int main() {
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> V[i + 1] >> W[i + 1];
  }
  int Q;
  cin >> Q;
  vector<int> sv(Q), L(Q);
  for (int i = 0; i < Q; i++) {
    cin >> sv[i] >> L[i];
  }

  int LMAX = 100005;
  int ten = 1 << 9;
  vector<vector<int>> dp(min(ten, N + 1), vector<int>(LMAX));

  for (int i = 0; i < W[1]; i++) {
    dp[1][i] = 0;
  }
  for (int i = W[1]; i < LMAX; i++) {
    dp[1][i] = V[1];
  }

  for (int v = 2; v < min(ten, N + 1); v++) {
    for (ll l = 0; l < LMAX; l++) {
      if (l - W[v] >= 0) {
        dp[v][l] = max(dp[v / 2][l], dp[v / 2][l - W[v]] + V[v]);
      } else {
        dp[v][l] = dp[v / 2][l];
      }
    }
  }

  int size;
  int vi;
  vector<int> CV;
  vector<int> CW;
  int VV, WW;
  int tmp;
  for (int q = 0; q < Q; q++) {
    if (sv[q] < ten) {
      printf("%d\\n", dp[sv[q]][L[q]]);
    } else {
      CV.clear();
      CW.clear();
      size = 0;
      vi = sv[q];
      while (vi >= ten) {
        CV.push_back(V[vi]);
        CW.push_back(W[vi]);
        vi = vi / 2;
        size++;
      }

      tmp = 0;
      for (int i = 0; i < (1 << size); i++) {
        VV = 0;
        WW = 0;
        for (int j = 0; j < size; j++) {
          if (i & (1 << j)) {
            VV += CV[j];
            WW += CW[j];
          }
        }
        if (L[q] - WW >= 0) {
          tmp = max(tmp, VV + dp[vi][L[q] - WW]);
        }
      }
      printf("%d\\n", tmp);
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;

int main() {
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> V[i + 1] >> W[i + 1];
  }
  int Q;
  cin >> Q;
  vector<int> sv(Q), L(Q);
  for (int i = 0; i < Q; i++) {
    cin >> sv[i] >> L[i];
  }

  int LMAX = 100005;
  vector<vector<int>> dp(N + 1, vector<int>(LMAX));

  for (int i = 0; i < W[1]; i++) {
    dp[1][i] = 0;
  }
  for (int i = W[1]; i < LMAX; i++) {
    dp[1][i] = V[1];
  }

  int ten = 1 << 9;
  for (int v = 2; v < min(ten, N + 1); v++) {
    for (ll l = 0; l < LMAX; l++) {
      if (l - W[v] >= 0) {
        dp[v][l] = max(dp[v / 2][l], dp[v / 2][l - W[v]] + V[v]);
      }
    }
  }

  int size;
  int vi;
  vector<int> CV;
  vector<int> CW;
  int VV, WW;
  int tmp;
  for (int q = 0; q < Q; q++) {
    if (sv[q] < ten) {
      printf("%d\\n", dp[sv[q]][L[q]]);
    } else {
      CV.clear();
      CW.clear();
      size = 0;
      vi = sv[q];
      while (vi >= ten) {
        CV.push_back(V[vi]);
        CW.push_back(W[vi]);
        vi = vi >> 1;
        size++;
      }

      tmp = 0;
      VV = 0;
      WW = 0;
      for (int i = 0; i < (1 << size); i++) {
        for (int j = 0; j < size; j++) {
          if (i & (1 << j)) {
            VV += CV[j];
            WW += CW[j];
          }
        }
        if (L[q] - WW >= 0) {
          tmp = max(tmp, VV + dp[vi][L[q] - WW]);
        }
      }
      printf("%d\\n", tmp);
    }
  }
}