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.Arrays;
import java.util.Scanner;

class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int W = sc.nextInt();
		int[] w = new int[N];
		int[] v = new int[N];
		for(int i=0; i<N; i++){
			w[i] = sc.nextInt();
			v[i] = sc.nextInt();

		}

		int[] w0 = new int[N];
		int[] v0 = new int[N];
		int w0_cnt = 0;
		int[] w1 = new int[N];
		int[] v1 = new int[N];
		int w1_cnt = 0;
		int[] w2 = new int[N];
		int[] v2 = new int[N];
		int w2_cnt = 0;
		int[] w3 = new int[N];
		int[] v3 = new int[N];
		int w3_cnt = 0;
		for(int i=0; i<N; i++){
			if(w[i]==w[0]){
				w0[w0_cnt] = w[i];
				v0[w0_cnt] = v[i];
				w0_cnt++;
			}else if(w[i]==w[0]+1){
				w1[w1_cnt] = w[i];
				v1[w1_cnt] = v[i];
				w1_cnt++;
			}else if(w[i]==w[0]+2){
				w2[w2_cnt] = w[i];
				v2[w2_cnt] = v[i];
				w2_cnt++;
			}else if(w[i]==w[0]+3){
				w3[w3_cnt] = w[i];
				w3[w3_cnt] = v[i];
				w3_cnt++;
			}
		}

		Arrays.sort(v0);
		long[] v0_sum = new long[w0_cnt+1];
		v0_sum[0] = 0;
		if(w0_cnt!=0){
			for(int i=1; i<=w0_cnt; i++){
				v0_sum[i] += v0_sum[i-1] + v0[N-i];
			}
		}
		Arrays.sort(v1);
		long[] v1_sum = new long[w1_cnt+1];
		v1_sum[0] = 0;
		if(w2_cnt!=0){
			for(int i=1; i<=w1_cnt; i++){
				v1_sum[i] += v1_sum[i-1] + v1[N-i];
			}
		}
		Arrays.sort(v2);
		long[] v2_sum = new long[w2_cnt+1];
		v2_sum[0] = 0;
		if(w2_cnt!=0){
			for(int i=1; i<=w2_cnt; i++){
				v2_sum[i] += v2_sum[i-1] + v2[N-i];
			}
		}
		Arrays.sort(v3);
		long[] v3_sum = new long[w3_cnt+1];
		v3_sum[0] = 0;
		if(w3_cnt!=0){
			for(int i=1; i<=w3_cnt; i++){
				v3_sum[i] += v3_sum[i-1] + v3[N-i];
			}
		}

		int l;
		long sum = 0;
		long max = 0;
		for(int i=0; i<=W/w[0] && i<= w0_cnt; i++){
			for(int j=0; j<=(W-w[0]*i)/(w[0]+1) && j<= w1_cnt; j++){
				for(int k=0; k<=(W-w[0]*i-(w[0]+1)*j)/(w[0]+2) && k<= w2_cnt; k++){
					l = (int)((W - w[0]*i - (w[0]+1)*j - (w[0]+2)*k ) / (w[0]+3));
					if(l>w3_cnt){
						l = w3_cnt;
					}
					sum = v0_sum[i] + v1_sum[j] + v2_sum[k]+ v3_sum[(int)l]; 
					if(sum>max){
						max = sum;
					}
				}
			}
		}

		System.out.print(max);

	}

}