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

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

#include <bits/stdc++.h>
using namespace std;

static const ll INF = 1e15;

static const ll mod = 1e9 + 7;

ll n;
vector<ll> vs;
vector<ll> wes;
vector<map<ll, ll>> memo;

ll dfs(ll node, ll les) {
  if (node <= min(1LL << 10, n)) {
    if (les == 0)
      return 0;
    auto &c = memo[node];
    auto f = c.upper_bound(les);
    std::advance(f, -1);
    return f->second;
  }
  auto a = dfs(node / 2, les);
  if (les >= wes[node]) {
    auto b = dfs(node / 2, les - wes[node]);
    b += vs[node];
    return max(a, b);
  }
  return a;
}

int main() {
  cin >> n;
  vs.resize(n + 1);
  wes.resize(n + 1);
  memo.resize(n + 1);
  rep(i, n) { cin >> vs[i + 1] >> wes[i + 1]; }
  memo[1][0] = 0;
  memo[1][wes[1]] = vs[1];
  if (n > 1) {
    vector<P> buffer;
    buffer.reserve(n);
    for (int i = 2; i <= min(1LL << 10, n); i++) {
      buffer.clear();
      const auto &prev = memo[i / 2];
      for (auto p : prev) {
        buffer.emplace_back(p);
        buffer.emplace_back(p.first + wes[i], p.second + vs[i]);
      }
      sort(buffer.begin(), buffer.end());
      ll pv = -1;
      auto &child = memo[i];
      for (auto p : buffer) {
        if (pv < p.second) {
          child[p.first] = p.second;
          pv = p.second;
        }
      }
    }
  }
  int q;
  cin >> q;
  vector<ll> ans;
  rep(_, q) {
    int v, l;
    cin >> v >> l;
    ll u = dfs(v, l);
    ans.push_back(u);
  }
  rep(i, q) { cout << ans[i] << endl; }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

#include <bits/stdc++.h>
using namespace std;

static const ll INF = 1e15;

static const ll mod = 1e9 + 7;

ll n;
vector<ll> vs;
vector<ll> wes;
vector<map<ll, ll>> memo;

ll dfs(ll node, ll les) {
  if (node <= n / 2) {
    if (les == 0)
      return 0;
    auto &c = memo[node];
    auto f = c.upper_bound(les);
    std::advance(f, -1);
    return f->second;
  }
  auto a = dfs(node / 2, les);
  if (les >= wes[node]) {
    auto b = dfs(node / 2, les - wes[node]);
    b += vs[node];
    return max(a, b);
  }
  return a;
}

int main() {
  cin >> n;
  vs.resize(n + 1);
  wes.resize(n + 1);
  memo.resize(n + 1);
  rep(i, n) { cin >> vs[i + 1] >> wes[i + 1]; }
  memo[1][0] = 0;
  memo[1][wes[1]] = vs[1];
  if (n > 1) {
    vector<P> buffer;
    buffer.reserve(n);
    for (int i = 2; i <= n / 2; i++) {
      buffer.clear();
      const auto &prev = memo[i / 2];
      for (auto p : prev) {
        buffer.emplace_back(p);
        buffer.emplace_back(p.first + wes[i], p.second + vs[i]);
      }
      sort(buffer.begin(), buffer.end());
      ll pv = -1;
      auto &child = memo[i];
      for (auto p : buffer) {
        if (pv < p.second) {
          child[p.first] = p.second;
          pv = p.second;
        }
      }
    }
  }
  int q;
  cin >> q;
  vector<ll> ans;
  rep(_, q) {
    int v, l;
    cin >> v >> l;
    ll u = dfs(v, l);
    ans.push_back(u);
  }
  rep(i, q) { cout << ans[i] << endl; }
  return 0;
}