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

#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
using Int = long long;
const char newl = '\\n';

// INSERT ABOVE HERE
using P = pair<Int, Int>;
P dp[1024][1024];
Int sz[1024] = {};

Int vv[1 << 18];
Int ww[1 << 18];

Int dep[1 << 18] = {};
const Int INF = 1e18;

Int query(Int v, Int l) {
  return (lower_bound(dp[v], dp[v] + sz[v], P(l, INF)) - 1)->second;
}

Int dfs(Int v, Int l) {
  if (l < 0)
    return -INF;
  if (dep[v] <= 10)
    return query(v, l);
  return max(dfs(v / 2, l), dfs(v / 2, l - ww[v]) + vv[v]);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin >> n;
  for (Int i = 1; i <= n; i++)
    cin >> vv[i] >> ww[i];

  Int q;
  cin >> q;
  vector<Int> xs(q), ys(q);
  for (Int i = 0; i < q; i++)
    cin >> xs[i] >> ys[i];

  dep[1] = 1;
  for (Int i = 2; i <= n; i++)
    dep[i] = dep[i / 2] + 1;

  for (Int i = 1; i <= min(n, 1023LL); i++) {
    vector<Int> rs;
    for (Int j = 0; j < dep[i]; j++)
      rs.emplace_back(i >> j);

    Int s = 1 << dep[i];
    vector<P> vp;
    for (Int b = 0; b < s; b++) {
      Int p = 0, q = 0;
      for (Int j = 0; j < dep[i]; j++) {
        if ((~b >> j) & 1)
          continue;
        p += ww[rs[j]];
        q += vv[rs[j]];
      }
      vp.emplace_back(p, q);
    }

    sort(vp.begin(), vp.end());
    vector<P> nx;
    for (auto p : vp)
      if (nx.empty() or nx.back().second < p.second)
        nx.emplace_back(p);

    sz[i] = nx.size();
    for (Int j = 0; j < sz[i]; j++)
      dp[i][j] = nx[j];
  }

  for (Int i = 0; i < q; i++)
    cout << dfs(xs[i], ys[i]) << newl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
using Int = long long;
const char newl = '\\n';

// INSERT ABOVE HERE
using P = pair<Int, Int>;
P dp[1024][1024];
Int sz[1024];

Int vv[1 << 18];
Int ww[1 << 18];

Int dep[1 << 18] = {};
const Int INF = 1e18;

Int query(Int v, Int l) {
  return (lower_bound(dp[v], dp[v] + sz[v], P(l, INF)) - 1)->second;
}

Int dfs(Int v, Int l) {
  if (l < 0)
    return -INF;
  if (dep[v] <= 10)
    return query(v, l);
  return max(dfs(v / 2, l), dfs(v / 2, l - ww[v]) + vv[v]);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin >> n;
  for (Int i = 1; i <= n; i++)
    cin >> vv[i] >> ww[i];

  Int q;
  cin >> q;
  vector<Int> xs(q), ys(q);
  for (Int i = 0; i < q; i++)
    cin >> xs[i] >> ys[i];

  dep[1] = 1;
  for (Int i = 2; i <= n; i++)
    dep[i] = dep[i / 2] + 1;

  for (Int i = 1; i <= min(n, 1024LL); i++) {
    vector<Int> rs;
    for (Int j = 0; j < dep[i]; j++)
      rs.emplace_back(i >> j);

    Int s = 1 << dep[i];
    vector<P> vp;
    for (Int b = 0; b < s; b++) {
      Int p = 0, q = 0;
      for (Int j = 0; j < dep[i]; j++) {
        if ((~b >> j) & 1)
          continue;
        p += ww[rs[j]];
        q += vv[rs[j]];
      }
      vp.emplace_back(p, q);
    }

    sort(vp.begin(), vp.end());
    vector<P> nx;
    for (auto p : vp)
      if (nx.empty() or nx.back().second < p.second)
        nx.emplace_back(p);

    sz[i] = nx.size();
    for (Int j = 0; j < sz[i]; j++)
      dp[i][j] = nx[j];
  }

  for (Int i = 0; i < q; i++)
    cout << dfs(xs[i], ys[i]) << newl;

  return 0;
}