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

#include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;

struct item {
  int v, w;
  item(int v = 0, int w = 0) : v(v), w(w) {}
  bool operator<(const item &r) const { return w < r.w; }
  void operator+=(const item &r) { w += r.w, v += r.v; }
};

vector<item> enum_powerset(const vector<item> &is) {
  vector<item> a(1);
  for (auto &i : is) {
    auto b = a;
    for (auto &e : b)
      e += i;
    vector<item> c;
    merge(all(a), all(b), back_inserter(c));
    swap(a, c);
  }
  return a;
}

int solve(const vector<item> &is, int W) {
  int n = sz(is);
  auto a = enum_powerset({is.begin(), is.begin() + n / 2});
  auto b = enum_powerset({is.begin() + n / 2, is.end()});
  reverse(all(a));

  int ans = 0;
  int vbmax = -INF;
  auto itr = b.begin();
  for (auto &[va, wa] : a) {
    while (itr != b.end() && wa + itr->w <= W) {
      chmax(vbmax, itr->v);
      ++itr;
    }
    chmax(ans, va + vbmax);
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<item> is(n);
  for (auto &[v, w] : is)
    cin >> v >> w;

  int q;
  cin >> q;
  rep(_, q) {
    int v, l;
    cin >> v >> l;
    vector<item> qi;
    while (v) {
      qi.push_back(is[v - 1]);
      v /= 2;
    }
    cout << solve(qi, l) << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using i128 = boost::multiprecision::int128_t;
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using VVV = vector<VV>;
using VVVll = vector<VVll>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
#define rep2(i, m, n) for (ll i = int(m); i < int(n); ++i)
#define drep2(i, m, n) for (ll i = int(m) - 1; i >= int(n); --i)
#define rep(i, n) rep2(i, 0, n)
#define drep(i, n) drep2(i, n, 0)
#define all(a) a.begin(), a.end()
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &e : v)
    os << e << " ";
  return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
  return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int Log2(ll x) {
  int k;
  for (k = 0; x > 0; ++k)
    x >>= 1;
  return k;
} // number of binary digits
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const ld PI = acos(-1.0);
const int MOD = int(1e9) + 7;
// const int MOD  = 998244353;

int main() {
  ll n;
  cin >> n;

  V v(n), w(n);
  rep2(i, 1, n + 1) cin >> v[i] >> w[i];

  int q;
  cin >> q;
  rep(_, q) {
    int x, L;
    cin >> x >> L;
    vector<P> vp({{L, 0}});
    vector<P> vp2({{0, 0}});
    int y = x;
    while (x >= (1 << 9)) {
      int k = vp.size();
      rep(i, k) vp.emplace_back(vp[i].first - w[x], vp[i].second + v[x]);
      x /= 2;
    }
    sort(all(vp));
    drep(i, vp.size() - 1) chmax(vp[i].second, vp[i + 1].second);

    while (x) {
      int k = vp2.size();
      rep(i, k) vp2.emplace_back(vp2[i].first + w[x], vp2[i].second + v[x]);
      x /= 2;
    }

    int ans = 0;
    for (auto &[a, b] : vp2) {
      auto z = lower_bound(all(vp), make_pair(a, 0));
      if (z != vp.end())
        chmax(ans, (*z).second + b);
    }
    cout << ans << "\\n";
  }

  return 0;
}