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;

namespace ABC060D
{
    class Program
    {
        // After reading editorial
        static void Main(string[] args)
        {
            string[] bufs = Console.ReadLine().Split(' ');
            int N = Int32.Parse(bufs[0]);
            int W = Int32.Parse(bufs[1]);

            int w0, w1, w2, w3;
            int[] v0 = new int[N];
            int[] v1 = new int[N];
            int[] v2 = new int[N];
            int[] v3 = new int[N];

            int n0 = 1;
            int n1 = 0;
            int n2 = 0;
            int n3 = 0;

            bufs = Console.ReadLine().Split(' ');
            w0 = Int32.Parse(bufs[0]);
            w1 = w0 + 1;
            w2 = w0 + 2;
            w3 = w0 + 3;
            v0[0] = Int32.Parse(bufs[1]);

            for (int i = 1; i < N; i++)
            {
                bufs = Console.ReadLine().Split(' ');
                int w = Int32.Parse(bufs[0]);
                int v = Int32.Parse(bufs[1]);

                if (w == w0)
                {
                    DescInsert(ref v0, v, n0++);
                }
                else if (w == w1)
                {
                    DescInsert(ref v1, v, n1++);
                }
                else if (w == w2)
                {
                    DescInsert(ref v2, v, n2++);
                }
                else if (w == w3)
                {
                    DescInsert(ref v3, v, n3++);
                }
            }

            int pick0 = Math.Min(W / w0, n0);
            int pick1, pick2, pick3;

            int res = 0;

            while (pick0 >= 0)
            {
                int remain = W - w0 * pick0;

                pick1 = Math.Min(remain / w1, n1);
                while (pick1 >= 0)
                {
                    remain = W - w0 * pick0 - w1 * pick1;
                    pick2 = Math.Min(remain / w2, n2);
                    while (pick2 >= 0)
                    {
                        remain = W - w0 * pick0 - w1 * pick1 - w2 * pick2;
                        pick3 = Math.Min(remain / w3, n3);

                        int sum = 0;
                        for (int i = 0; i < pick0; i++)
                        {
                            sum += v0[i];
                        }
                        for (int i = 0; i < pick1; i++)
                        {
                            sum += v1[i];
                        }
                        for (int i = 0; i < pick2; i++)
                        {
                            sum += v2[i];
                        }
                        for (int i = 0; i < pick3; i++)
                        {
                            sum += v3[i];
                        }

                        if (res < sum) { res = sum; }

                        pick2--;
                    }

                    pick1--;
                }

                pick0--;
            }

            Console.WriteLine(res);
        }

        static void DescInsert(ref int[] arr, int src, int len)
        {
            for (int i = len - 1; i >= 0; i--)
            {
                if (arr[i] > src)
                {
                    arr[i + 1] = src;
                    return;
                }
                arr[i + 1] = arr[i];
            }

            arr[0] = src;
        }
    }
}