#include using namespace std; #define all(v) (v).begin(),(v).end() #define pb(a) push_back(a) #define rep(i, n) for(int i=0;i> n >> m >> q; vector a(n), b(n); rep(i, n) cin >> a[i] >> b[i]; ll N = 15; if(n <= N) { ll ans = 0; ll mask = 1; rep(i, n) mask *= 3; rep(bit, mask) { ll sum = 0, summ = 0, sumq = 0; ll num = bit; rep(i, n) { if(num % 3 == 1) { sum += b[i]; summ += a[i]; } else if(num % 3 == 2) { sum += b[i]; sumq += a[i]; } num /= 3; } if(summ <= m and sumq <= q) { ans = max(ans, sum); } } cout << ans << endl; return 0; } ll ans = 0; ll mask = 1; rep(i, N) mask *= 3; map>> dp; rep(bit, mask) { ll sum = 0, summ = 0, sumq = 0; ll num = bit; rep(i, N) { if(num % 3 == 1) { sum += b[i]; summ += a[i]; } else if(num % 3 == 2) { sum += b[i]; sumq += a[i]; } num /= 3; } if(summ <= m and sumq <= q) { ans = max(ans, sum); if(dp.count(summ)) { auto itr = dp[summ].lower_bound({sumq, sum}); while(itr != dp[summ].end()) { if(itr->second < sum) itr = dp[summ].erase(itr); else break; } if(itr != dp[summ].begin()) { itr --; if(itr->second >= sum) continue; } } dp[summ].insert({sumq, sum}); } } rep(i, n - N) mask *= 3; rep(bit, mask) { ll sum = 0, summ = 0, sumq = 0; ll num = bit; for(int i = N; i < n; i ++) { if(num % 3 == 1) { sum += b[i]; summ += a[i]; } else if(num % 3 == 2) { sum += b[i]; sumq += a[i]; } num /= 3; } if(summ <= m and sumq <= q) { ans = max(ans, sum); for(auto [s1, st]: dp) { if(s1 + summ > m) continue; auto itr = st.upper_bound({q - sumq, INF}); if(itr == st.begin()) continue; itr --; ans = max(ans, sum + itr->second); } } } cout << ans << endl; return 0; }