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;
int w[105],v[105];
int w1[105],w2[105],w3[105],w4[105];
int x1,x2,x3,x4;
int cnt1=1,cnt2=1,cnt3=1,cnt4=1;
bool cmp(int x,int y)
{
	return x>y;
}
int main()
{
	int n,W;
	cin>>n>>W;
	for(int i=0;i<n;i++)
	{
		cin>>w[i]>>v[i];
		if(w[i]==w[0])
		{
			w1[cnt1++]=v[i];
		}
		if(w[i]==w[0]+1)
		{
			w2[cnt2++]=v[i];
		}
		if(w[i]==w[0]+2)
		{
			w3[cnt3++]=v[i];
		}
		if(w[i]==w[0]+3)
		{
			w4[cnt4++]=v[i];
		}
	}
	sort(w1+1,w1+cnt1,cmp);
	sort(w2+1,w2+cnt2,cmp);
	sort(w3+1,w3+cnt3,cmp);
	sort(w4+1,w4+cnt4,cmp);
	for(int i=2;i<cnt1;i++) w1[i]+=w1[i-1];
	for(int i=2;i<cnt2;i++) w2[i]+=w2[i-1];
	for(int i=2;i<cnt3;i++) w3[i]+=w3[i-1];
	for(int i=2;i<cnt4;i++) w4[i]+=w4[i-1];
	x1=w[0],x2=x1+1,x3=x2+1,x4=x3+1;
//	cout<<cnt1<<" "<<cnt2<<" "<<cnt
	int ma=-1;
	for(int i1=0;i1<=cnt1;i1++)
	{
		for(int i2=0;i2<=cnt2;i2++)
		{
			for(int i3=0;i3<=cnt3;i3++)
			{
				for(int i4=0;i4<=cnt4;i4++)
				{
					if(1ll*i1*x1+1ll*i2*x2+1ll*i3*x3+1ll*i4*x4>1ll*W) continue;
					ma=max(w1[i1]+w2[i2]+w3[i3]+w4[i4],ma);
				}
			}
		}
	}
	cout<<ma;
}