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

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> V(n + 1), W(n + 1);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &V[i], &W[i]);
  const int MX = 100000;
  vector<vector<int>> dp(512);
  dp[0].resize(MX + 1);
  for (int i = 1; i <= min(511, n); ++i) {
    int p = i / 2;
    dp[i] = dp[p];
    for (int j = W[i]; j <= MX; ++j)
      dp[i][j] = max(dp[i][j], dp[p][j - W[i]] + V[i]);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    vector<int> pars;
    while (v > 511) {
      pars.push_back(v);
      v >>= 1;
    }
    int ans = 0;
    int s = pars.size();
    function<void(int, int, int)> go = [&](int i, int w, int val) {
      if (w > L)
        return;
      if (i == s) {
        ans = max(ans, dp[v][L - w] + val);
        return;
      }
      go(i + 1, w, val);
      go(i + 1, w + W[pars[i]], val + V[pars[i]]);
    };
    go(0, 0, 0);
    printf("%d\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> V(n + 1), W(n + 1);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &V[i], &W[i]);
  const int MX = 100000;
  vector<vector<ll>> dp(512, vector<ll>(MX + 1));
  for (int i = 1; i <= min(511, n); ++i) {
    int p = i / 2;
    dp[i] = dp[p];
    for (int j = W[i]; j <= MX; ++j)
      dp[i][j] = max(dp[i][j], dp[p][j - W[i]] + V[i]);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    vector<int> pars;
    while (v > 511) {
      pars.push_back(v);
      v >>= 1;
    }
    ll ans = 0;
    int s = pars.size();
    function<void(int, int, ll)> go = [&](int i, int w, ll val) {
      if (w > L)
        return;
      if (i == s) {
        ans = max(ans, dp[v][L - w] + val);
        return;
      }
      go(i + 1, w, val);
      go(i + 1, w + W[pars[i]], val + V[pars[i]]);
    };
    go(0, 0, 0);
    printf("%lld\\n", ans);
  }
}