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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
#define vec vector<ll>
#define mat vector<vector<ll>>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 998244353;
signed main() {
  ll n;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];
  ll q;
  cin >> q;
  if (n < (1LL << 9)) {
    while (q) {
      q--;
      ll u, l;
      cin >> u >> l;
      ll ans = 0;
      vector<ll> b(0);
      while (u) {
        b.pb(u);
        u /= 2;
      }
      ll m = b.size();
      rep(i, 1LL << m) {
        ll cnt = 0;
        ll val = 0;
        rep(j, m) {
          if ((1LL << j) & i) {
            ll idx = b[j];
            cnt += w[idx];
            val += v[idx];
          }
        }
        if (cnt <= l) {
          ans = max(ans, val);
        }
      }
      cout << ans << endl;
    }
  } else {
    vector<vector<ll>> dp(1LL << 9, vector<ll>(100010, -1));
    dp[1][0] = 0;
    REP(i, 1, 1LL << 9) {
      for (int j = 100000; j >= 0; j--) {
        if (dp[i][j] < 0)
          continue;
        if (j + w[i] <= 100010) {
          dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[i][j] + v[i]);
        }
      }
      rep(j, 100010) {
        if (i < (1LL << 8)) {
          dp[i * 2][j] = dp[i][j];
          dp[i * 2 + 1][j] = dp[i][j];
        }
      }
    }
    REP(i, 1, 1LL << 9) {
      ll ma = 0;
      rep(j, 100010) {
        ma = max(ma, dp[i][j]);
        dp[i][j] = ma;
      }
    }
    while (q) {
      q--;
      ll u, l;
      cin >> u >> l;
      vector<ll> b(0);
      ll ans = 0;
      if (u < (1LL << 9)) {
        cout << dp[u][l] << endl;
        continue;
      }
      while (u >= (1LL << 9)) {
        b.pb(u);
        u /= 2;
      }
      ll m = b.size();
      rep(i, 1LL << m) {
        ll cnt = 0;
        ll val = 0;
        rep(j, m) {
          if ((1LL << j) & i) {
            ll idx = b[j];
            cnt += w[idx];
            val += v[idx];
          }
        }
        if (cnt <= l) {
          ans = max(ans, dp[u][l - cnt] + val);
        }
      }
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
#define vec vector<ll>
#define mat vector<vector<ll>>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 998244353;
signed main() {
  // 入力
  ll n;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];
  ll q;
  cin >> q;
  if (n < (1LL << 9)) {
    while (q) {
      q--;
      ll u, l;
      cin >> u >> l;
      ll ans = 0;
      vector<ll> b(0);
      while (u > 0) {
        b.pb(u);
        u /= 2;
      }
      ll m = b.size();
      rep(i, 1LL << m) {
        ll cnt = 0;
        ll val = 0;
        rep(j, m) {
          if ((1LL << j) & i) {
            ll idx = b[j];
            cnt += w[idx];
            val += v[idx];
          }
        }
        if (cnt <= l) {
          ans = max(ans, val);
        }
      }
      cout << ans << endl;
    }
  } else {
    vector<vector<ll>> dp(1LL << 9, vector<ll>(100010));
    REP(i, 1, 1LL << 8) {
      rep(j, 100010) {
        if (j + w[i] <= 100010) {
          dp[i * 2][j + w[i]] = max(dp[i * 2][j + w[i]], dp[i][j] + v[i]);
          dp[i * 2 + 1][j + w[i]] =
              max(dp[i * 2 + 1][j + w[i]], dp[i][j] + v[i]);
        }
        dp[i * 2][j] = max(dp[i * 2][j], dp[i][j]);
        dp[i * 2 + 1][j] = max(dp[i * 2 + 1][j], dp[i][j]);
      }
    }
    REP(i, 1, 1LL << 9) {
      ll ma = 0;
      rep(j, 100010) {
        ma = max(ma, dp[i][j]);
        dp[i][j] = ma;
      }
    }
    while (q) {
      q--;
      ll u, l;
      cin >> u >> l;
      vector<ll> b(0);
      ll ans = 0;
      if (u < (1LL << 9)) {
        cout << dp[u][l] << endl;
        continue;
      }
      while (u >= (1LL << 9)) {
        b.pb(u);
        u /= 2;
      }
      ll m = b.size();
      rep(i, 1LL << m) {
        ll cnt = 0;
        ll val = 0;
        rep(j, n) {
          if ((1LL << j) & i) {
            ll idx = b[j];
            cnt += w[idx];
            val += v[idx];
          }
        }
        if (cnt <= l) {
          ans = max(ans, dp[u][l - cnt] + val);
        }
      }
      cout << ans << endl;
    }
  }
}