Problem:
Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <vector> 
#include <list> 
 #include <map>
 #include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << '=' << (x) << endl;
#define debug(x) cerr << #x << '=' << (x) << '('<<'L' << __LINE__ << ')' << ' ' << __FILE__ << endl;

const LL MAX_N=123,MAX_W=1234567890;


// LL dp[MAX_N][MAX_W];//N個までの商品を用いてW以内の重さでできる最大の価値
// LL v[MAX_N],w[MAX_N];

// LL N,W;
// int main(){
   
//     CLR(dp);

//     cin>>N>>W;
//     FOR(i,1,N+1){
//         cin>>w[i];
//         cin>>v[i];
//     }

//     FOR(i,1,N+1) FOR(j,1,W+1){
//         if(j<w[i]){
//             dp[i][j]=dp[i-1][j];
//         }
//         else{
//             dp[i][j]=max(dp[i-1][j-w[i]]+v[i],dp[i-1][j]);
//         }
//     }

//     cout<<dp[N][W]<<endl;



// }

LL N,W,Wmin;

vector< vector<LL> > V;
LL w,v;
int main(){
    V=vector< vector<LL> >(4,vector<LL>(1,0));
    
    cin>>N>>W;

    cin>>Wmin>>v;

    V[0].push_back(v);
    FOR(i,1,N){
        cin>>w>>v;
        V[w-Wmin].push_back(v);
        //numW[w-Wmin]++;
    }
    
    
    REP(i,4)sort((V[i]).begin()+1,(V[i]).end(),std::greater<LL>());

    cerr<<endl;
    REP(i,4){REP(j,V[i].size()){cerr<<V[i][j]<<" ";} cerr<<endl;}
    cerr<<endl;

    REP(i,4){
        FOR(j,1,V[i].size()){
            V[i][j]+=V[i][j-1];
        }
    }

    cerr<<endl;
    REP(i,4){REP(j,V[i].size()){cerr<<V[i][j]<<" ";} cerr<<endl;}
    
    cerr<<endl;

    LL sum=0,maxsum=0;

    REP(i,V[0].size())REP(j,V[1].size())REP(k,V[2].size())REP(l,V[3].size()) {
        if(i*Wmin+j*(Wmin+1)+k*(Wmin+2)+l*(Wmin+3)>W){continue;}
        sum=V[0][i]+V[1][j]+V[2][k]+V[3][l];
        cerr<<i<<j<<k<<l<<" "<<sum<<endl;                
        maxsum=max(sum,maxsum);


    }
    cout<<maxsum<<endl;
}