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>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int val[1000005], wgt[1000005];
vector<vector<int>> adj;
int x[1000005], y[1000005];
const int lim = 1e5;
int dp[15][100005];
vector<pair<int, int>> pp[1000005];
int ans[100005];

void dfs(int ver, int xx) {
  if (xx < 10) {
    for (int j = 0; j <= lim; j++) {
      dp[xx][j] = dp[xx - 1][j];
      if (j - wgt[ver] >= 0)
        dp[xx][j] = max(dp[xx][j], dp[xx - 1][j - wgt[ver]] + val[ver]);
    }
    for (int j = 1; j <= lim; j++)
      dp[xx][j] = max(dp[xx][j], dp[xx][j - 1]);
    for (auto i : pp[ver]) {
      ans[i.second] = dp[xx][i.first];
    }
    for (auto i : adj[ver]) {
      dfs(i, xx + 1);
    }
  } else {
    int tt = xx - 10;
    int z = (1 << tt);
    const int fi = (1 << (tt + 1));
    for (int i = z; i < fi; i++) {
      x[i] += wgt[ver] + x[i - z];
      y[i] += val[ver] + y[i - z];
    }

    for (auto i : pp[ver]) {
      for (int j = 0; j < fi; j++) {
        if (x[j] <= i.first)
          ans[i.second] = max(ans[i.second], y[j] + dp[9][i.first - x[j]]);
      }
    }

    for (auto i : adj[ver]) {
      dfs(i, xx + 1);
    }

    for (int i = z; i < fi; i++) {
      x[i] = 0;
      y[i] = 0;
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif

  int n;
  cin >> n;
  adj.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> val[i] >> wgt[i];
  for (int i = 2; i <= n; i++) {
    adj[i / 2].push_back(i);
  }

  int q;
  cin >> q;
  int xx, yy;
  for (int i = 0; i < q; i++) {
    cin >> xx >> yy;
    pp[xx].push_back({yy, i});
  }
  dfs(1, 1);
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int val[1000005], wgt[1000005];
vector<vector<int>> adj;
int x[1000005], y[1000005];
const int lim = 1e5;
int dp[15][100004];
vector<pair<int, int>> pp[1000005];
int ans[100005];

void dfs(int ver, int xx) {
  if (xx < 10) {
    for (int j = 0; j <= lim; j++) {
      dp[xx][j] = dp[xx - 1][j];
      if (j - wgt[ver] >= 0)
        dp[xx][j] = max(dp[xx][j], dp[xx - 1][j - wgt[ver]] + val[ver]);
    }
    for (int j = 1; j <= lim; j++)
      dp[xx][j] = max(dp[xx][j], dp[xx][j - 1]);
    for (auto i : pp[ver]) {
      ans[i.second] = dp[xx][i.first];
    }
  } else {
    int tt = xx - 10;
    int z = (1 << tt);
    for (int i = (1 << tt); i < (1 << (tt + 1)); i++) {
      x[i] += wgt[ver] + x[i - z];
      y[i] += val[ver] + y[i - z];
    }
    const int fi = (1 << (tt + 1));
    for (auto i : pp[ver]) {
      for (int j = 0; j < fi; j++) {
        if (x[j] <= i.first)
          ans[i.second] = max(ans[i.second], y[j] + dp[9][i.first - x[j]]);
      }
    }
  }
  for (auto i : adj[ver]) {
    dfs(i, xx + 1);
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif

  int n;
  cin >> n;
  adj.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> val[i] >> wgt[i];
  for (int i = 2; i <= n; i++) {
    adj[i / 2].push_back(i);
  }

  int q;
  cin >> q;
  int xx, yy;
  for (int i = 0; i < q; i++) {
    cin >> xx >> yy;
    pp[xx].push_back({yy, i});
  }
  dfs(1, 1);
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}