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;

namespace ConsoleApp23
{
    class Program
    {

        class item
        {
            /// <summary>
            /// アイテムの重さ
            /// </summary>
            public long weight;
            /// <summary>
            /// アイテムの価値
            /// </summary>
            public int value;
            public item()
            {

                this.weight = 0;
                this.value = 0;
            }
        }
        private static int max(int i, int j)
        {
            if (i < j) return j;
            return i;
        }
        private static long longMin(long i, long j)
        {
            if (i > j) return j;
            return i;
        }
        static void Main(string[] args)
        {
            //入力部
            int N = 0;                        //与えられる荷物の数
            int W = 0;                        //最大容量
            int ans = 0;                      //回答
            long minWaight = 0;
            //int K = 0;
            string[] cin = Console.ReadLine().Split(' ');
            N = int.Parse(cin[0]);
            W = int.Parse(cin[1]);
            List<item> A = new List<item>();
            for (int i = 0; i < N; i++)
            {
                item item = new item();
                cin = Console.ReadLine().Split(' ');

                A.Add(item);
                item.weight = long.Parse(cin[0]);
                item.value = int.Parse(cin[1]);
                if (i == 0)
                {
                    minWaight = item.weight;
                }
                minWaight = longMin(minWaight, item.weight);
            }

            //計算部
            //4種類の重さに分割
            List<item>[] items = new List<item>[4];
            int[][] sumValueSpretted = new int[4][];
            for (int i = 0; i < 4; i++)
            {
                items[i] = A.Where(a => a.weight == minWaight + (long)i).ToList();
                //value降順にソート
                items[i].Sort((a, b) => b.value - a.value);

                //j番目までの価値を計算
                sumValueSpretted[i] = new int[N + 1];
                sumValueSpretted[i][0] = 0;
                for (int j = 0; j < items[i].Count; j++)
                {
                    sumValueSpretted[i][j + 1] = sumValueSpretted[i][j] + items[i][j].value;
                }
            }

            //全探索
            for (int index0 = 0; index0 <= items[0].Count; index0++)
            {
                for (int index1 = 0; index1 <= items[1].Count; index1++)
                {
                    for (int index2 = 0; index2 <= items[2].Count; index2++)
                    {
                        for (int index3 = 0; index3 <= items[3].Count; index3++)
                        {
                            if (index0 * minWaight + index1 * (minWaight + 1) + index2 * (minWaight + 2) + index3 * (minWaight + 3) <= W)
                            {
                                ans = max(ans, sumValueSpretted[0][index0] + sumValueSpretted[1][index1] + sumValueSpretted[2][index2] + sumValueSpretted[3][index3]);
                            }
                        }
                    }
                }
            }

            //出力部
            //cout << ans << "\n";
            Console.WriteLine(ans);
            return;
        }
    }
}