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 (Clang 3.8.0) (judge verdict: Runtime Error)

#include<iostream>  
#include<algorithm>  
#include<cstdio>
#include<cstring>  
using namespace std;
int n,m,a1,b1,c1,d1;
struct node
{
	int w,v;
}e[110];
bool cmp(node a,node b)  
{  
    return a.v > b.v;  
}  
node a[110],b[110],c[110],d[110];
long long qzwa[110],qzwb[110],qzwc[110],qzwd[110];
long long qzva[110],qzvb[110],qzvc[110],qzvd[110];
long long s,ans;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
	cin>>e[i].w>>e[i].v;
	if(e[i].w==e[1].w) a[++a1]=e[i];  
    if(e[i].w==e[1].w+1)b[++b1]=e[i];  
    if(e[i].w==e[1].w+2)c[++c1]=e[i];  
    if(e[i].w==e[1].w+3)d[++d1]=e[i];  
	}
	
	sort(a+1,a+a1+1,cmp);  
    sort(b+1,b+b1+1,cmp);  
    sort(c+1,c+c1+1,cmp);  
    sort(d+1,c+d1+1,cmp);  
	
	for(int i=1;i<=a1;i++)
	{
		qzwa[i]=qzwa[i-1]+a[i].w;
		qzva[i]=qzva[i-1]+a[i].v;	
	}
		for(int i=1;i<=b1;i++)
	{
		qzwb[i]=qzwb[i-1]+b[i].w;
		qzvb[i]=qzvb[i-1]+b[i].v;	
	}
	for(int i=1;i<=c1;i++)
	{
		qzwc[i]=qzwc[i-1]+c[i].w;
		qzvc[i]=qzvc[i-1]+c[i].v;	
	}
		for(int i=1;i<=d1;i++)
	{
		qzwd[i]=qzwd[i-1]+d[i].w;
		qzvd[i]=qzvd[i-1]+d[i].v;	
	}
	for(int i=0;i<=a1;i++)   
    for(int j=0;j<=b1;j++)
	for(int k=0;k<=c1;k++)
	for(int l=0;l<=d1;l++)  
    {
    s=qzwa[i]+qzwb[j]+qzwc[k]+qzwd[l];  
    if(s<=m)  
	ans=max(ans,qzva[i]+qzvb[j]+qzvc[k]+qzvd[l]);         
    }  
    cout<<ans;
	return 0;
}