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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \\
  REP(sz, vec.size())                                                          \\
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

ll n;
V<P<ll, ll>> vw;

vvll predp;
void precalc(ll i, vll &dp) {
  vll nxt(100001, 0);
  for (ll j = 0; j <= 100000; j++) {
    chmax(nxt[j], dp[j]);
    if (j + vw[i].second <= 100000)
      chmax(nxt[j + vw[i].second], dp[j] + vw[i].first);
  }
  predp[i] = nxt;
  ll left = 2 * i + 1;
  if (left < n && left < ((1LL << 10) - 1))
    precalc(left, nxt);
  ll right = 2 * i + 2;
  if (right < n && right < ((1LL << 10) - 1))
    precalc(right, nxt);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vw.resize(n);
  for (ll i = 0; i < n; i++)
    cin >> vw[i].first >> vw[i].second;
  predp.resize(min((1LL << 10) - 1, n));
  vll dp(100001, 0);
  precalc(0, dp);

  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll v, l;
    cin >> v >> l;
    v--;
    if (v < ((1LL << 10) - 1)) {
      cout << predp[v][l] << '\\n';
    } else {
      V<P<ll, ll>> cand;
      V<int> nodes;
      while (v >= ((1LL << 10) - 1)) {
        cand.emplace_back(vw[v]);
        v = (v - 1) >> 1;
      }
      int sz = cand.size();
      ll ans = predp[v][l];
      for (ll state = 0; state < (1LL << sz); state++) {
        ll tmpw = 0, tmpv = 0;
        for (int bit = 0; bit < sz; bit++) {
          if ((state >> bit) & 1) {
            tmpv += cand[bit].first;
            tmpw += cand[bit].second;
          }
        }
        if (tmpw <= l) {
          chmax(ans, predp[v][l - tmpw] + tmpv);
        }
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \\
  REP(sz, vec.size())                                                          \\
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

ll n;
V<P<ll, ll>> vw;

vvll predp;
void precalc(ll i, ll p) {
  for (ll j = 0; j <= 100000; j++) {
    chmax(predp[i][j], predp[p][j]);
    if (j + vw[i].second <= 100000)
      chmax(predp[i][j + vw[i].second], predp[p][j] + vw[i].first);
  }
  ll left = 2 * i + 1;
  if (left < n && left < ((1LL << 10) - 1))
    precalc(left, i);
  ll right = 2 * i + 2;
  if (right < n && right < ((1LL << 10) - 1))
    precalc(right, i);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vw.resize(n);
  for (ll i = 0; i < n; i++)
    cin >> vw[i].first >> vw[i].second;
  predp.resize(min((1LL << 10) - 1, n), vll(100001, 0));
  for (ll i = vw[0].second; i <= 100000; i++)
    predp[0][i] = vw[0].first;
  precalc(1, 0);
  precalc(2, 0);

  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll v, l;
    cin >> v >> l;
    v--;
    if (v < ((1LL << 10) - 1)) {
      cout << predp[v][l] << '\\n';
    } else {
      V<P<ll, ll>> cand;
      while (v >= ((1LL << 10) - 1)) {
        cand.emplace_back(vw[v]);
        v = (v - 1) >> 1;
      }

      int sz = cand.size();
      ll ans = predp[v][l];
      for (ll state = 0; state < (1LL << sz); state++) {
        ll tmpw = 0, tmpv = 0;
        for (int bit = 0; bit < sz; bit++) {
          if ((state >> bit) & 1) {
            tmpv += cand[bit].first;
            tmpw += cand[bit].second;
          }
        }
        if (tmpw <= l) {
          chmax(ans, predp[v][l - tmpw] + tmpv);
        }
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}