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

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

int main() {
#ifdef ELEGIA
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  const int L = 100000;

  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i];
  int b = min(1 << 10, n);
  vector<vector<ll>> dp(b + 1, vector<ll>(L + 1));
  for (int i = 1; i <= b; ++i) {
    dp[i] = dp[i >> 1];
    for (int j = w[i]; j <= L; ++j)
      dp[i][j] = max(dp[i][j], dp[i >> 1][j - w[i]] + v[i]);
  }
  int q;
  cin >> q;
  while (q--) {
    int u, l;
    cin >> u >> l;
    vector<pair<int, int>> th;
    while (u > b) {
      th.emplace_back(v[u], w[u]);
      u >>= 1;
    }
    ll ans = 0;
    function<void(int, int, ll)> dfs = [&](int k, int wt, ll vt) {
      if (k == th.size()) {
        ans = max(ans, dp[u][l - wt] + vt);
        return;
      }
      if (wt + th[k].second <= l)
        dfs(k + 1, wt + th[k].second, vt + th[k].first);
      dfs(k + 1, wt, vt);
    };
    dfs(0, 0, 0);
    cout << ans << '\\n';
  }

#ifdef ELEGIA
  LOG("Time: %dms\\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}


Error: TLE

Fixed submission:

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

int main() {
#ifdef ELEGIA
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  const int L = 100000;

  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i];
  int b = sqrt(n);
  vector<vector<ll>> dp(b + 1, vector<ll>(L + 1));
  for (int i = 1; i <= b; ++i) {
    dp[i] = dp[i >> 1];
    for (int j = w[i]; j <= L; ++j)
      dp[i][j] = max(dp[i][j], dp[i >> 1][j - w[i]] + v[i]);
  }
  int q;
  cin >> q;
  while (q--) {
    int u, l;
    cin >> u >> l;
    vector<pair<int, int>> th;
    while (u > b) {
      th.emplace_back(v[u], w[u]);
      u >>= 1;
    }
    ll ans = 0;
    function<void(int, int, ll)> dfs = [&](int k, int wt, ll vt) {
      if (k == th.size()) {
        ans = max(ans, dp[u][l - wt] + vt);
        return;
      }
      if (wt + th[k].second <= l)
        dfs(k + 1, wt + th[k].second, vt + th[k].first);
      dfs(k + 1, wt, vt);
    };
    dfs(0, 0, 0);
    cout << ans << '\\n';
  }

#ifdef ELEGIA
  LOG("Time: %dms\\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}