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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 4611686018427387903;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

bool cmp(ll n, P const &p) { return ((p.first > n)); }

int n;
VI a(1000100), b(1000100);
vector<vector<P>> d(5000000, vector<P>(0));
int boarder = 2047;

void dfs(int v) {
  if (v >= boarder || v >= n)
    return;
  if (v == 0) {
    d[v].push_back({b[v], a[v]});
    d[v].push_back({0, 0});
  } else {
    for (auto p : d[(v + 1) / 2 - 1]) {
      d[v].push_back({p.first + b[v], p.second + a[v]});
      d[v].push_back({p.first, p.second});
    }
  }
  if ((v + 1) * 2 <= n)
    dfs((v + 1) * 2 - 1);
  if ((v + 1) * 2 + 1 <= n)
    dfs((v + 1) * 2);
  sort(ALL(d[v]));
  ll mx = 0;
  REP(i, d[v].size()) {
    mx = max(mx, d[v][i].second);
    d[v][i].second = mx;
  }
}

int main() {
  cin >> n;
  REP(i, n) cin >> a[i] >> b[i];
  dfs(0);
  int q;
  cin >> q;
  ll v, l;
  while (q--) {
    cin >> v >> l;
    v--;
    if (v < boarder) {
      int it = upper_bound(d[v].begin(), d[v].end(), l, cmp) - d[v].begin();
      if (it == 0)
        cout << 0 << endl;
      else
        cout << d[v][it - 1].second << endl;
    } else {
      VI s(0);
      while (v >= boarder) {
        s.push_back(v);
        v = (v + 1) / 2 - 1;
      }
      ll ans = 0;
      REP(i, 1 << s.size()) {
        ll suma = 0, sumb = 0;
        REP(j, s.size()) if (i >> j & 1) {
          suma += a[s[j]];
          sumb += b[s[j]];
        }
        if (l - sumb < 0)
          continue;
        int it =
            upper_bound(d[v].begin(), d[v].end(), l - sumb, cmp) - d[v].begin();
        if (it == 0)
          ans = max(ans, suma);
        else
          ans = max(ans, suma + d[v][it - 1].second);
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 4611686018427387903;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

bool cmp(ll n, P const &p) { return ((p.first > n)); }

int n;
VI a(100010), b(100010);
vector<vector<P>> d(500000, vector<P>(0));
int boarder = 1023;

void dfs(int v) {
  if (v >= boarder || v >= n)
    return;
  if (v == 0) {
    d[v].push_back({b[v], a[v]});
    d[v].push_back({0, 0});
  } else {
    for (auto p : d[(v + 1) / 2 - 1]) {
      d[v].push_back({p.first + b[v], p.second + a[v]});
      d[v].push_back({p.first, p.second});
    }
  }
  if ((v + 1) * 2 <= n)
    dfs((v + 1) * 2 - 1);
  if ((v + 1) * 2 + 1 <= n)
    dfs((v + 1) * 2);
  sort(ALL(d[v]));
  ll mx = 0;
  REP(i, d[v].size()) {
    mx = max(mx, d[v][i].second);
    d[v][i].second = mx;
  }
}

int main() {
  cin >> n;
  REP(i, n) cin >> a[i] >> b[i];
  dfs(0);
  int q;
  cin >> q;
  ll v, l;
  while (q--) {
    cin >> v >> l;
    v--;
    if (v < boarder) {
      int it = upper_bound(d[v].begin(), d[v].end(), l, cmp) - d[v].begin();
      if (it == 0)
        cout << 0 << endl;
      else
        cout << d[v][it - 1].second << endl;
    } else {
      VI s(0);
      while (v >= boarder) {
        s.push_back(v);
        v = (v + 1) / 2 - 1;
      }
      ll ans = 0;
      REP(i, 1 << s.size()) {
        ll suma = 0, sumb = 0;
        REP(j, s.size()) if (i >> j & 1) {
          suma += a[s[j]];
          sumb += b[s[j]];
        }
        int it =
            upper_bound(d[v].begin(), d[v].end(), l - sumb, cmp) - d[v].begin();
        if (it == 0)
          ans = max(ans, suma);
        else
          ans = max(ans, suma + d[v][it - 1].second);
      }
      cout << ans << endl;
    }
  }
  return 0;
}