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

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  lint n;
  cin >> n;
  vector<int> V(n + 1), W(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> V[i] >> W[i];
  }
  lint q;
  cin >> q;
  vector<int> v(q), l(q);
  int L = 0;
  for (int i = 0; i < q; ++i) {
    cin >> v[i] >> l[i];
    chmax(L, l[i]);
  }
  int up = min(1LL << 11, n + 1);
  vector<vector<int>> dp(up, vector<int>(L + 1, 0));
  for (int i = 1; i < up; ++i) {
    int p = i / 2;
    for (int j = 0; j <= L; ++j) {
      chmax(dp[i][j], dp[p][j]);
      if (j - W[i] >= 0)
        chmax(dp[i][j], dp[p][j - W[i]] + V[i]);
    }
  }
  for (int i = 1; i < up; ++i) {
    for (int j = 1; j <= L; ++j) {
      chmax(dp[i][j], dp[i][j - 1]);
    }
  }
  for (int i = 0; i < q; ++i) {
    if (v[i] < (up)) {
      cout << dp[v[i]][l[i]] << "\\n";
    } else {
      vector<lint> vv, ww;
      lint c = v[i];
      while (c >= (up)) {
        vv.push_back(V[c]);
        ww.push_back(W[c]);
        c /= 2;
      }
      int k = vv.size();
      lint ret = 0;
      for (int j = 0; j < 1 << k; ++j) {
        lint vs = 0;
        lint ws = 0;
        for (int x = 0; x < k; ++x) {
          if (j >> x & 1) {
            vs += vv[x];
            ws += ww[x];
          }
        }
        if (ws <= l[i]) {
          chmax(ret, vs + dp[c][l[i] - ws]);
        }
      }
      cout << ret << "\\n";
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  lint n;
  cin >> n;
  vector<lint> V(n + 1), W(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> V[i] >> W[i];
  }
  lint q;
  cin >> q;
  vector<lint> v(q), l(q);
  lint L = 0;
  for (int i = 0; i < q; ++i) {
    cin >> v[i] >> l[i];
    chmax(L, l[i]);
  }
  int up = min(1LL << 11, n + 1);
  vector<vector<lint>> dp(up, vector<lint>(L + 1, 0));
  for (int i = 1; i < up; ++i) {
    int p = i / 2;
    for (int j = 0; j <= L; ++j) {
      chmax(dp[i][j], dp[p][j]);
      if (j - W[i] >= 0)
        chmax(dp[i][j], dp[p][j - W[i]] + V[i]);
    }
  }
  for (int i = 1; i < up; ++i) {
    for (int j = 1; j <= L; ++j) {
      chmax(dp[i][j], dp[i][j - 1]);
    }
  }
  for (int i = 0; i < q; ++i) {
    if (v[i] < (up)) {
      cout << dp[v[i]][l[i]] << "\\n";
    } else {
      vector<lint> vv, ww;
      lint c = v[i];
      while (c >= (up)) {
        vv.push_back(V[c]);
        ww.push_back(W[c]);
        c /= 2;
      }
      int k = vv.size();
      lint ret = 0;
      for (int j = 0; j < 1 << k; ++j) {
        lint vs = 0;
        lint ws = 0;
        for (int x = 0; x < k; ++x) {
          if (j >> x & 1) {
            vs += vv[x];
            ws += ww[x];
          }
        }
        if (ws <= l[i]) {
          chmax(ret, vs + dp[c][l[i] - ws]);
        }
      }
      cout << ret << "\\n";
    }
  }
  return 0;
}