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# (.NET Core 3.1.201) (judge verdict: Accepted)

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

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            //Out.Flush();
            Read();
        }

        static void Solve()
        {
            (long N, long W) = Slice2<long>();
            var cdt = Enumerable.Repeat(0, 4).Select(_ => new List<long>()).ToArray();

            var WV0 = Slice2<long>();
            cdt[0].Add(WV0.b);
            for (var i = 1; i < N; ++i)
            {
                (long w, long v) = Slice2<long>();
                cdt[w - WV0.a].Add(v);
            }
            for (var i = 0; i < 4; ++i)
            {
                cdt[i].Sort();
                cdt[i].Reverse();
                cdt[i].Insert(0, 0);
            }
            var ans = 0L;
            for (var c1 = 0; c1 < cdt[0].Count; ++c1)
                for (var c2 = 0; c2 < cdt[1].Count; ++c2)
                    for (var c3 = 0; c3 < cdt[2].Count; ++c3)
                        for (var c4 = 0; c4 < cdt[3].Count; ++c4)
                        { 
                            var b = WV0.a;
                            var w = b * c1 + (b + 1) * c2 + (b + 2) * c3 + (b + 3) * c4;
                            if (w <= W)
                            {
                                ans = Max(ans,
                                    cdt[0].Take(c1 + 1).Sum() +
                                    cdt[1].Take(c2 + 1).Sum() +
                                    cdt[2].Take(c3 + 1).Sum() +
                                    cdt[3].Take(c4 + 1).Sum());
                            }
                        }
            WriteLine(ans);
        }

        static long Mod = (long)1e9 + 7;
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static public (T1 a, T2 b) Slice<T1, T2>()
        {
            var t = ReadLine().Split();
            return (
            (T1)Convert.ChangeType(t[0], typeof(T1)),
            (T2)Convert.ChangeType(t[1], typeof(T2)));
        }
        static public (T a, T b) Slice2<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)));
        }
        static public (T a, T b, T c) Slice3<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)),
            (T)Convert.ChangeType(t[2], typeof(T)));
        }
    }
}