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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18 + 1
#define eps 0.000000000001
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> // usage rand<long long>()
T rand() {
  return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

//////////////
auto clk = clock();
#define all(x) x.begin(), x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int, int>
/////////////
int va[300001][10] = {};
int v[300001];
int w[300001];
int ans[300001];
int n;
vector<pair<int, int>> qu[300001];
void dfs2(int i, vector<int> &vv) {
  if (i > n)
    return;
  vv.push_back(i);
  dfs2(2 * i + 1, vv);
  dfs2(2 * i, vv);
  if (qu[i].size())
    for (int j = 0; j < (1 << vv.size()); ++j) {
      int tw = 0, tva = 0;
      for (int k = 0; k < vv.size(); ++k) {
        if (j & (1 << k)) {
          tw += w[vv[k]];
          tva += v[vv[k]];
        }
      }
      for (auto r : qu[i]) {
        if (r.F >= tw)
          ans[r.S] = max(ans[r.S], va[r.F - tw][9] + tva);
      }
    }
  vv.pop_back();
}
void dfs(int i, int lv) {
  if (i > n)
    return;
  if (lv > 9) {
    vector<int> vv;
    dfs2(i, vv);
  } else {
    for (int j = 0; j <= 100000; ++j) {
      va[j][lv] = va[j][lv - 1];
      if (j > 0)
        va[j][lv] = max(va[j][lv], va[j][lv - 1]);
      if (j - w[i] >= 0)
        va[j][lv] = max(va[j][lv - 1], va[j - w[i]][lv - 1] + v[i]);
    }
    for (auto r : qu[i]) {
      ans[r.S] = va[r.F][lv];
    }
    dfs(2 * i, lv + 1);
    dfs(2 * i + 1, lv + 1);
  }
}
signed main() {
  fastio;
#ifdef ANIKET_GOYAL
  freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int no, l;
    cin >> no >> l;
    qu[no].push_back({l, i});
  }
  dfs(1, 1);
  for (int i = 1; i <= q; ++i) {
    cout << ans[i] << "\\n";
  }
#ifdef ANIKET_GOYAL
  // cout<<endl<<endl<<endl<<endl<<"Time elapsed:
  // "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18 + 1
#define eps 0.000000000001
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> // usage rand<long long>()
T rand() {
  return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

//////////////
auto clk = clock();
#define all(x) x.begin(), x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int, int>
/////////////
int va[100001][10] = {};
int v[100001];
int w[100001];
int ans[100001];
int n;
vector<pair<int, int>> qu[100001];
void dfs2(int i, vector<int> &vv) {
  if (i > n)
    return;
  vv.push_back(i);
  dfs2(2 * i + 1, vv);
  dfs2(2 * i, vv);
  if (qu[i].size())
    for (int j = 0; j < (1 << vv.size()); ++j) {
      int tw = 0, tva = 0;
      for (int k = 0; k < vv.size(); ++k) {
        if (j & (1 << k)) {
          tw += w[vv[k]];
          tva += v[vv[k]];
        }
      }
      for (auto r : qu[i]) {
        if (r.F >= tw)
          ans[r.S] = max(ans[r.S], va[r.F - tw][9] + tva);
      }
    }
  vv.pop_back();
}
void dfs(int i, int lv) {
  if (i > n)
    return;
  if (lv > 9) {
    vector<int> vv;
    dfs2(i, vv);
  } else {
    for (int j = 0; j <= 100000; ++j) {
      va[j][lv] = va[j][lv - 1];
      if (j > 0)
        va[j][lv] = max(va[j][lv], va[j][lv - 1]);
      if (j - w[i] >= 0)
        va[j][lv] = max(va[j][lv - 1], va[j - w[i]][lv - 1] + v[i]);
    }
    for (auto r : qu[i]) {
      ans[r.S] = va[r.F][lv];
    }
    dfs(2 * i, lv + 1);
    dfs(2 * i + 1, lv + 1);
  }
}
signed main() {
  fastio;
#ifdef ANIKET_GOYAL
  freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int no, l;
    cin >> no >> l;
    qu[no].push_back({l, i});
  }
  dfs(1, 1);
  for (int i = 1; i <= q; ++i) {
    cout << ans[i] << "\\n";
  }
#ifdef ANIKET_GOYAL
  // cout<<endl<<endl<<endl<<endl<<"Time elapsed:
  // "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}