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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

int dp_upper[1024][100001];
int par[1 << 18];

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

  int N;
  cin >> N;
  int NN = 1 << 18;
  int Lmax = 100000;
  vector<int> V(NN), W(NN);
  rep(i, N) { cin >> V[i + 1] >> W[i + 1]; }
  vector<vector<int>> V_list(NN + 1, vector<int>(0));
  vector<vector<int>> W_list(NN + 1, vector<int>(0));
  rep(i_, NN) {
    int i;
    i = i_ + 1;
    int p;
    p = i / 2;
    rep(j, V_list[p].size()) {
      V_list[i].push_back(V_list[p][j]);
      W_list[i].push_back(W_list[p][j]);
    }
    V_list[i].push_back(V[i]);
    W_list[i].push_back(W[i]);
  }

  rep(i_, 1024 - 1) {
    int i;
    i = i_ + 1;
    int K;
    K = V_list[i].size();
    rep(j, 1 << K) {
      int vs = 0;
      int ws = 0;
      rep(k, K) {
        if ((j >> k) & 1) {
          vs += V_list[i][k];
          ws += W_list[i][k];
        }
      }
      if (ws <= Lmax) {
        dp_upper[i][ws] = max(dp_upper[i][ws], vs);
      }
    }
    rep(w_, Lmax) {
      int w;
      w = w_ + 1;
      dp_upper[i][w] = max(dp_upper[i][w], dp_upper[i][w - 1]);
    }
  }

  memset(par, 0, sizeof(par));
  rep(i_, NN - 1024) {
    int i;
    i = i_ + 1024;
    int p;
    p = i / 2;
    if (par[p] == 0) {
      par[i] = p;
    } else {
      par[i] = par[p];
    }
  }
  int Q;
  cin >> Q;
  rep(q, Q) {
    int v, L;
    cin >> v >> L;
    if (v < 1024) {
      cout << dp_upper[v][L] << "\\n";
    } else {
      int ans = 0;
      int p;
      p = par[v];
      int K;
      K = V_list[v].size() - 10;
      rep(j, 1 << K) {
        int vs = 0;
        int ws = 0;
        rep(k, K) {
          if ((j >> k) & 1) {
            vs += V_list[v][k + 10];
            ws += W_list[v][k + 10];
          }
          if (ws <= L) {
            ans = max(ans, vs + dp_upper[p][L - ws]);
          }
        }
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

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

  int N;
  cin >> N;
  int NN = 1 << 18;
  int Lmax = 100000;
  vector<int> V(NN), W(NN);
  rep(i, N) { cin >> V[i + 1] >> W[i + 1]; }

  vector<vector<int>> V_list(NN + 1, vector<int>(0));
  vector<vector<int>> W_list(NN + 1, vector<int>(0));
  rep(i_, NN) {
    int i;
    i = i_ + 1;
    int p;
    p = i / 2;
    rep(j, V_list[p].size()) {
      V_list[i].push_back(V_list[p][j]);
      W_list[i].push_back(W_list[p][j]);
    }
    V_list[i].push_back(V[i]);
    W_list[i].push_back(W[i]);
  }
  int dp_upper[1024][Lmax + 1];
  rep(i_, 1024 - 1) {
    int i;
    i = i_ + 1;
    int K;
    K = V_list[i].size();
    rep(j, 1 << K) {
      int vs = 0;
      int ws = 0;
      rep(k, K) {
        if ((j >> k) & 1) {
          vs += V_list[i][k];
          ws += W_list[i][k];
        }
      }
      if (ws <= Lmax) {
        dp_upper[i][ws] = max(dp_upper[i][ws], vs);
      }
    }
    rep(w_, Lmax) {
      int w;
      w = w_ + 1;
      dp_upper[i][w] = max(dp_upper[i][w], dp_upper[i][w - 1]);
    }
  }
  int par[NN + 1];
  memset(par, 0, sizeof(par));
  rep(i_, NN - 1024) {
    int i;
    i = i_ + 1024;
    int p;
    p = i / 2;
    if (par[p] == 0) {
      par[i] = p;
    } else {
      par[i] = par[p];
    }
  }
  int Q;
  cin >> Q;
  rep(q, Q) {
    int v, L;
    cin >> v >> L;
    if (v < 1024) {
      cout << dp_upper[v][L] << "\\n";
    } else {
      int ans = 0;
      int p;
      p = par[v];
      int K;
      K = V_list[v].size() - 10;
      rep(j, 1 << K) {
        int vs = 0;
        int ws = 0;
        rep(k, K) {
          if ((j >> k) & 1) {
            vs += V_list[v][k + 10];
            ws += W_list[v][k + 10];
          }
          if (ws <= L) {
            ans = max(ans, vs + dp_upper[p][L - ws]);
          }
        }
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}