#include<bits/stdc++.h>

using namespace std;

int knapsack(vector <int> &w,vector <int> &v,int s,int W)
{
    if(s==0||W==0){
        return 0;
    }
    else if(W<w[s-1]){
        return knapsack(w,v,s-1,W);
    }
    else /*if(s>0&&W>=w[s-1])*/{
        return max(knapsack(w,v,s-1,W),v[s-1]+knapsack(w,v,s-1,W-w[s-1]));
    }
}

int main()
{
    int N,W;
    cin >> N >> W;
    vector <int>  w(N);
	vector <int>  v(N);
	for(int i=0;i<N;i++)
        cin >> w[i] >> v[i];
    cout << knapsack(w,v,N,W);
}