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++ (GCC 9.2.1) (judge verdict: Accepted)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 110;

int n,cnt;
ll W,mi,ans;
int cn[10];
ll val[10][maxn],w[maxn],cw[maxn],wei[10],v[maxn],pre[10][maxn];

bool cmp(ll a,ll b){
	return a>b;
}

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	mi = 1e9 + 7;
	ans = 0;
	n = read(),W = read(); 
	for(int i=1;i<=n;i++){
		w[i] = read(); cw[i] = w[i];
		mi = min(mi, w[i]);
		v[i] = read();
	}
	
	sort(cw+1,cw+1+n);
	
	for(int i = 1;i <= n;i++){
		if(cw[i] != cw[i-1]){
			wei[++cnt] = cw[i];
		}
	}
	
	for(int i=1;i<=n;i++){
		int nt = w[i]-mi+1;
		val[nt][++cn[nt]] = v[i];
	}
	
	for(int i=1;i<=cnt;i++){
		sort(val[i]+1,val[i]+1+cn[i],cmp);
	}
	
	for(int i=1 ;i <= cnt;i++){
		for(int j = 1 ;j<=cn[i]; j++){
			pre[i][j]=val[i][j];
			pre[i][j]+=pre[i][j-1];
		}
	}
	
	if(cnt==4){
		for(int i=0 ;i <= cn[1];i++){
			for( int j= 0;j <= cn[2] ;j++){
				for( int k=0 ;k <= cn[3] ;k++){
					for(int p = 0; p <= cn[4] ; p++){
						if(i*wei[1]+j*wei[2]+k*wei[3]+p*wei[4]<=W){
							ans =max( pre[1][i] + pre[2][j] + pre[3][k] + pre[4][p],ans);
						}
						
					}
				}
			}
		}
	}
		
	if(cnt==3){
		for(int i=0 ;i <= cn[1];i++){
			for( int j= 0;j <= cn[2] ;j++){
				for( int k=0 ;k <= cn[3] ;k++){
					if(i*wei[1]+j*wei[2]+k*wei[3]<=W){
						ans =max( pre[1][i] + pre[2][j] + pre[3][k],ans);
					}
				}
			}
		}
	}
	
		
	if(cnt==2){
		for(int i=0 ;i <= cn[1];i++){
			for( int j= 0;j <= cn[2] ;j++){
				if(i*wei[1]+j*wei[2]<=W){
					ans =max( pre[1][i] + pre[2][j] ,ans);	
				}
			}
		}
	}
	
	if(cnt==1){
		for(int i=0 ;i <= cn[1];i++){
			if(i*wei[1]<=W){
				ans =max( pre[1][i] ,ans);
			}
		}
	}
	
	printf("%lld\n",ans);
	
	return 0;
}