#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef LOCAL # include "debug_print.hpp" # define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define debug(...) (static_cast(0)) #endif using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define rrep(i,n) for(int i=(n)-1; i>=0; i--) #define FOR(i,a,b) for(int i=(a); i<(b); i++) #define RFOR(i,a,b) for(int i=(b-1); i>=(a); i--) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); #define pb push_back using ll = long long; using D = double; using LD = long double; using P = pair; template using PQ = priority_queue>; template using minPQ = priority_queue, greater>; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const vector &v) { int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template istream &operator>>(istream &is, vector &v) { for (auto &x : v) is >> x; return is; } void in() {} template void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; out(u...); } void outr() {} template void outr(const T &t, const U &...u) { cout << t; outr(u...); } #include "atcoder/segtree.hpp" ll op(ll a, ll b) {return a+b;} ll e() {return 0;} int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,k; ll l,p; in(n,k,l,p); vector a(n),b(n); rep(i,n) in(a[i],b[i]); int half_n = n/2; int half_f = (1 << half_n); int half_s = (1 << (n-half_n)); vector cost_fst(half_f), val_fst(half_f); vector cost_snd(half_s), val_snd(half_s); vector cost_bin, val_bin; rep(bit, half_f){ rep(i,half_n){ if((bit >> i) & 1) { cost_fst[bit] += a[i]; val_fst[bit] += b[i]; } } } rep(i,half_f) { cost_bin.emplace_back(cost_fst[i]); val_bin.emplace_back(val_fst[i]); } sort(ALL(cost_bin)); UNIQUE(cost_bin); sort(ALL(val_bin)); UNIQUE(val_bin); int sz_cost = cost_bin.size(); int sz_val = val_bin.size(); vector> pair_f, pair_s; rep(i,half_f){ pair_f.emplace_back(cost_fst[i], val_fst[i], __builtin_popcount(i)); } rep(bit, half_s){ FOR(i,half_n,n){ if((bit >> (i-half_n)) & 1) { cost_snd[bit] += a[i]; val_snd[bit] += b[i]; } } } rep(i,half_s){ pair_s.emplace_back(cost_snd[i], val_snd[i], __builtin_popcount(i)); } sort(ALL(pair_f)); sort(RALL(pair_s)); vector> st(k+1,atcoder::segtree(sz_val)); int pos = 0; ll ans = 0; rep(i,half_s){ while(pos < half_f and get<0>(pair_f[pos]) + get<0>(pair_s[i]) <= l){ if(get<2> (pair_f[pos]) > k) { pos++; continue; } int val_idx = lower_bound(ALL(val_bin), get<1>(pair_f[pos])) - val_bin.begin(); st[get<2>(pair_f[pos])].set(val_idx, st[get<2>(pair_f[pos])].get(val_idx)+1); pos++; } ll val_min = p-get<1>(pair_s[i]); if(val_min > val_bin.back()) continue; ll num_max = k-get<2>(pair_s[i]); rep(j,num_max+1){ int val_idx = lower_bound(ALL(val_bin), val_min) - val_bin.begin(); ans += st[j].prod(val_idx, sz_val); } } out(ans); }