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

#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (300006)
ll n, q, dp[(1 << 9) + 2][100002], mid = 9;
pi store[(1 << 9) + 2];
pi A[MAXN];
int main() {
  FAST cin >> n;
  FOR(i, 1, n) cin >> A[i].f >> A[i].s;
  FOR(i, 1, (1 << min(n, mid)) - 1) {
    DEC(w, 1e5, 0) {
      dp[i][w] = dp[i / 2][w];
      if (w >= A[i].s)
        dp[i][w] = max(dp[i][w], dp[i / 2][w - A[i].s] + A[i].f);
    }
  }
  cin >> q;
  while (q--) {
    ll v, l;
    cin >> v >> l;
    ll lvl = ll(log2l(v)) + 1;
    vector<ll> stuff;
    while (lvl > mid) {
      stuff.eb(v);
      v /= 2, --lvl;
    }
    ll ans = 0;
    for (auto &i : store)
      i = pi(0, 0);
    FOR(i, 0, (1 << siz(stuff)) - 1) {
      ll total = 0, sum = 0;
      if (i) {
        ll lsb = __builtin_ctz(i);
        store[i].f = store[i - (1 << lsb)].f + A[stuff[lsb]].f;
        store[i].s = store[i - (1 << lsb)].s + A[stuff[lsb]].s;
        total = store[i].s, sum = store[i].f;
      }
      if (total > l)
        continue;
      ans = max(ans, sum + dp[v][l - total]);
    }
    cout << ans << '\\n';
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (300006)
ll n, q, dp[(1 << 9) + 2][100002], mid = 9;
pi A[MAXN];
int main() {
  FAST cin >> n;
  FOR(i, 1, n) cin >> A[i].f >> A[i].s;
  FOR(i, 1, (1 << min(n, mid)) - 1) {
    DEC(w, 1e5, 0) {
      dp[i][w] = dp[i / 2][w];
      if (w >= A[i].s)
        dp[i][w] = max(dp[i][w], dp[i / 2][w - A[i].s] + A[i].f);
    }
  }
  cin >> q;
  while (q--) {
    ll v, l;
    cin >> v >> l;
    ll lvl = ll(log2l(v)) + 1;
    vector<ll> stuff;
    while (lvl > mid) {
      stuff.eb(v);
      v /= 2, --lvl;
    }
    ll ans = 0;
    FOR(i, 0, (1 << siz(stuff)) - 1) {
      ll total = 0, sum = 0;
      FOR(j, 0, siz(stuff) - 1)
      if (1 << j & i) total += A[stuff[j]].s,
          sum += A[stuff[j]].f;
      if (total > l)
        continue;
      ans = max(ans, sum + dp[v][l - total]);
    }
    cout << ans << '\\n';
  }
}