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

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
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;
}
constexpr int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vector<int> v(N + 1), w(N + 1);
  REP(i, N) { cin >> v[i + 1] >> w[i + 1]; }
  int Q;
  cin >> Q;
  vector<vector<pair<int, int>>> query(N + 1);
  REP(q, Q) {
    int V, W;
    cin >> V >> W;
    query[V].emplace_back(W, q);
  }
  vector<pair<int, int>> vec;
  vector<int> ans(Q);
  vector<int> dp(100010);
  auto f1 = [&](int now) -> void {
    vector<int> v(vec.size() - 10), w(vec.size() - 10);
    REP(i, vec.size() - 10) {
      v[i] = vec[i + 10].first;
      w[i] = vec[i + 10].second;
    }
    REP(i, 1ll << v.size()) {
      int vs = 0, ws = 0;
      REP(j, v.size()) {
        if (!(i & (1ll << j)))
          continue;
        vs += v[j];
        ws += w[j];
      }
      for (auto [W, idx] : query[now]) {
        if (W - ws >= 0)
          CHMAX(ans[idx], dp[W - ws] + vs);
      }
    }
  };
  auto f2 = [&](int now) -> void {
    vector<int> v(vec.size()), w(vec.size());
    REP(i, vec.size()) {
      v[i] = vec[i].first;
      w[i] = vec[i].second;
    }
    REP(i, 1ll << v.size()) {
      int vs = 0, ws = 0;
      REP(j, v.size()) {
        if (!(i & (1ll << j)))
          continue;
        vs += v[j];
        ws += w[j];
      }
      for (auto [W, idx] : query[now]) {
        if (W >= ws)
          CHMAX(ans[idx], vs);
      }
    }
  };
  auto dfs = [&](auto &&f, int now, int dep) -> void {
    vec.emplace_back(v[now], w[now]);
    if (dep == 10) {
      dp = vector<int>(100010, -INF);
      dp[0] = 0;
      REP(i, 1ll << 10) {
        int vs = 0, ws = 0;
        REP(j, 10) {
          if (!(i & (1ll << j)))
            continue;
          auto [value, weight] = vec[j];
          vs += value;
          ws += weight;
        }
        if (ws < 100010)
          CHMAX(dp[ws], vs);
      }
      REP(i, 100009) CHMAX(dp[i + 1], dp[i]);
    }
    if (dep > 10) {
      f1(now);
    } else {
      f2(now);
    }
    REP(k, 2) {
      if (now * 2 + k <= N)
        f(f, now * 2 + k, dep + 1);
    }
    vec.pop_back();
  };
  dfs(dfs, 1, 1);
  REP(i, Q) { cout << ans[i] << "\\n"; }
}

Error: TLE

Fixed submission:


#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
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;
}
constexpr int INF = 1e18;

int W;

void f1(vector<pair<int, int>> &vec, vector<pair<int, int>> &query,
        vector<int> &ans) {
  int N = vec.size();
  int N2 = N / 2;
  vector<int> v(N), w(N);
  REP(i, N) {
    v[i] = vec[i].first;
    w[i] = vec[i].second;
  }
  pair<int, int> ps[1 << ((N + 1) / 2)];
  FOR(i, 0, (1 << N2)) {
    int sw = 0, sv = 0;
    FOR(j, 0, N2) {
      if (i >> j & 1) {
        sw += w[j];
        sv += v[j];
      }
    }
    ps[i] = make_pair(sw, sv);
  }
  sort(ps, ps + (1 << N2));
  int m = 1;
  FOR(i, 1, (1 << N2)) {
    if (ps[m - 1].second < ps[i].second) {
      ps[m++] = ps[i];
    }
  }
  FOR(i, 0, (1 << (N - N2))) {
    int sw = 0, sv = 0;
    FOR(j, 0, (N - N2)) {
      if (i >> j & 1) {
        sw += w[N2 + j];
        sv += v[N2 + j];
      }
    } /*
     if(sw<=W){
         int tv=(lower_bound(ps,ps+m,make_pair(W-sw,INF))-1)->second;
         CHMAX(ans,sv+tv);
     }*/
    for (auto [W, idx] : query) {
      if (sw <= W) {
        int tv = (lower_bound(ps, ps + m, make_pair(W - sw, INF)) - 1)->second;
        CHMAX(ans[idx], sv + tv);
      }
    }
  }
}

signed main() {
  int N;
  cin >> N;
  vector<int> v(N + 1), w(N + 1);
  REP(i, N) { cin >> v[i + 1] >> w[i + 1]; }
  int Q;
  cin >> Q;
  vector<vector<pair<int, int>>> query(N + 1);
  REP(i, Q) {
    int V, L;
    cin >> V >> L;
    query[V].emplace_back(L, i);
  }
  vector<pair<int, int>> vec;
  vector<int> ans(Q);
  auto dfs = [&](auto &&f, int now) -> void {
    vec.emplace_back(v[now], w[now]);
    f1(vec, query[now], ans);
    REP(k, 2) {
      int to = now * 2 + k;
      if (to > N)
        continue;
      f(f, to);
    }
    vec.pop_back();
  };
  dfs(dfs, 1);
  REP(i, Q) out(ans[i]);
}