#include #include #include using namespace std; long long N, W, v[5009], w[5009]; vector>vec, vec2; int main() { cin >> N >> W; for (int i = 1; i <= N; i++) { cin >> v[i] >> w[i]; } vec.push_back(make_pair(0, 0)); for (int i = 1; i <= N; i++) { for (int j = 0; j < vec.size(); j++) { vec2.push_back(vec[j]); vec2.push_back(make_pair(vec[j].first + w[i], vec[j].second + v[i])); } sort(vec2.begin(), vec2.end()); vec.clear(); long long maxn = -1; for (int j = 0; j < vec2.size(); j++) { if (maxn < vec2[j].second) { maxn = vec2[j].second; vec.push_back(vec2[j]); } } vec2.clear(); } long long T = 0; for (int i = 0; i < vec.size(); i++) { if (vec[i].first <= W) T = vec[i].second; } cout << T << endl; return 0; }