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

#include <iostream>
#include <vector>

template <class T> std::vector<T> vec(int len, T elem) {
  return std::vector<T>(len, elem);
}

constexpr int N = 1 << 9;
constexpr int W = 100000;

void solve() {
  int n;
  std::cin >> n;

  std::vector<std::pair<int, int>> ps(n);
  for (auto &[v, w] : ps)
    std::cin >> v >> w;
  ps.emplace(ps.begin());

  auto dps = vec(N, vec(W + 1, 0));
  for (int i = 1; i < N && i <= n; ++i) {
    auto &dp = dps[i];
    dp = dps[i >> 1];

    auto [v, w] = ps[i];
    for (int x = W; x >= w; --x) {
      dp[x] = std::max(dp[x], dp[x - w] + v);
    }
  }

  int q;
  std::cin >> q;
  while (q--) {
    int i, wmax;
    std::cin >> i >> wmax;

    std::vector<std::pair<int, int>> qs;
    while (i >= N) {
      qs.push_back(ps[i]);
      i >>= 1;
    }

    const auto &dp = dps[i];

    int ans = 0, m = qs.size();
    for (int b = 0; b < (1 << m); ++b) {
      int wsum = 0, vsum = 0;
      for (int j = 0; j < m; ++j) {
        if ((b >> j) & 1)
          continue;
        auto [v, w] = qs[j];
        wsum += w, vsum += v;
      }

      if (wsum > wmax)
        continue;
      ans = std::max(ans, vsum + dp[wmax - wsum]);
    }

    std::cout << ans << "\\n";
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

std::map<int, int> enumerate(const std::vector<std::pair<int, int>> &ps) {
  // std::cerr << "input: \\n";
  // for (auto [w, v] : ps) std::cerr << w << " " << v << "\\n";
  // std::cerr << "\\n";

  int n = ps.size();

  std::map<int, int> ret;
  for (int b = 0; b < (1 << n); ++b) {
    int v = 0, w = 0;

    for (int i = 0; i < n; ++i) {
      if ((b >> i) & 1)
        continue;
      w += ps[i].first;
      v += ps[i].second;
    }

    if (!ret.count(w))
      ret[w] = v;
    ret[w] = std::max(ret[w], v);
  }

  int vmax = -1;
  for (auto it = ret.begin(); it != ret.end();) {
    auto [w, v] = *it;

    if (v <= vmax) {
      it = ret.erase(it);
    } else {
      vmax = v;
      ++it;
    }
  }

  // std::cerr << "output: \\n";
  // for (auto [w, v] : ret) std::cerr << w << " " << v << "\\n";
  // std::cerr << "----------\\n";

  return ret;
}

int query(const std::vector<std::pair<int, int>> &ps, int wmax) {
  int n = ps.size() / 2;

  std::vector<std::pair<int, int>> ps1(ps.begin(), ps.begin() + n),
      ps2(ps.begin() + n, ps.end());

  auto mp1 = enumerate(ps1), mp2 = enumerate(ps2);

  int ret = -1;
  for (auto [w, v] : mp1) {
    auto it = mp2.upper_bound(wmax - w);
    if (it == mp2.begin())
      continue;
    --it;
    ret = std::max(ret, v + it->second);
  }

  return ret;
}

void solve() {
  int n;
  std::cin >> n;

  std::vector<std::pair<int, int>> ps(n);
  for (auto &[w, v] : ps)
    std::cin >> v >> w;

  int q;
  std::cin >> q;
  while (q--) {
    int v, l;
    std::cin >> v >> l;
    --v;

    std::vector<std::pair<int, int>> qs{ps[0]};
    while (v > 0) {
      qs.push_back(ps[v]);
      v = (v - 1) / 2;
    }

    std::cout << query(qs, l) << "\\n";
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  solve();

  return 0;
}