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

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\\n"

vvi dp((1 << 11) + 1, vi(100010));

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vi V(N + 1), W(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }

  for (int i = 0; i < 100010; i++) {
    if (i < W[1])
      dp[1][i] = 0;
    else
      dp[1][i] = V[1];
  }
  int NN = min((1 << 11), N);
  for (int i = 2; i <= NN; i++) {
    for (int j = 0; j < 100010; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j - W[i] >= 0)
        chmax(dp[i][j], dp[i / 2][j - W[i]] + V[i]);
    }
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    if (v <= NN) {
      cout << dp[v][L] << endl;
      continue;
    }

    int tmp = v / (1 << 11);
    int counter = 0;
    while (tmp > 0) {
      counter++;
      tmp /= 2;
    }
    int ans = 0;
    int par = v / (1 << counter);
    for (int bit = 0; bit < (1 << counter); bit++) {
      int wsum = 0, vsum = 0;
      for (int i = 0; i < counter; i++) {
        int j = v / (1 << i);
        if ((bit >> i) & 1) {
          wsum += W[j];
          vsum += V[j];
          if (wsum > L)
            break;
        }
      }
      if (wsum > L)
        continue;
      chmax(ans, vsum + dp[par][L - wsum]);
    }
    cout << ans << endl;
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\\n"

vvll dp((1 << 11) + 1, vll(100010));

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vll V(N + 1), W(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }

  for (int i = 0; i < 100010; i++) {
    if (i < W[1])
      dp[1][i] = 0;
    else
      dp[1][i] = V[1];
  }
  ll NN = min((1LL << 11), N);
  for (int i = 2; i <= NN; i++) {
    for (int j = 0; j < 100010; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j - W[i] >= 0)
        chmax(dp[i][j], dp[i / 2][j - W[i]] + V[i]);
    }
  }

  ll Q;
  cin >> Q;
  while (Q--) {
    ll v, L;
    cin >> v >> L;
    if (v <= NN) {
      cout << dp[v][L] << endl;
      continue;
    }

    ll tmp = v / (1LL << 11);
    ll counter = 0;
    while (tmp > 0) {
      counter++;
      tmp /= 2;
    }
    ll ans = 0;
    ll par = v / (1 << counter);
    for (int bit = 0; bit < (1 << counter); bit++) {
      ll wsum = 0, vsum = 0;
      for (int i = 0; i < counter; i++) {
        int j = v / (1 << i);
        if ((bit >> i) & 1) {
          wsum += W[j];
          vsum += V[j];
          if (wsum > L)
            break;
        }
      }
      if (wsum > L)
        continue;
      chmax(ans, vsum + dp[par][L - wsum]);
    }
    cout << ans << endl;
  }
}