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

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int depth(int i) {
  if (i == 1)
    return 1;
  return 1 + depth(i / 2);
}

int N;
vector<int> V, W;

void input(void) {
  cin >> N;
  V.resize(N);
  W.resize(N);
  for (int i = 0; i < N; ++i)
    cin >> V[i] >> W[i];
}

vector<vector<int>> dp;

void dfs(int s, vector<int> &res, int dep) {
  if (s > N)
    return;
  if (dep > 9)
    return;
  dp[s] = res;
  for (int w = 0; w <= 100000; ++w)
    if (w >= W[s - 1])
      dp[s][w] = max(dp[s][w], res[w - W[s - 1]] + V[s - 1]);
  dfs(s * 2, dp[s], dep + 1);
  dfs(s * 2 + 1, dp[s], dep + 1);
}

void build(void) {
  dp.resize(N + 1);
  vector<int> dp(100001, 0);
  dfs(1, dp, 1);
}

vector<pair<int, int>> all_find(int st, int gl = 0) {
  vector<pair<int, int>> res(1, {0, 0});
  while (st > gl) {
    int v = V[st - 1], w = W[st - 1];
    vector<pair<int, int>> tmp = res;
    for (auto p : res)
      tmp.emplace_back(p.first + w, p.second + v);
    res = tmp;
    st /= 2;
  }
  return res;
}

void solve(int x, int L) {
  if (depth(x) <= 9) {
    cout << dp[x][L] << endl;
    return;
  }
  int _x = x;
  while (depth(_x) > 9)
    _x /= 2;
  auto tmp = all_find(x, _x);
  int res = 0;
  for (auto p : tmp)
    if (p.first <= L)
      res = max(res, p.second + dp[_x][L - p.first]);
  cout << res << endl;
}

int main(void) {
  input();
  build();
  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    solve(v, L);
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int depth(int i) {
  if (i == 1)
    return 1;
  return 1 + depth(i / 2);
}

int N;
vector<int> V, W;

void input(void) {
  cin >> N;
  V.resize(N);
  W.resize(N);
  for (int i = 0; i < N; ++i)
    cin >> V[i] >> W[i];
}

vector<vector<int>> dp;

void dfs(int s, vector<int> &res, int dep) {
  if (s > N)
    return;
  if (dep > 9)
    return;
  dp[s].resize(100001, 0);
  dp[s] = res;
  for (int w = 0; w <= 100000; ++w)
    if (w >= W[s - 1])
      dp[s][w] = max(dp[s][w], res[w - W[s - 1]] + V[s - 1]);
  dfs(s * 2, dp[s], dep + 1);
  dfs(s * 2 + 1, dp[s], dep + 1);
}

void build(void) {
  dp.resize(N + 1);
  vector<int> dp(100001, 0);
  dfs(1, dp, 1);
}

vector<pair<int, int>> all_find(int st, int gl = 0) {
  vector<pair<int, int>> res(1, {0, 0});
  while (st > gl) {
    int v = V[st - 1], w = W[st - 1];
    vector<pair<int, int>> tmp = res;
    for (auto p : res) {
      if (p.first + w > 100000)
        continue;
      tmp.emplace_back(p.first + w, p.second + v);
    }
    res = tmp;
    st /= gl;
  }
  return res;
}

void solve(int x, int L) {
  if (depth(x) <= 9) {
    cout << dp[x][L] << endl;
    return;
  }
  int _x = x;
  while (depth(_x) > 9)
    _x /= 2;
  auto tmp = all_find(x, _x);
  int res = 0;
  for (auto p : tmp)
    res = max(res, p.second + dp[_x][L - p.first]);
  cout << res << endl;
}

int main(void) {
  input();
  build();
  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    solve(v, L);
  }
  return 0;
}