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

/*Bismillahir Rahmanir Rahim*/ //{
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std;
using __gnu_cxx::power;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
  static constexpr auto &os = cout; // replace by cerr in interactive.
  template <class C> static auto o(C *x) -> decltype(os << *x, 0);
  template <class C> static char o(...);
  template <class C>
  typename enable_if<sizeof o<C>(0) != 1, Dbg &>::type operator<<(const C &x) {
    os << x;
    return *this;
  }
  template <class C> Dbg &dump(C b, C e) {
    for (
        struct {
          C i;
          int j;
        } i = {(*this << "\\n[", b), 0};
        i.i != e and i.j < 26; i.i++, i.j++)
      *this << ", " + 2 * (i.i == b) << *i.i;
    return *this << ']';
  }
  template <class C>
  typename enable_if<sizeof o<C>(0) == 1, Dbg &>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
  }
  template <class C, size_t X>
  typename enable_if<!is_same<C, const char>::value, Dbg &>::type
  operator<<(C (&x)[X]) {
    return dump(begin(x), end(x));
  }
  template <class B, class C> Dbg &operator<<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
  }
  Dbg &operator<<(ostream &(*x)(std::ostream &)) {
    os << x;
    return *this;
  }
} dbg;
void err(istringstream *iss) { delete iss; }
template <class C, class... Args>
void err(istringstream *iss, const C &val, const Args &...args) {
  string name;
  *iss >> name;
  if (name.back() == ',')
    name.pop_back();
  dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...)                                                           \\
  dbg << __LINE__ << ": ", err(new istringstream(#args), args), dbg << endl
#define cbug(a, args...)                                                       \\
  if (a)                                                                       \\
    bug(args);
#define ASSERT(a, o, b, args...)                                               \\
  if (!((a)o(b)))                                                              \\
    bug(a, b, ##args), assert((a)o(b));
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn; //}
const int inf = 1000000007, mxn = 1 << 18 | 5, mxl = 100005;
#define bin(m) bitset<9>(m)
#define test(m, p) (1 & ((m) >> (p)))
#define set_(m, p) ((m) | (1 << (p)))
#define setb(m, p) m = set_(m, p)
#define reset_(m, p) ((m) & ~(1 << (p)))
#define resetb(m, p) m = reset_(m, p)
#define toggle_(m, p) ((m) ^ (1 << (p)))
#define toggleb(m, p) m = toggle_(m, p)
#define assign_(m, p, val) (val ? set_(m) : reset_(m))
#define assignb(m, p, val) m = assign_(m, p, val)
#define submask(s, m) s = (s - 1) & m
#define getAllSet(p) ((1 << (p)) - 1)
#define isAllSet(m, p) (m) == getAllSet(p)

int n, v[mxn], w[mxn], ans[1 << 9 | 5][mxl], ansv[1 << 9 | 5], answ[1 << 9 | 5];

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  cout.precision(11), cout.setf(ios::fixed);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 1; i <= min(n, (1 << 9) - 1); i++) {
    for (int j = 1; j <= mxl - 1; j++) {
      ans[i][j] = max({ans[i][j - 1], ans[i >> 1][j],
                       (j >= w[i] ? ans[i >> 1][j - w[i]] + v[i] : 0)});
    }
  }

  cin >> Q;
  while (Q--) {
    int V, L;
    cin >> V >> L;
    if (V < (1 << 9)) {
      cout << ans[V][L] << '\\n';
      continue;
    }
    int tmp = V, cnt = 0;
    while (tmp >= (1 << 9))
      tmp >>= 1, cnt++;
    memset(ansv, 0, sizeof(ansv)), memset(answ, 0, sizeof(answ)),
        memset(ans[0], 0, sizeof(ans[0]));
    int mx = ans[tmp][L];
    for (int j = 1; j < 1 << cnt; j++) {
      int msb = __lg(j), prv = reset_(j, msb);
      ansv[j] = ansv[prv] + v[V >> msb], answ[j] = answ[prv] + w[V >> msb];
      if (answ[j] <= L) {
        ans[0][answ[j]] = max(ans[0][answ[j]], ansv[j]);
        mx = max(mx, ans[0][answ[j]] + ans[tmp][L - answ[j]]);
      }
    }
    cout << mx << '\\n';
  }
}

Error: TLE

Fixed submission:

/*Bismillahir Rahmanir Rahim*/ //{
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std;
using __gnu_cxx::power;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
  static constexpr auto &os = cout; // replace by cerr in interactive.
  template <class C> static auto o(C *x) -> decltype(os << *x, 0);
  template <class C> static char o(...);
  template <class C>
  typename enable_if<sizeof o<C>(0) != 1, Dbg &>::type operator<<(const C &x) {
    os << x;
    return *this;
  }
  template <class C> Dbg &dump(C b, C e) {
    for (
        struct {
          C i;
          int j;
        } i = {(*this << "\\n[", b), 0};
        i.i != e and i.j < 26; i.i++, i.j++)
      *this << ", " + 2 * (i.i == b) << *i.i;
    return *this << ']';
  }
  template <class C>
  typename enable_if<sizeof o<C>(0) == 1, Dbg &>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
  }
  template <class C, size_t X>
  typename enable_if<!is_same<C, const char>::value, Dbg &>::type
  operator<<(C (&x)[X]) {
    return dump(begin(x), end(x));
  }
  template <class B, class C> Dbg &operator<<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
  }
  Dbg &operator<<(ostream &(*x)(std::ostream &)) {
    os << x;
    return *this;
  }
} dbg;
void err(istringstream *iss) { delete iss; }
template <class C, class... Args>
void err(istringstream *iss, const C &val, const Args &...args) {
  string name;
  *iss >> name;
  if (name.back() == ',')
    name.pop_back();
  dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...)                                                           \\
  dbg << __LINE__ << ": ", err(new istringstream(#args), args), dbg << endl
#define cbug(a, args...)                                                       \\
  if (a)                                                                       \\
    bug(args);
#define ASSERT(a, o, b, args...)                                               \\
  if (!((a)o(b)))                                                              \\
    bug(a, b, ##args), assert((a)o(b));
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn; //}
const int inf = 1000000007, mxn = 1 << 18 | 5, mxl = 100005;
#define bin(m) bitset<9>(m)
#define test(m, p) (1 & ((m) >> (p)))
#define set_(m, p) ((m) | (1 << (p)))
#define setb(m, p) m = set_(m, p)
#define reset_(m, p) ((m) & ~(1 << (p)))
#define resetb(m, p) m = reset_(m, p)
#define toggle_(m, p) ((m) ^ (1 << (p)))
#define toggleb(m, p) m = toggle_(m, p)
#define assign_(m, p, val) (val ? set_(m) : reset_(m))
#define assignb(m, p, val) m = assign_(m, p, val)
#define submask(s, m) s = (s - 1) & m
#define getAllSet(p) ((1 << (p)) - 1)
#define isAllSet(m, p) (m) == getAllSet(p)

int n, v[mxn], w[mxn], ans[1 << 9 | 5][mxl], ansv[1 << 9 | 5], answ[1 << 9 | 5];

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  cout.precision(11), cout.setf(ios::fixed);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 1; i <= min(n, (1 << 9) - 1); i++) {
    for (int j = 1; j <= mxl - 1; j++) {
      ans[i][j] =
          max(ans[i >> 1][j], (j >= w[i] ? ans[i >> 1][j - w[i]] + v[i] : 0));
    }
  }

  // if (n>=(1<<9)) {
  //     for (int i = 1<<8; i <= (1<<9)-1; i++) {
  //         memset(ansv, 0, sizeof(ansv)), memset(answ, 0, sizeof(answ));
  //         for (int j = 1; j < 1<<9; j++) {
  //             int msb = __lg(j), prv = reset_(j, msb);
  //             ansv[j] = ansv[prv]+v[i>>msb], answ[j] = answ[prv]+w[i>>msb];
  //             if (answ[j] < mxl) ans[i][answ[j]] = max(ans[i][answ[j]],
  //             ansv[j]);
  //         }
  //         for (int j = 1; j <= mxl-1; j++) {
  //             ans[i][j] = max(ans[i][j], ans[i][j-1]);
  //         }
  //     }
  // }
  cin >> Q;
  while (Q--) {
    int V, L;
    cin >> V >> L;
    if (V < (1 << 9)) {
      cout << ans[V][L] << '\\n';
      continue;
    }
    int tmp = V, cnt = 0;
    while (tmp >= (1 << 9))
      tmp >>= 1, cnt++;
    memset(ansv, 0, sizeof(ansv)), memset(answ, 0, sizeof(answ)),
        memset(ans[0], 0, sizeof(ans[0]));
    for (int j = 1; j < 1 << cnt; j++) {
      int msb = __lg(j), prv = reset_(j, msb);
      ansv[j] = ansv[prv] + v[V >> msb], answ[j] = answ[prv] + w[V >> msb];
      if (answ[j] < mxl)
        ans[0][answ[j]] = max(ans[0][answ[j]], ansv[j]);
    }
    for (int j = 1; j <= mxl - 1; j++) {
      ans[0][j] = max(ans[0][j], ans[0][j - 1]);
    }
    int mx = 0;
    for (int i = 0; i <= L; i++) {
      mx = max(mx, ans[0][i] + ans[tmp][L - i]);
    }
    cout << mx << '\\n';
  }
}