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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using System.Runtime.CompilerServices;
using static MyUtil;

class MyUtil
{
    public static int[] ReadIntArray()
    {
	return ReadLine().Split().Select(x => int.Parse(x)).ToArray();
    }
}

class Program
{
    public static void Main()
    {
	var tmp = ReadIntArray();
	int n = tmp[0], w = tmp[1];

	int nw0 = 0, nw1 = 0, nw2 = 0, nw3 = 0;
	long[] vs0 = new long[n];
	long[] vs1 = new long[n];
	long[] vs2 = new long[n];
	long[] vs3 = new long[n];

	tmp = ReadIntArray();
	long w1 = tmp[0], v1 = tmp[1];
	nw0++;
	vs0[0] = v1;

	for (int i = 1; i < n; i++)
	{
	    tmp = ReadIntArray();
	    int ww = tmp[0], vv = tmp[1];

	    if      (ww == w1 + 0) vs0[nw0++] = vv;
	    else if (ww == w1 + 1) vs1[nw1++] = vv;
	    else if (ww == w1 + 2) vs2[nw2++] = vv;
	    else                   vs3[nw3++] = vv; 
	}

	Array.Resize(ref vs0, nw0); 
	Array.Resize(ref vs1, nw1);
	Array.Resize(ref vs2, nw2);
	Array.Resize(ref vs3, nw3);

	Array.Sort(vs0); Array.Reverse(vs0);
	Array.Sort(vs1); Array.Reverse(vs1);
	Array.Sort(vs2); Array.Reverse(vs2);
	Array.Sort(vs3); Array.Reverse(vs3);

	for (int i = 1; i < vs0.Length; i++){ vs0[i] += vs0[i-1]; }
	for (int i = 1; i < vs1.Length; i++){ vs1[i] += vs1[i-1]; }
	for (int i = 1; i < vs2.Length; i++){ vs2[i] += vs2[i-1]; }
	for (int i = 1; i < vs3.Length; i++){ vs3[i] += vs3[i-1]; }

	long ans = 0;

	for (int i = 0; i <= Math.Min(n, vs0.Length); i++)
	{
	    for (int j = 0; j <= Math.Min(n - i, vs1.Length); j++)
	    {
		for (int k = 0; k <= Math.Min(n - i - j, vs2.Length); k++)
		{
		    for (int l = 0; l <= Math.Min(n - i - j - k, vs3.Length); l++)
		    {
			long weight =
			    w1 * i + (w1+1)*j + (w1+2)*k + (w1+3)*l;
			if (weight <= w)
			{
			    long value = 0;
			    if (i > 0) value += vs0[i-1];
			    if (j > 0) value += vs1[j-1];
			    if (k > 0) value += vs2[k-1];
			    if (l > 0) value += vs3[l-1];
			    ans = Math.Max(ans, value);
			}
		    }
		}
	    }
	}
	    
	WriteLine(ans);
    }
}