#include #include #include #include #include std::vector>> enumerate_all( const size_t N, const size_t K, const std::vector w, const std::vector v ) { std::vector>> res(K + 1); res[0].emplace_back(0, 0); for (size_t i = 0; i < N; ++i) { std::vector mid(K + 1); for (size_t j = 1; j <= K; ++j) { mid[j] = res[j].size(); } for (size_t j = K; j --> 0;) { for (const auto& [wsum, vsum] : res[j]) { res[j + 1].emplace_back(wsum + w[i], vsum + v[i]); } } for (size_t j = 1; j <= K; ++j) { std::inplace_merge(res[j].begin(), res[j].begin() + mid[j], res[j].end()); } } return res; } int64_t solve( const size_t N, const size_t K, const int64_t W, const int64_t V, const std::vector w, const std::vector v ) { const size_t H = N / 2; auto L = enumerate_all( H, K, std::vector(w.begin(), w.begin() + H), std::vector(v.begin(), v.begin() + H) ); auto R = enumerate_all( N - H, K, std::vector(w.begin() + H, w.end()), std::vector(v.begin() + H, v.end()) ); int64_t ans = 0; for (size_t j = 0; j <= K; ++j) { std::vector comp_v; for (const auto &e : R[j]) { comp_v.emplace_back(e.second); } std::sort(comp_v.begin(), comp_v.end()); comp_v.erase(std::unique(comp_v.begin(), comp_v.end()), comp_v.end()); for (auto &e : R[j]) { e.second = std::lower_bound(comp_v.begin(), comp_v.end(), e.second) - comp_v.begin(); } const size_t m = comp_v.size(); for (size_t i = 0; i + j <= K; ++i) { atcoder::fenwick_tree ft(m); auto itR = R[j].begin(); for (auto itL = L[i].rbegin(); itL != L[i].rend(); ++itL) { auto [wsum, vsum] = *itL; int64_t maxw = W - wsum; for (; itR != R[j].end() and itR->first <= maxw; ++itR) { ft.add(itR->second, 1); } int64_t maxv = V - vsum; std::size_t l = std::lower_bound(comp_v.begin(), comp_v.end(), maxv) - comp_v.begin(); ans += ft.sum(l, m); } } } return ans; } int main() { size_t N, K; int64_t W, V; std::cin >> N >> K >> W >> V; std::vector w(N), v(N); for (size_t i = 0; i < N; ++i) { std::cin >> w[i] >> v[i]; } std::cout << solve(N, K, W, V, w, v) << std::endl; }