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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

struct Q {
  Int v, l, ind, add;
  Q(Int v, Int l, Int ind, Int add) : v(v), l(l), ind(ind), add(add) {}
};

#define mid 9
vector<Q> queries[1 << 18];
Int W[1 << 18], V[1 << 18];
Int dp[1 << (mid + 1)][110000];
Int ans[110000];

Int calc(Int v, Int l, Int add = 0) {
  if (l < 0)
    return 0;
  if (v == 1 || ((1 << mid) <= v && v < (1 << (mid + 1)))) {
    return dp[v][l] + add;
  } else {
    return max(calc(v / 2, l - W[v], add + V[v]), calc(v / 2, l, add));
  }
}

void solve(Int v) {
  if (v >= (1 << (mid + 1)))
    return;
  Int p = v / 2;
  for (Int i = 0; i < 110000; i++) {
    dp[v][i] = dp[p][i];
    if (i - W[v] >= 0) {
      dp[v][i] = max(dp[v][i], dp[p][i - W[v]] + V[v]);
    }
  }
  solve(v * 2);
  solve(v * 2 + 1);
}

int main() {
  Int n, q;
  cin >> n;
  for (Int i = 1; i <= n; i++) {
    cin >> V[i] >> W[i];
  }
  solve(1);
  cin >> q;
  for (Int i = 0; i < q; i++) {
    Int v, l;
    cin >> v >> l;
    cout << calc(v, l) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

struct Q {
  Int v, l, ind, add;
  Q(Int v, Int l, Int ind, Int add) : v(v), l(l), ind(ind), add(add) {}
};

vector<Q> queries[1 << 18];
Int W[1 << 18], V[1 << 18];
#define mid 9
void set_query(Int v, Int l, Int ind, Int add = 0) {
  if (l < 0)
    return;
  if (v == 1 || ((1 << mid) <= v && v < (1 << (mid + 1)))) {
    queries[v].push_back(Q(v, l, ind, add));
  } else {
    set_query(v / 2, l - W[v], ind, add + V[v]);
    set_query(v / 2, l, ind, add);
  }
}

Int dp[1 << (mid + 1)][110000];
Int ans[110000];

void solve(Int v) {
  if (v >= (1 << (mid + 1)))
    return;
  Int p = v / 2;
  for (Int i = 0; i < 110000; i++) {
    dp[v][i] = dp[p][i];
    if (i - W[v] >= 0) {
      dp[v][i] = max(dp[v][i], dp[p][i - W[v]] + V[v]);
    }
  }
  for (auto q : queries[v]) {
    Int ind = q.ind;
    Int l = q.l;
    Int add = q.add;
    ans[ind] = max(ans[ind], dp[v][l] + add);
  }
  solve(v * 2);
  solve(v * 2 + 1);
}

int main() {
  Int n, q;
  cin >> n;
  for (Int i = 1; i <= n; i++) {
    cin >> V[i] >> W[i];
  }
  cin >> q;
  for (Int i = 0; i < q; i++) {
    Int v, l;
    cin >> v >> l;
    set_query(v, l, i);
  }

  solve(1);
  for (Int i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}