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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

long long int N;
int Q, v[100005], L[100005];
long long int V[300005], W[300005];

set<long long int> S;

long long int dp[2060][100005];

int max_level;

int node[40];

void visit(int k, int level) {

  if (level > max_level / 2) {
    return;
  }

  int p = k / 2;

  for (int j = 1; j <= 100000; j++) {
    dp[k][j] = dp[k][j - 1];
    dp[k][j] = max(dp[k][j], dp[p][j]);
    if (j - W[k] >= 0) {
      dp[k][j] = max(dp[k][j], dp[p][j - W[k]] + V[k]);
    }
  }

  visit(k * 2, level + 1);
  visit(k * 2 + 1, level + 1);
}
int calc_level(long long int v) {
  int cnt = 0;
  while (v > 0) {
    cnt++;
    v /= 2;
  }
  return cnt;
}
int main(void) {

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }

  cin >> Q;

  for (int i = 1; i <= Q; i++) {
    cin >> v[i] >> L[i];
  }

  for (int i = 1; i <= N; i++) {
    int tmp = v[i];
    int cnt = 0;
    while (tmp > 0) {
      cnt++;
      tmp /= 2;
    }
    max_level = max(max_level, cnt);
  }

  visit(1, 1);

  for (int i = 1; i <= Q; i++) {
    int lv = calc_level(v[i]);
    if (lv <= max_level / 2) {
      cout << dp[v[i]][L[i]] << endl;
    } else {
      long long int ans = 0;
      int H = lv - max_level / 2;
      int tmp = v[i];
      for (int i = 1; i <= H; i++) {
        node[i] = tmp;
        tmp /= 2;
      }

      for (long long int k = 0; k < (1LL << H); k++) {
        long long int sum_v = 0;
        long long int sum_w = 0;

        for (int j = 1; j <= H; j++) {
          if ((k >> (j - 1)) & 1) {
            sum_v += V[node[j]];
            sum_w += W[node[j]];
          }
        }

        if (sum_w > L[i]) {
          continue;
        }

        ans = max(ans, dp[tmp][L[i] - sum_w] + sum_v);
      }
      cout << ans << endl;
    }
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

long long int N;
int Q, V[300005], W[300005], v[100005], L[100005];

set<long long int> S;

long long int dp[1050][200005];

int max_level;

int node[40];

void visit(int k, int level) {

  if (level > max_level / 2) {
    return;
  }

  int p = k / 2;

  for (int j = 1; j <= 100000; j++) {
    dp[k][j] = dp[k][j - 1];
    dp[k][j] = max(dp[k][j], dp[p][j]);
    if (j - W[k] >= 0) {
      dp[k][j] = max(dp[k][j], dp[p][j - W[k]] + V[k]);
    }
  }

  visit(k * 2, level + 1);
  visit(k * 2 + 1, level + 1);
}
int calc_level(long long int v) {
  int cnt = 0;
  while (v > 0) {
    cnt++;
    v /= 2;
  }
  return cnt;
}
int main(void) {

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }

  cin >> Q;

  for (int i = 1; i <= Q; i++) {
    cin >> v[i] >> L[i];
  }

  for (int i = 1; i <= N; i++) {
    int tmp = v[i];
    int cnt = 0;
    while (tmp > 0) {
      cnt++;
      tmp /= 2;
    }
    max_level = max(max_level, cnt);
  }

  visit(1, 1);

  for (int i = 1; i <= Q; i++) {
    int lv = calc_level(v[i]);
    if (lv <= max_level / 2) {
      cout << dp[v[i]][L[i]] << endl;
    } else {
      long long int ans = 0;
      int H = lv - max_level / 2;
      int tmp = v[i];
      for (int i = 1; i <= H; i++) {
        node[i] = tmp;
        tmp /= 2;
      }

      for (long long int k = 0; k < (1LL << H); k++) {
        long long int sum_v = 0;
        long long int sum_w = 0;

        for (int j = 1; j <= H; j++) {
          if ((k >> (j - 1)) & 1) {
            sum_v += V[node[j]];
            sum_w += W[node[j]];
          }
        }

        if (sum_w > L[i]) {
          continue;
        }

        ans = max(ans, dp[tmp][L[i] - sum_w] + sum_v);
      }
      cout << ans << endl;
    }
  }

  return 0;
}