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

#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"

using namespace std;
using namespace __gnu_pbds;

#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)
int dp[514][100001], n;
vector<int> v(262144 + 1, 0), w(262144 + 1, 0);
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, q;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (i = 1; i <= min(n, 512); i++) {
    for (j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i / 2][j];
      if (w[i] <= j) {
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
      }
    }
  }
  cin >> q;
  while (q--) {
    int x, l, ans = 0;
    vector<int> cur;
    cin >> x >> l;
    while (x > 512) {
      cur.pb(x);
      x /= 2;
    }
    int len = cur.size();
    for (int i = 0; i < (1 << len); i++) {
      int cur_val = 0, cur_weight = 0;
      for (int j = 0; j < len; j++) {
        if (i & (1 << j)) {
          cur_val += v[cur[j]];
          cur_weight += w[cur[j]];
        }
      }
      if (cur_weight > l) {
        continue;
      }
      ans = max(ans, cur_val + dp[x][l - cur_weight]);
    }
    cout << ans << "\\n";
  }
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"

using namespace std;
using namespace __gnu_pbds;

#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)
int dp[1024][100000], n;
vector<int> v(100000 + 1, 0), w(100000 + 1, 0);
void dfs(ll x, ll level) {
  if (level > 9 || x > n) {
    return;
  }
  for (int j = 0; j <= 100000; j++) {
    if (w[x] > j) {
      dp[x][j] = dp[x / 2][j];
    } else {
      dp[x][j] = max(dp[x / 2][j], dp[x / 2][j - w[x]] + v[x]);
    }
  }
  dfs(2 * x, level + 1);
  dfs(2 * x + 1, level + 1);
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, q;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  dfs(1, 1);
  cin >> q;
  while (q--) {
    int x, l, ans = 0;
    vector<int> cur;
    cin >> x >> l;
    while (x > 512) {
      cur.pb(x);
      x /= 2;
    }
    int len = cur.size();
    for (int i = 0; i <= (1 << len) - 1; i++) {
      int cur_val = 0, cur_weight = 0;
      for (int j = 0; j < len; j++) {
        if (i & (1 << j)) {
          cur_val += v[cur[j]];
          cur_weight += w[cur[j]];
        }
      }
      if (cur_weight > l) {
        continue;
      }
      ans = max(ans, cur_val + dp[x][l - cur_weight]);
    }
    cout << ans << "\\n";
  }
}