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.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicLong;

public class Main {

	static int first = -1;
	static int w = -1;

	// 14:33-
	public static void main(String[] args) {
		Scanner sc = new Scanner(new BufferedInputStream(System.in));
		int n = sc.nextInt();
		w = sc.nextInt();

		ArrayList[] ws = new ArrayList[4];
		for (int i = 0; i < ws.length; i++) ws[i] = new ArrayList();

		for (int i = 0; i < n; i++) {
			int wi = sc.nextInt();
			if (i == 0) first = wi;
			int vi = sc.nextInt();

			ws[wi - first].add(vi);
		}

		for (int i = 0; i < ws.length; i++) Collections.sort(ws[i]);

		ArrayList[] prf = new ArrayList[4];
		for (int i = 0; i < prf.length; i++) {
			prf[i] = new ArrayList();
			prf[i].add(0L);
		}

		for (int i = 0; i < ws.length; i++) {
			int k = 1;
			int lastElem = ws[i].size() - 1;
			for (int j = lastElem; j >= 0; j--) {
				long val = (Integer) ws[i].get(j);
				val += (long) prf[i].get(k-1);
				prf[i].add(val);
				k++;
			}
		}


		AtomicLong max = new AtomicLong();
		int[] choice = new int[4];
		doit(choice, ws, prf, 0, max);

		System.out.println(max.get());
	}

	private static void doit(int[] choice, ArrayList[] ws, ArrayList[] prf, int pos, AtomicLong max) {
		if (pos == ws.length) {
			long avail = w;
			long worth = 0;
			for (int i = 0; i < choice.length; i++) {
				avail -= choice[i] * (first + i);
				if (avail < 0) break;
				for (int j = 0; j < choice[i]; j++) worth += (Integer) ws[i].get(ws[i].size() - 1 - j);
//				worth += (long) prf[i].get(choice[i]);
			}
			if (avail >= 0) max.set(Math.max(worth, max.get()));
			return;
		}

		for (int i = 0; i <= ws[pos].size(); i++) {
			choice[pos] = i;
			doit(choice, ws, prf, pos + 1, max);
		}
	}
}