#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main() {
    ll n, W;
    cin >> n >> W;
    ll w[n], v[n];
    for (int i = 0; i < n; i++) {
        cin >> w[i] >> v[i];
    }

    map<ll, ll> mp;
    mp[0LL] = 0LL;
    for (int i = 0; i < n; i++) {
        map<ll, ll> mp2;
        for (map<ll, ll>::iterator ite = mp.begin(); ite != mp.end(); ite++) {
            mp2[(*ite).first] = max(mp2[(*ite).first], (*ite).second);
            if ((*ite).first + w[i] <= W) {
                mp2[(*ite).first + w[i]] = max(mp2[(*ite).first + w[i]], (*ite).second + v[i]);
            }
        }
        swap(mp, mp2);
    }

    ll ans = 0;
    for (map<ll, ll>::iterator ite = mp.begin(); ite != mp.end(); ite++) {
        ans = max(ans, (*ite).second);
    }
    cout << ans << endl;

    return 0;
}
