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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using vs = vector<string>;
using vi = vector<ll>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int u = min(511, n);
  vi v(n), w(n);
  rep(i, n) { cin >> v[i] >> w[i]; }
  int q;
  cin >> q;
  ll ma = 0;
  vi vv(q), l(q);
  rep(i, q) {
    cin >> vv[i] >> l[i];
    --vv[i];
    ma = max(ma, l[i]);
  }

  vvi d(u, vi(ma + 1));
  rep(i, u) {
    if (!i) {
      rep(j, ma + 1) {
        if (j >= w[0])
          d[0][j] = v[0];
      }
    } else {
      int ch = (i - 1) / 2;
      rep(j, ma + 1) {
        if (j >= w[i]) {
          d[i][j] = max(d[ch][j], d[ch][j - w[i]] + v[i]);
        } else {
          d[i][j] = d[ch][j];
        }
      }
    }
  }

  rep(i, q) {
    if (vv[i] < u) {
      cout << d[vv[i]][l[i]] << '\\n';
    } else {
      int p = vv[i];
      vector<P> se(1, P(0, 0));
      while (p >= u) {
        int si = se.size();
        rep(j, si) { se.emplace_back(se[j].first + w[p], se[j].second + v[p]); }
        p = (p - 1) / 2;
      }
      ll ans = 0;
      rep(j, se.size()) {
        if (se[j].first > l[i])
          continue;
        ans = max(ans, se[j].second + d[p][l[i] - se[j].first]);
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using vs = vector<string>;
using vi = vector<ll>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int u = min((1 << 9) - 1, n);
  vi v(n), w(n);
  rep(i, n) { cin >> v[i] >> w[i]; }
  int q;
  cin >> q;
  ll ma = 0;
  vi vv(q), l(q);
  rep(i, q) {
    cin >> vv[i] >> l[i];
    --vv[i];
    ma = max(ma, l[i]);
  }

  vvi d(u, vi(ma + 1));
  rep(i, u) {
    if (!i) {
      rep(j, ma + 1) {
        if (j >= w[0])
          d[0][j] = v[0];
      }
    } else {
      int ch = (i - 1) / 2;
      rep(j, ma + 1) {
        if (j >= w[i]) {
          d[i][j] = max(d[ch][j], d[ch][j - w[i]] + v[i]);
        } else {
          d[i][j] = d[ch][j];
        }
      }
    }
  }

  rep(i, q) {
    if (vv[i] <= u) {
      cout << d[vv[i]][l[i]] << '\\n';
    } else {
      int p = vv[i];
      vector<P> se(1, P(0, 0));
      while (p >= u) {
        int si = se.size();
        rep(j, si) { se.emplace_back(se[j].first + w[i], se[j].second); }
        p = (p - 1) / 2;
      }
      ll ans = 0;
      rep(j, se.size()) {
        if (se[j].first > l[i])
          continue;
        ans = max(ans, se[j].second + d[p][l[i] - se[j].first]);
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}