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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void print() { cout << "\\n"; }
template <class T> void print(const T &x) { cout << x << "\\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
template <class T> void printVector(const vector<T> &v) {
  for (const T &x : v) {
    cout << x << " ";
  }
  cout << "\\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n;
  vector<int> V(n), W(n);
  for (int i = 0; i < n; i++) {
    cin >> V[i] >> W[i];
  }
  int M = 1 << 10;
  vector<P> rec1[M];
  rec1[0].emplace_back(0, 0);
  rec1[0].emplace_back(W[0], V[0]);
  for (int i = 1; i < M; i++) {
    if (i >= n) {
      break;
    }
    vector<P> tmp;
    for (auto j : rec1[(i - 1) / 2]) {
      tmp.emplace_back(j.first, j.second);
      tmp.emplace_back(j.first + W[i], j.second + V[i]);
    }
    sort(ALL(tmp));
    int now = -1;
    for (auto j : tmp) {
      if (now < j.second) {
        rec1[i].emplace_back(j);
        now = j.second;
      }
    }
  }
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    v--;
    vector<P> item;
    while (v >= M) {
      item.emplace_back(W[v], V[v]);
      v = (v - 1) / 2;
    }
    vector<P> rec2;
    int N = item.size();
    for (int bit = 0; bit < (1 << N); bit++) {
      int sumw = 0, sumv = 0;
      for (int i = 0; i < N; i++) {
        if (bit & (1 << i)) {
          sumw += item[i].first;
          sumv += item[i].second;
        }
      }
      rec2.emplace_back(sumw, sumv);
    }
    int ans = 0;
    for (auto j : rec2) {
      P p = P(L - j.first, INF);
      auto it = lower_bound(ALL(rec1[v]), p);
      if (it != rec1[v].begin()) {
        it--;
        chmax(ans, it->second + j.second);
      }
    }
    print(ans);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void print() { cout << "\\n"; }
template <class T> void print(const T &x) { cout << x << "\\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
template <class T> void printVector(const vector<T> &v) {
  for (const T &x : v) {
    cout << x << " ";
  }
  cout << "\\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

using P = pair<ll, ll>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int aa, L;
    cin >> aa >> L;
    vector<ll> vv, ww;
    while (aa != 1) {
      // cerr << aa << " ";
      vv.emplace_back(v[aa - 1]);
      ww.emplace_back(w[aa - 1]);
      aa /= 2;
    }
    // cerr << 1 << endl;
    ww.emplace_back(w[0]);
    vv.emplace_back(v[0]);
    int N = vv.size();
    vector<ll> w1, w2, v1, v2;
    for (int i = 0; i < N; i++) {
      if (i < (2 * N / 3)) {
        w1.emplace_back(ww[i]);
        v1.emplace_back(vv[i]);
      } else {
        w2.emplace_back(ww[i]);
        v2.emplace_back(vv[i]);
      }
    }
    int n1 = w1.size(), n2 = w2.size();
    vector<P> rec[2];
    for (int bit = 0; bit < (1 << n1); bit++) {
      ll sumw = 0, sumv = 0;
      for (int i = 0; i < n1; i++) {
        if (bit & (1 << i)) {
          sumw += w1[i];
          sumv += v1[i];
        }
      }
      rec[0].emplace_back(P(sumw, sumv));
    }
    for (int bit = 0; bit < (1 << n2); bit++) {
      ll sumw = 0, sumv = 0;
      for (int i = 0; i < n2; i++) {
        if (bit & (1 << i)) {
          sumw += w2[i];
          sumv += v2[i];
        }
      }
      rec[1].emplace_back(P(sumw, sumv));
    }
    sort(ALL(rec[0]));
    for (int i = 1; i < rec[0].size(); i++) {
      rec[0][i].second = max(rec[0][i].second, rec[0][i - 1].second);
    }
    ll ans = -LLINF;
    for (const auto &a : rec[1]) {
      auto it = lower_bound(ALL(rec[0]), P(L - a.first, LLINF));
      if (it != rec[0].begin()) {
        it--;
        chmax(ans, a.second + it->second);
      }
    }
    cout << ans << "\\n";
  }
}