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

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;
template <class T> inline void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> inline void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> inline bool kbit(const T &x, const int &k) {
  return ((x >> k) & 1LL);
}
inline int popcount(const int &n) { return __builtin_popcount(n); }
inline ll popcountll(const ll &n) { return __builtin_popcountll(n); }
inline ll mask(const ll &k) { return (1LL << k) - 1LL; }
template <class T> void zip(V<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <class T> int lwb(V<T> &v, const T &x) {
  return lower_bound(all(v), x) - v.begin();
}
void dump() { cerr << '\\n'; }
template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) {
  cerr << head << (sizeof...(Tail) == 0 ? " " : ", ");
  dump(std::move(tail)...);
}
template <class T> void print(const vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << (i + 1 == v.size() ? '\\n' : ' ');
}
template <class T> void read(vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
}
constexpr char sp = ' ', newl = '\\n';
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 1e9 + 7;
//////////////////////////////////////////////INSERT ABOVE HERE
ll vv[1 << 9][100010];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  V<ll> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  int lim = (1 << 9);
  for (int i = 1; i < min(lim, n + 1); i++) {
    V<int> is;
    int id = i;
    while (id >= 1) {
      is.eb(id);
      id /= 2;
    }
    V<P> vs;
    for (int j = 0; j < (1 << is.size()); j++) {
      ll sw = 0, sv = 0;
      for (int k = 0; k < is.size(); k++) {
        if (kbit(j, k)) {
          sw += w[is[k]];
          sv += v[is[k]];
        }
      }
      vs.eb(sw, sv);
    }
    sort(all(vs));
    int now = 0;
    for (int j = 0; j <= 100000; j++) {
      if (j > 0)
        chmax(vv[i][j], vv[i][j - 1]);
      while (now < vs.size() && vs[now].fi == j) {
        chmax(vv[i][j], vs[now].se);
        now++;
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int id, l;
    cin >> id >> l;
    ll res = 0;
    if (id < lim) {
      res = vv[id][l];

    } else {
      V<int> is;
      while (id >= lim) {
        is.eb(id);
        id /= 2;
      }
      for (int j = 0; j < (1 << is.size()); j++) {
        ll sw = 0, sv = 0;
        for (int k = 0; k < is.size(); k++) {
          if (kbit(j, k)) {
            sw += w[is[k]];
            sv += v[is[k]];
          }
        }
        if (sw > l)
          continue;
        ll s = sv + vv[id][l - sw];
        chmax(res, s);
      }
    }
    cout << res << newl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;
template <class T> inline void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> inline void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> inline bool kbit(const T &x, const int &k) {
  return ((x >> k) & 1LL);
}
inline int popcount(const int &n) { return __builtin_popcount(n); }
inline ll popcountll(const ll &n) { return __builtin_popcountll(n); }
inline ll mask(const ll &k) { return (1LL << k) - 1LL; }
template <class T> void zip(V<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <class T> int lwb(V<T> &v, const T &x) {
  return lower_bound(all(v), x) - v.begin();
}
void dump() { cerr << '\\n'; }
template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) {
  cerr << head << (sizeof...(Tail) == 0 ? " " : ", ");
  dump(std::move(tail)...);
}
template <class T> void print(const vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << (i + 1 == v.size() ? '\\n' : ' ');
}
template <class T> void read(vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
}
constexpr char sp = ' ', newl = '\\n';
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 1e9 + 7;
//////////////////////////////////////////////INSERT ABOVE HERE
V<P> vs[1 << 10];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  V<ll> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  int lim = (1 << 9);
  for (int i = 1; i < min(lim, n + 1); i++) {
    V<int> is;
    int id = i;
    while (id >= 1) {
      is.eb(id);
      id /= 2;
    }
    for (int j = 0; j < (1 << is.size()); j++) {
      ll sw = 0, sv = 0;
      for (int k = 0; k < is.size(); k++) {
        if (kbit(j, k)) {
          sw += w[is[k]];
          sv += v[is[k]];
        }
      }
      vs[i].eb(sw, sv);
    }
    sort(all(vs[i]));
    for (int j = 1; j < vs[i].size(); j++) {
      chmax(vs[i][j].se, vs[i][j - 1].se);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int id, l;
    cin >> id >> l;
    ll res = 0;
    if (id < lim) {
      int to = lwb(vs[id], P(l + 1, -1)) - 1;
      if (to >= 0) {
        res = vs[id][to].se;
      }
    } else {
      V<int> is;
      while (id >= lim) {
        is.eb(id);
        id /= 2;
      }
      for (int j = 0; j < (1 << is.size()); j++) {
        ll sw = 0, sv = 0;
        for (int k = 0; k < is.size(); k++) {
          if (kbit(j, k)) {
            sw += w[is[k]];
            sv += v[is[k]];
          }
        }
        if (sw > l)
          continue;
        int to = lwb(vs[id], P(l - sw + 1, -1)) - 1;
        ll s = sv;
        if (to >= 0) {
          s += vs[id][to].se;
        }
        chmax(res, s);
      }
    }
    cout << res << newl;
  }
}