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 <iostream>
#include <cassert>
#include <climits>
#include <bitset>
#include <stack>
#include <queue>
#include <iomanip>
#include <limits>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>

using namespace std;
typedef long long ll;

typedef pair<ll,ll> P;
long long int INF = 1e18;
double Pi = 3.1415926535897932384626;
long long int mod = 1000000007;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue <ll> pql;
priority_queue <P> pqp;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
//small priority queue
//top pop

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "RULD";
//↓,→,↑,←


#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<x<<" ";
#define ps(x) cout<<fixed<<setprecision(15)<<x<<endl;
#define pu(x) cout<<x;
#define re(i,a,b) for(i=a;i<=b;i++);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout<< #x << " = " << x <<endl


ll rui(ll abc,ll bed){
    //abcのbed乗を計算する
    if(bed==0){return 1;}
    else{
        ll ced = rui(abc,bed/2);
        ced *= ced;
        ced %= mod;
        if(bed%2==1){ced*=abc; ced%=mod;}
        return ced;
    }
}
ll gcd(ll number1,ll number2){
	if(number1 > number2){
		swap(number1,number2);
	}
	if(number1 == 0 || number1 == number2){
		return number2;
	}else{
		return gcd(number2 % number1,number1);
	}
}

ll i,j,k,ii,jj;
ll n,m,num,ans;
ll a,b,c,d,e,g,h,r,bag;
ll wn1,wn2,wn3,wn4;
ll w[5005],v[5005];
ll dp[5][5005];
ll inde[5];

int main(){
    cin >> n >> bag;
	for(i=0;i<n;i++){
		cin >> w[i] >> v[i];
	}
	for(i=0;i<=3;i++){
		inde[i] = 1;
	}
	for(i=0;i<n;i++){
		for(num = 0;num <= 3;num ++){
			if(w[i] == w[0] + num){
				dp[num][inde[num]++] = v[i];
			}
		}
	}
	for(num=0;num<=3;num++){
		sort(dp[num] + 1,dp[num] + inde[num] + 1);
		reverse(dp[num] + 1,dp[num] + inde[num] + 1);
	}
	for(i=0;i<=3;i++){
		for(j=1;j<=inde[i];j++){
			dp[i][j] += dp[i][j-1];
			//pe(dp[i][j]);
		}
		//el;
	}
	for(int num1=0;num1 <= inde[0];num1 ++){
		for(int num2=0;num2 <= inde[1];num2 ++){
			for(int num3=0;num3 <= inde[2];num3++){
				if((bag-w[0]*num1-w[0]*num2-num2-w[0]*num3-2*num3) < 0)continue;
				ll kosuu = (bag-w[0]*num1-w[0]*num2-num2-w[0]*num3-2*num3) / (w[0] + 3);
				ll kati = dp[0][num1] + dp[1][num2] + dp[2][num3] + dp[3][kosuu];
				ans = max(ans,kati);
			}
		}
	}

	p(ans);
    return 0;
}