Problem p02648 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> inline bool chmin(T &x, U y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &x, U y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

int n, L = (1 << 9) - 1;
vector<vector<pair<int, int>>> Q;
vector<int> ans;
vector<pair<int, int>> E[20];
vector<int> v, w;

void rec(int i, int d) {
  E[d].emplace_back(0, 0);
  if (d == 10) {
    E[d].emplace_back(w[i], v[i]);
  } else {
    int s{}, t{}, N = E[d - 1].size();
    while (t < N) {
      if (s >= N or E[d - 1][t].first + w[i] < E[d - 1][s].first) {
        if (E[d - 1][t].first + w[i] > 100000)
          break;
        if (E[d].back().second < E[d - 1][t].second + v[i]) {
          E[d].emplace_back(E[d - 1][t].first + w[i],
                            E[d - 1][t].second + v[i]);
        }
        ++t;
      } else {
        if (E[d - 1][s].first > 100000)
          break;
        if (E[d].back().second < E[d - 1][s].second) {
          E[d].push_back(E[d - 1][s]);
        }
        ++s;
      }
    }
  }
  for (auto &[l, id] : Q[i]) {
    if (d < 10) {
      ans[id] = (lower_bound(E[d].begin(), E[d].end(), make_pair(l + 1, 0)) - 1)
                    ->second;
    } else {
      for (auto &[W, V] : E[d]) {
        if (W > l)
          break;
        chmax(ans[id], V + (lower_bound(E[9].begin(), E[9].end(),
                                        make_pair(l - W + 1, 0)) -
                            1)
                               ->second);
      }
    }
  }
  if (i * 2 <= n)
    rec(i * 2, d + 1);
  if (i * 2 + 1 <= n)
    rec(i * 2 + 1, d + 1);
  E[d].clear();
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> n;
  Q.resize(n + 1);
  v.resize(n + 1);
  w.resize(n + 1);
  Fr(i, n) cin >> v[i] >> w[i];
  int q;
  struct qu {
    int i, v, l;
  };
  cin >> q;
  ans.resize(q);
  fr(i, q) {
    int x, l;
    cin >> x >> l;
    Q[x].emplace_back(l, i);
  }
  E[0].emplace_back(0, 0);
  rec(1, 1);
  for (auto &i : ans)
    cout << i << '\\n';
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> inline bool chmin(T &x, U y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &x, U y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, L = (1 << 9) - 1;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  Fr(i, n) cin >> v[i] >> w[i];
  vector<vector<pair<int, ll>>> e(n + 1);
  e[1].emplace_back(0, 0);
  e[1].emplace_back(w[1], v[1]);
  for (int i = 2; i <= n; ++i) {
    e[i].emplace_back(0, 0);
    if (L < i and i <= 2 * L + 1) {
      e[i].emplace_back(w[i], v[i]);
      continue;
    }
    int s{}, t{}, p = i / 2, N = e[p].size();
    while (t < N) {
      if (s >= N or e[p][t].first + w[i] < e[p][s].first) {
        if (e[p][t].first + w[i] > 100000)
          break;
        if (e[i].back().second < e[p][t].second + v[i]) {
          e[i].emplace_back(e[p][t].first + w[i], e[p][t].second + v[i]);
        }
        ++t;
      } else {
        if (e[p][s].first > 100000)
          break;
        if (e[i].back().second < e[p][s].second) {
          e[i].push_back(e[p][s]);
        }
        ++s;
      }
    }
  }
  int q;
  cin >> q;
  fr(i, q) {
    int x, l;
    cin >> x >> l;
    if (x <= L) {
      cout << (lower_bound(e[x].begin(), e[x].end(), make_pair(l + 1, 0ll)) - 1)
                  ->second
           << '\\n';
      continue;
    }
    int ans{}, y = x;
    while (y > L)
      y /= 2;
    for (auto &j : e[x]) {
      if (j.first > l)
        break;
      chmax(ans, j.second + (lower_bound(e[y].begin(), e[y].end(),
                                         make_pair(l - j.first + 1, 0ll)) -
                             1)
                                ->second);
    }
    cout << ans << '\\n';
  }
}