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 <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define itrfor(itr,A) for(auto itr = A.begin(); itr !=A.end();itr++)
typedef long long llong;
char moji[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char moji2[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char moji3[10]={'0','1','2','3','4','5','6','7','8','9'};
#define Sort(a) sort(a.begin(),a.end());
#define Reverse(a) reverse(a.begin(),a.end());
#define ld PRId64
#define n_max 100


bool comp(pair<llong,llong> a,pair<llong,llong>b){
    if(a.first != b.first) return a.first < b.first;
    return a.second > b.second;
}

int main(){
    int n,W;
    cin >> n >>W;
    pair<llong,llong> goods[n_max+5];
    llong w,v;
    set<int> judge;
    REP(i,n){
        cin >>w >> v;
        judge.insert(w);
        goods[i]= make_pair(w,v);
    }
    REP(i,4-judge.size()) goods[n+i]=make_pair(w+5+i,0);
    n+=4-judge.size();
    sort(goods,goods+n,comp);

    int ind=0;
    llong ru1[n_max+5]={},ru2[n_max+5]={},ru3[n_max+5]={},ru4[n_max+5]={};
    int ru1_size,ru2_size,ru3_size,ru4_size;
    llong w1,w2,w3,w4;
    int i=1;
    w1=goods[ind].first;
    while(1){
        ru1[i] = ru1[i-1] + goods[ind].second;
        if(goods[ind].first!=goods[ind+1].first) break;
        i+=1;
        ind+=1; 
    }
    ru1_size=i+1;
    ind+=1;

    i=1;
    w2=goods[ind].first;
    while(1){
        
        ru2[i] = ru2[i-1] + goods[ind].second;
        if(goods[ind].first!=goods[ind+1].first) break;
        i+=1;
        ind+=1; 
    }
    ru2_size=i+1;
    ind+=1;

    i=1;
    w3=goods[ind].first;
    while(1){

        ru3[i] = ru3[i-1] + goods[ind].second;
        if(goods[ind].first!=goods[ind+1].first) break;
        i+=1;
        ind+=1; 
    }
    ru3_size=i+1;
    ind+=1;

    i=1;
    w4=goods[ind].first;
    while(1){
        ru4[i] = ru4[i-1] + goods[ind].second;
        if(goods[ind].first!=goods[ind+1].first) break;
        ind+=1; 
        i+=1;
    }
    ru4_size=i+1;
    ind+=1;






    llong ans=0;
    REP(i,ru1_size){
        if(w1*i> W) break;
        REP(j,ru2_size){
            if(w1*i + w2*j > W) break;
            REP(ii,ru3_size){
                if(w1*i + w2*j + w3 * ii > W) break;
                REP(jj,ru4_size){
                    if(w1*i + w2*j + w3 * ii + w4 *jj > W) break;
                    ans=max(ans,ru1[i] + ru2[j] + ru3[ii] + ru4[jj]);
                }
            }
        }
    }

    cout << ans << endl;


}