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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
constexpr char ln = '\\n';
constexpr long long MOD = 1000000007;
// constexpr long long MOD = 998244353;
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline int popcount(int x) { return __builtin_popcount(x); }
inline int popcount(long long x) { return __builtin_popcountll(x); }
void print() { cout << "\\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

const int MAXL = 100000;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<ll> V(N + 1), W(N + 1);
  rep(i, N) cin >> V[i + 1] >> W[i + 1];
  int Q;
  cin >> Q;
  vector<vector<pii>> L(N + 1);
  rep(i, Q) {
    int v, l;
    cin >> v >> l;
    L[v].emplace_back(l, i);
  }

  vector<vector<ll>> dp(512, vector<ll>(MAXL + 1));
  vector<ll> ans(Q);
  auto dfs = [&](auto &&self, int v, int a, vector<pll> arr) -> void {
    if (v > N)
      return;
    int pv = v / 2;
    if (v < 512) {
      rep(i, MAXL + 1) {
        chmax(dp[v][i], dp[pv][i]);
        if (i + W[v] <= MAXL)
          chmax(dp[v][i + W[v]], dp[pv][i] + V[v]);
      }
      for (auto i : L[v]) {
        ans[i.second] = dp[v][i.first];
      }
      self(self, v * 2, v, arr);
      self(self, v * 2 + 1, v, arr);
    } else {
      auto B = arr;
      for (auto val : arr) {
        B.emplace_back(val.first + V[v], val.second + W[v]);
      }
      for (auto i : L[v]) {
        for (auto val : B) {
          if (val.second > i.first)
            continue;
          chmax(ans[i.second], dp[a][i.first - val.second] + val.first);
        }
      }
      self(self, v * 2, a, B);
      self(self, v * 2 + 1, a, B);
    }
  };
  vector<pll> arr;
  arr.emplace_back(0, 0);
  dfs(dfs, 1, 0, arr);
  rep(i, Q) cout << ans[i] << ln;
}

Error: MLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
constexpr char ln = '\\n';
constexpr long long MOD = 1000000007;
// constexpr long long MOD = 998244353;
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline int popcount(int x) { return __builtin_popcount(x); }
inline int popcount(long long x) { return __builtin_popcountll(x); }
void print() { cout << "\\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

const int MAXL = 100000;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<ll> V(N + 1), W(N + 1);
  rep(i, N) cin >> V[i + 1] >> W[i + 1];
  int Q;
  cin >> Q;
  vector<vector<pii>> L(N + 1);
  rep(i, Q) {
    int v, l;
    cin >> v >> l;
    L[v].emplace_back(l, i);
  }

  vector<vector<ll>> dp(512, vector<ll>(MAXL + 1));
  vector<vector<pll>> arr(N + 1);
  vector<ll> ans(Q);
  auto dfs = [&](auto &&self, int v, int a) -> void {
    if (v > N)
      return;
    int pv = v / 2;
    if (v < 512) {
      rep(i, MAXL + 1) {
        chmax(dp[v][i], dp[pv][i]);
        if (i + W[v] <= MAXL)
          chmax(dp[v][i + W[v]], dp[pv][i] + V[v]);
      }
      for (auto i : L[v]) {
        ans[i.second] = dp[v][i.first];
      }
      arr[v].emplace_back(0, 0);
      self(self, v * 2, v);
      self(self, v * 2 + 1, v);
    } else {
      for (auto val : arr[pv]) {
        arr[v].emplace_back(val);
        arr[v].emplace_back(val.first + V[v], val.second + W[v]);
      }
      for (auto i : L[v]) {
        for (auto val : arr[pv]) {
          if (val.second > i.first)
            continue;
          chmax(ans[i.second], dp[a][i.first - val.second] + val.first);
        }
      }
      self(self, v * 2, a);
      self(self, v * 2 + 1, a);
    }
  };

  dfs(dfs, 1, 0);
  rep(i, Q) cout << ans[i] << ln;
}