/*
By Tianyi Chen. All rights reserved.
Date: 2017-04-29
*/
#include<bits/stdc++.h>
using namespace std;
int n,W,w,v,ans;
map<int,int>wv;
int main(){
 scanf("%d%d",&n,&W);
 wv[0]=0;
 while(n--){
  scanf("%d%d",&w,&v);
  for(auto it=--wv.end();;--it){
   if(it->first+w<=W)wv[it->first+w]=max(wv[it->first+w],it->second+v);
   if(it==wv.begin())break;
  }
 }
 for(auto&&x:wv)ans=max(ans,x.second);
 printf("%d\n",ans);
}