Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define ll long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 998244353;
const int INF = 1e9;
const ll INFLONG = 1e18;

vector<vector<ll>> kn(pow(2, 9), vector<ll>(100001, 0));
vector<pair<ll, ll>> vec(1000000);
vector<ll> ans(100000, 0);
ll n;

void dfs(ll k) {
  if (k >= min((ll)pow(2, 9), n)) {
    return;
  }
  if (k == 0) {
    REP(i, vec[k].first, 100001) { kn[k][i] = vec[k].second; }
  } else {
    rep(i, 100001) {
      kn[k][i] = kn[(k - 1) / 2][i];
      if (i >= vec[k].first) {
        chmax(kn[k][i], kn[(k - 1) / 2][i - vec[k].first] + vec[k].second);
      }
    }
  }
  dfs(k * 2 + 1);
  dfs(k * 2 + 2);
}

void calc(ll v, ll l, ll score, ll num) {
  if (l < 0) {
    return;
  }
  if (v <= (ll)pow(2, 9)) {
    chmax(ans[num], kn[v][l] + score);
  } else {
    calc((v - 1) / 2, l, score, num);
    calc((v - 1) / 2, l - vec[v].first, score + vec[v].second, num);
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    cin >> t1 >> t2;
    vec[i].first = t2; // weight
    vec[i].second = t1;
  }
  dfs(0);
  ll q;
  cin >> q;
  rep(i, q) {
    cin >> t1 >> t2;
    t1--;
    calc(t1, t2, 0, i);
  }
  rep(i, q) { cout << ans[i] << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define ll long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 998244353;
const int INF = 1e9;
const ll INFLONG = 1e18;

vector<vector<ll>> kn(pow(2, 9), vector<ll>(100001, 0));
vector<pair<ll, ll>> vec(1000000);
vector<ll> ans(100000, 0);
ll n;

void dfs(ll k) {
  if (k >= min((ll)pow(2, 9), n)) {
    return;
  }
  if (k == 0) {
    REP(i, vec[k].first, 100001) { kn[k][i] = vec[k].second; }
  } else {
    rep(i, 100001) {
      kn[k][i] = kn[(k - 1) / 2][i];
      if (i >= vec[k].first) {
        chmax(kn[k][i], kn[(k - 1) / 2][i - vec[k].first] + vec[k].second);
      }
    }
  }
  dfs(k * 2 + 1);
  dfs(k * 2 + 2);
}

void calc(ll v, ll l, ll score, ll num) {
  if (l < 0) {
    return;
  }
  if (v < (ll)pow(2, 9)) {
    chmax(ans[num], kn[v][l] + score);
  } else {
    calc((v - 1) / 2, l, score, num);
    calc((v - 1) / 2, l - vec[v].first, score + vec[v].second, num);
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    cin >> t1 >> t2;
    vec[i].first = t2; // weight
    vec[i].second = t1;
  }
  dfs(0);
  ll q;
  cin >> q;
  rep(i, q) {
    cin >> t1 >> t2;
    t1--;
    calc(t1, t2, 0, i);
  }
  rep(i, q) { cout << ans[i] << endl; }
}