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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  rep(i, a.size()) cout << a[i] << " ";
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\\n";
}
const int mod = 1e9 + 7;
// const int mod = 998244353;

int main() {
  int n;
  cin >> n;
  vector<int> V(n + 1), W(n + 1);
  rep(i, n) cin >> V[i + 1] >> W[i + 1];
  int M = 1024;
  vector<vector<pair<int, int>>> vp(M + 1);
  vp[0].emplace_back(0, 0);
  REP(i, 1, M + 1) {
    if (i > n)
      break;
    vector<pair<int, int>> vv;
    for (auto b : vp[i / 2]) {
      vv.emplace_back(b);
      vv.emplace_back(b.first + W[i], b.second + V[i]);
    }
    sort(all(vv));
    int now = -1;
    for (auto b : vv) {
      if (now < b.second) {
        vp[i].push_back(b);
        now = b.second;
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    vector<pair<int, int>> acc;
    acc.emplace_back(0, 0);
    while (v > M) {
      vector<pair<int, int>> tmp;
      for (auto b : acc) {
        tmp.emplace_back(b);
        tmp.emplace_back(b.first + W[v], b.second + V[v]);
      }
      acc = tmp;
      v >>= 1;
    }
    int ans = 0;
    for (auto b : acc) {
      pair<int, int> pp = {L - b.first, inf};
      auto itr = lower_bound(all(vp[v]), pp);
      if (itr != vp[v].begin()) {
        itr--;
        chmax(ans, (*itr).second + b.second);
      }
    }
    cout << ans << "\\n";
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  rep(i, a.size()) cout << a[i] << " ";
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\\n";
}
const int mod = 1e9 + 7;
// const int mod = 998244353;

int main() {
  int n;
  cin >> n;
  vector<int> V(n + 1), W(n + 1);
  rep(i, n) cin >> V[i + 1] >> W[i + 1];
  int M = 1024;
  vector<vector<pair<int, int>>> vp(M);
  vp[0].emplace_back(0, 0);
  REP(i, 1, M + 1) {
    if (i > n)
      break;
    vector<pair<int, int>> vv;
    for (auto b : vp[i / 2]) {
      vv.emplace_back(b);
      vv.emplace_back(b.first + W[i], b.second + V[i]);
    }
    sort(all(vv));
    int now = -1;
    for (auto b : vv) {
      if (now < b.second) {
        vp[i].push_back(b);
        now = b.second;
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    vector<pair<int, int>> acc;
    acc.emplace_back(0, 0);
    while (v > M) {
      vector<pair<int, int>> tmp;
      for (auto b : acc) {
        tmp.emplace_back(b);
        tmp.emplace_back(b.first + W[v], b.second + V[v]);
      }
      acc = tmp;
      v >>= 1;
    }
    int ans = 0;
    for (auto b : acc) {
      pair<int, int> pp = {L - b.first, inf};
      auto itr = lower_bound(all(vp[v]), pp);
      if (itr != vp[v].begin()) {
        itr--;
        chmax(ans, (*itr).second + b.second);
      }
    }
    cout << ans << "\\n";
  }
}