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.Collections.Generic;
using System.Linq;
using System.IO;
using System.Globalization;
using System.Diagnostics;
using static System.Console;
using Pair = System.Collections.Generic.KeyValuePair<int, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().Solve();
        Out.Flush();
    }
    Scanner cin = new Scanner();
    Random rnd = new Random();
    Stopwatch sw = new Stopwatch();
    readonly int[] dd = { 0, 1, 0, -1, 0 };
    readonly int mod = 1000000007;
    readonly string alfa = "abcdefghijklmnopqrstuvwxyz";

    int N, W;
    long we = 0;
    long ans = 0;
    void Solve()
    {
        N = cin.Nextint;
        W = cin.Nextint;
        var w = new int[N];
        var v = new int[N];
        for (int i = 0; i < N; i++)
        {
            w[i] = cin.Nextint;
            v[i] = cin.Nextint;
        }
        int w1 = w[0];
        var A = new List<int>();
        var B = new List<int>();
        var C = new List<int>();
        var D = new List<int>();
        for (int i = 0; i < N; i++)
        {
            if (w[i] == w1) A.Add(v[i]);
            else if (w[i] == w1 + 1) B.Add(v[i]);
            else if (w[i] == w1 + 2) C.Add(v[i]);
            else if (w[i] == w1 + 3) D.Add(v[i]);
        }
        A = A.OrderByDescending(i => i).ToList();
        B = B.OrderByDescending(i => i).ToList();
        C = C.OrderByDescending(i => i).ToList();
        D = D.OrderByDescending(i => i).ToList();

        long a = 0, b = 0, c = 0, d = 0;
        while (true)
        {
            a = Counter(A, w1);
            b = Counter(B, w1 + 1);
            c = Counter(C, w1 + 2);
            d = Counter(D, w1 + 3);
            if (a == 0 && b == 0 && c == 0 && d == 0) break;
            if (a > b && a > c && a > d)
            {
                ans += A[0];
                A.RemoveAt(0);
                we += w1;
            }
            else if(b > a && b > c && b > d)
            {
                ans += B[0];
                B.RemoveAt(0);
                we += w1 + 1;
            }
            else if (c > a && c > b && c > d)
            {
                ans += C[0];
                C.RemoveAt(0);
                we += w1 + 2;
            }
            else
            {
                ans += D[0];
                D.RemoveAt(0);
                we += w1 + 3;
            }

        }
        WriteLine(ans);
    }
    long Counter(List<int> L, int weight)
    {
        int cnt = 0;
        long box = (W - we) / weight;
        if (box <= 0) return 0;
        long a = 0;
        for (int j = 0; j < L.Count; j++)
        {
            if (cnt == box) break;
            a += L[j];
            cnt++;
        }
        return a;
    }

}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] Scan { get { return ReadLine().Split(); } }
    public int[] Scanint { get { return Array.ConvertAll(Scan, int.Parse); } }
    public long[] Scanlong { get { return Array.ConvertAll(Scan, long.Parse); } }
    public double[] Scandouble { get { return Array.ConvertAll(Scan, double.Parse); } }
    public string Next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return Next;
        }
    }
    public int Nextint { get { return int.Parse(Next); } }
    public long Nextlong { get { return long.Parse(Next); } }
    public double Nextdouble { get { return double.Parse(Next); } }
}