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: Wrong Answer)

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace AtCorder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Solve(new ConsoleInput(Console.In, ' '));
        }

        public void Solve(ConsoleInput cin)
        {
            var N = cin.ReadInt;
            var W = cin.ReadInt;
            var dic = new Dictionary<int, int>();
            var w0 = new List<int>();
            var w1 = new List<int>();
            var w2 = new List<int>();
            var w3 = new List<int>();
            var iw = cin.ReadInt;
            var iv = cin.ReadInt;
            w0.Add(iv);
            for(int i = 1; i < N; i++)
            {
                var w = cin.ReadInt;
                var v = cin.ReadInt;
                var num = w - iw;
                switch (num)
                {
                    case 0:
                        w0.Add(v);
                        break;
                    case 1:
                        w1.Add(v);
                        break;
                    case 2:
                        w2.Add(v);
                        break;
                    case 3:
                        w3.Add(v);
                        break;
                }
            }
            w0.Sort();
            w0.Reverse();
            w1.Sort();
            w1.Reverse();
            w2.Sort();
            w2.Reverse();
            w3.Sort();
            w3.Reverse();
            var ans = 0L;
            var len0 = w0.Count;
            var len1 = w1.Count;
            var len2 = w2.Count;
            var len3 = w3.Count;
            for(int i = 0; i <= len0; i++)
            {
                for(int j = 0; j <= len1; j++)
                {
                    for(int t = 0; t <= len2; t++)
                    {
                        for(int u = 0; u <= len3; u++)
                        {
                            if(iw * i + (iw + 1) * j + (iw + 2) * t + (iw + 3) * u > W)
                            {
                                break;
                            }
                            long tmpAns = w0.Take(i).Sum() + w1.Take(j).Sum() + w2.Take(t).Sum() + w3.Take(u).Sum();
                            if (tmpAns > ans)
                            {
                                ans = tmpAns;
                            }
                        }
                    }
                }
            }
            WriteLine(ans);
        }



        public long C(int X, int Y)
        {

            if (Y == 0 || Y == X)
            {
                return 1;
            }
            if (X < Y)
            {
                return 0;
            }
            var Pascal = new long[X + 1, X + 1];
            for (int i = 0; i <= X; i++)
            {
                Pascal[i, 0] = 1L;
                Pascal[i, i] = 1L;
            }
            for (int i = 2; i <= X; i++)
            {
                for (int j = 1; j < i; j++)
                {
                    Pascal[i, j] = Pascal[i - 1, j] + Pascal[i - 1, j - 1];
                }
            }
            return Pascal[X, Y];
        }

        public class ConsoleInput
        {
            private readonly System.IO.TextReader _stream;
            private char _separator = ' ';
            private Queue<string> inputStream;
            public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
            {
                this._separator = separator;
                this._stream = stream;
                inputStream = new Queue<string>();
            }
            public string Read
            {
                get
                {
                    if (inputStream.Count != 0) return inputStream.Dequeue();
                    string[] tmp = _stream.ReadLine().Split(_separator);
                    for (int i = 0; i < tmp.Length; ++i)
                        inputStream.Enqueue(tmp[i]);
                    return inputStream.Dequeue();
                }
            }
            public string ReadLine { get { return _stream.ReadLine(); } }
            public int ReadInt { get { return int.Parse(Read); } }
            public long ReadLong { get { return long.Parse(Read); } }
            public double ReadDouble { get { return double.Parse(Read); } }
            public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
            public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
            public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
        }
    }
}