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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  vl v(n + 1), w(n + 1);
  ll k = log2(n) + 1;
  ll mid = k / 2;
  repl(i, 1, n + 1) { cin >> v[i] >> w[i]; }
  vvl g((1 << mid), vl(100010, 0));
  repl(i, 1, (1 << mid)) {
    rep(j, 100010) {
      if (j + w[i] <= 100000)
        chmax(g[i][j + w[i]], g[i / 2][j] + v[i]);
      chmax(g[i][j], g[i / 2][j]);
    }
  }
  ll q;
  cin >> q;
  while (q--) {
    ll s, l;
    cin >> s >> l;
    if (s < (1 << mid)) {
      cout << g[s][l] << endl;
      continue;
    }
    ll res = 0;
    vpl use;
    while (s >= (1 << mid)) {
      use.pb({w[s], v[s]});
      s /= 2;
    }
    ll t = use.size();
    vl tv, tw;
    rep(bit, 1 << t) {
      ll a = 0, b = 0;
      rep(i, t) {
        if (bit & (1 << i)) {
          a += use[i].fi;
          b += use[i].se;
        }
      }
      tw.pb(a);
      tv.pb(b);
    }
    rep(i, tw.size()) {
      if (tw[i] > l)
        continue;
      chmax(res, tv[i] + g[s][l - tw[i]]);
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll n;
  cin >> n;
  vl v(n + 1), w(n + 1);
  repl(i, 1, n + 1) { cin >> v[i] >> w[i]; }
  ll q;
  cin >> q;
  while (q--) {
    ll s, l;
    cin >> s >> l;
    vl itv, itw;
    while (s) {
      itv.pb(v[s]), itw.pb(w[s]);
      s /= 2;
    }
    vpl a, b; // a.pb({0,0});b.pb({0,0});
    rep(bit, 1 << (itv.size() / 2)) {
      ll tv = 0, tw = 0;
      rep(i, itv.size() / 2) {
        if (bit & (1 << i)) {
          tv += itv[i];
          tw += itw[i];
        }
      }
      a.pb({tv, tw});
    }
    // rev(all(itv));rev(all(itw));
    rep(bit, 1 << (itv.size() - itv.size() / 2)) {
      ll tv = 0, tw = 0;
      rep(i, itv.size() - itv.size() / 2) {
        if (bit & (1 << i)) {
          tv += itv[itv.size() - 1 - i];
          tw += itw[itv.size() - 1 - i];
        }
      }
      b.pb({tw, tv});
    }
    sort(all(b));
    ll res = 0;
    rep(i, b.size() - 1) { chmax(b[i + 1].se, b[i].se); }
    vl btw, btv;
    rep(i, b.size()) {
      btw.pb(b[i].fi);
      btv.pb(b[i].se);
    }
    for (auto p : a) {
      ll x = l - p.se;
      if (x < 0)
        continue;
      ll pls = upper_bound(all(btw), x) - btw.begin();
      pls--;
      chmax(res, btv[pls] + p.fi);
    }
    printf("%lld\\n", res);
  }
}