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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
public class Main {
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int W = sc.nextInt();
		Queue A = new PriorityQueue();
		Queue B = new PriorityQueue();
		Queue C = new PriorityQueue();
		Queue D = new PriorityQueue();
		/*int[]A = new int[W];
		int[]B = new int[W];
		int[]C = new int[W];
		int[]D = new int[W];*/
		int w1 = 0;
		for(int i = 0 ; i<N; i++){
			int w = sc.nextInt();
			int v = sc.nextInt();
			if(i == 0 )w1 = w;
			if(w-w1 == 0){
				A.add(v);
				//a++;
			}
			else if(w-w1 == 1){
				//B[b] = v;
				B.add(v);
		//		b++;
			}
			else if(w-w1 == 2){
			//	C[c] = v;
			//	c++;
				C.add(v);
			}
			else if(w-w1 == 3){
		//		D[d] = v;
		//		d++;
				D.add(v);
			}
		}
		/*Arrays.sort(A);
		Arrays.sort(B);
		Arrays.sort(C);
		Arrays.sort(D);
		System.out.println(Arrays.toString(A));
		System.out.println(Arrays.toString(B));
		System.out.println(Arrays.toString(C));
		System.out.println(Arrays.toString(D));*/
		int res = 0;
		
		for(int i = 0 ; i<=A.size(); i++){
			for(int j = 0 ; j<=B.size(); j++){
				for(int x = 0 ; x<=C.size(); x++){
					for(int y = 0 ; y<=D.size(); y++){
						int w = i*w1+j*(w1+1)+x*(w1+2)+y*(w1+3);
						int v = 0;
						if(w<=W){
							int a = i, b = j, c = x, d = y;
							Queue ca = new PriorityQueue(A);
							Queue cb = new PriorityQueue(B);
							Queue cc = new PriorityQueue(C);
							Queue cd = new PriorityQueue(D);
							while(a-->0){
								v+=(int)ca.poll();
							}
							while(b-->0){
								v+=(int)cb.poll();
							}
							while(c-->0){
								v+=(int)cc.poll();
							}
							while(d-->0){
								v+=(int)cd.poll();
							}
						}
						res = Math.max(res, v);
					}
				}
			}
		}
		System.out.println(res);
		/*for(int i = A.length-1 ; i>=0 && A[i]!=0; i--){
			for(int j = B.length-1 ; j>=0 && B[i]!=0 ; j--){
				for(int k = C.length-1; k>=0 && C[i]!=0; k--){
					for(int m = D.length-1 ; m>=0 && D[i]!=0 ; m--){
						if(i*w1+j*(w1+1)+k*(w1+2)+m*(w1+3) <= W ){
							int v = A[i]+B[j]+C[k]+D[m];
							res = Math.max(v, res);
						}
					}
				}
			}
		}*/
	//	System.out.println(res);
	}
}