#include #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; template ostream& operator<<(ostream& os, const vector& v) { os << "sz:" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template ostream& operator<<(ostream& os, const pair& p) { os << "(" << p.first << "," << p.second << ")"; return os; } struct Product { ll v; ll w; bool operator<(const Product& p) const { return v * p.w < w * p.v; } bool operator>(const Product& p) const { return v * p.w > w * p.v; } }; ostream& operator<<(ostream& os, const Product& p) { os << "<" << p.v << ", " << p.w << ">"; return os; } ll ans = 0; int N; ll W; void dfs(const vector& product, const int ind, const ll weight, const ll value) { if (ind == N) { ans = max(ans, value); return; } if (weight + product[ind].w <= W) { ll relaxed = value + product[ind].v; ll use = weight + product[ind].w; for (int i = ind + 1; i < N; i++) { if (use + product[i].w <= W) { use += product[i].w; relaxed += product[i].v; } else { use = W; relaxed += (product[i].v * (W - use)) / product[i].w; break; } } if (relaxed + value > ans) { dfs(product, ind + 1, weight + product[ind].w, value + product[ind].v); } } ll relaxed = value; ll use = weight; for (int i = ind + 1; i < N; i++) { if (use + product[i].w <= W) { use += product[i].w; relaxed += product[i].v; } else { use = W; relaxed += (product[i].v * (W - use)) / product[i].w; break; } } if (relaxed + value > ans) { dfs(product, ind + 1, weight, value); } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> W; vector product; for (int i = 0; i < N; i++) { ll v, w; cin >> v >> w; if (w > W) { continue; } product.push_back(Product{v, w}); } N = product.size(); sort(product.begin(), product.end(), greater{}); dfs(product, 0, 0, 0); cout << ans << endl; return 0; }