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

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define mex 1000005
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
// vector<int> adj[mex];
// a+b=a^b+2*(a&b)
int K[512][100001];

int main() {
  fast int n;
  cin >> n;
  int val[n], wt[n];
  for (int i = 0; i < n; i++)
    cin >> val[i] >> wt[i];
  int W = 100001;

  int n1 = min((1 << 9) - 1, n);
  // vector<vector<int>> K(n+1,vector<int> (W+1));

  for (int i = 0; i <= n1; i++) {
    for (int w = 0; w <= W; w++) {
      if (i == 0 || w == 0)
        K[i][w] = 0;
      else if (wt[i - 1] <= w)
        K[i][w] = max(val[i - 1] + K[i / 2][w - wt[i - 1]], K[i / 2][w]);
      else
        K[i][w] = K[i / 2][w];
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    if (v < ((1 << 9) - 1))
      cout << K[v][l] << "\\n";
    else {
      int l1 = 1, x1 = 2;
      while (x1 <= v) {
        x1 = x1 * 2;
        l1 += 1;
      }
      ll ans = 0;
      for (int i = 0; i < (1 << (l1 - 9)); i++) {
        int nn = v;
        ll s1 = 0, s2 = 0;
        for (int j = 0; j < (l1 - 9); j++) {
          if (i & (1LL << j)) {
            s1 = s1 + wt[nn - 1];
            s2 = s2 + val[nn - 1];
          }
          nn = nn / 2;
        }
        if (s1 > l)
          continue;
        else
          ans = max(ans, s2 + K[nn][l - s1]);
      }
      cout << ans << "\\n";
    }
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define mex 1000005
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
// vector<int> adj[mex];
// a+b=a^b+2*(a&b)
int K[512][100001];

int main() {
  fast int n;
  cin >> n;
  int val[n], wt[n];
  for (int i = 0; i < n; i++)
    cin >> val[i] >> wt[i];
  int W = 100001;

  int n1 = min((1 << 9) - 1, n);
  // vector<vector<int>> K(n+1,vector<int> (W+1));

  for (int i = 0; i <= n1; i++) {
    for (int w = 0; w <= W; w++) {
      if (i == 0 || w == 0)
        K[i][w] = 0;
      else if (wt[i - 1] <= w)
        K[i][w] = max(val[i - 1] + K[i / 2][w - wt[i - 1]], K[i / 2][w]);
      else
        K[i][w] = K[i / 2][w];
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    if (v <= ((1 << 9) - 1))
      cout << K[v][l] << endl;
    else {
      int l1 = 1, x1 = 2;
      while (x1 < v) {
        x1 = x1 * 2;
        l1 += 1;
      }
      ll ans = 0;
      for (int i = 0; i < (1 << (l1 - 9)); i++) {
        int nn = v;
        ll s1 = 0, s2 = 0;
        for (int j = 0; j < (l1 - 9); j++) {
          if (i & (1 << j)) {
            s1 = s1 + wt[nn - 1];
            s2 = s2 + val[nn - 1];
          }
          nn = nn / 2;
        }
        if (s1 > l)
          continue;
        else
          ans = max(ans, s2 + K[nn][l - s1]);
      }
      cout << ans << endl;
    }
  }
}