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;
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();

        var dp = new long[A.Count + 1, B.Count + 1, C.Count + 1, D.Count + 1, 2];
        for (int i = 0; i <= A.Count; i++)
        {
            if (i == 0) continue;
            if (dp[i - 1, 0, 0, 0, 1] + w1 <= W)
            {
                dp[i, 0, 0, 0, 0] = dp[i - 1, 0, 0, 0, 0] + A[i - 1];
                dp[i, 0, 0, 0, 1] = dp[i - 1, 0, 0, 0, 1] + w1;
            }
            else dp[i, 0, 0, 0, 1] = W + 1;
        }
        for (int i = 0; i <= A.Count; i++)
        {
            for (int j = 0; j <= B.Count; j++)
            {
                if (j == 0) continue;
                if (dp[i, j - 1, 0, 0, 1] + w1 + 1 <= W)
                {
                    dp[i, j, 0, 0, 0] = dp[i, j - 1, 0, 0, 0] + B[j - 1];
                    dp[i, j, 0, 0, 1] = dp[i, j - 1, 0, 0, 1] + w1 + 1;
                }
                else dp[i, j, 0, 0, 1] = W + 1;
            }
        }
        for (int i = 0; i <= A.Count; i++)
        {
            for (int j = 0; j <= B.Count; j++)
            {
                for (int k = 0; k <= C.Count; k++)
                {
                    if (k == 0) continue;
                    if (dp[i, j, k - 1, 0, 1] + w1 + 2 <= W)
                    {
                        dp[i, j, k, 0, 0] = dp[i, j, k - 1, 0, 0] + C[k - 1];
                        dp[i, j, k, 0, 1] = dp[i, j, k - 1, 0, 1] + w1 + 2;
                    }
                    else dp[i, j, k, 0, 1] = W + 1;
                }
            }
        }
        long ans = 0;
        for (int i = 0; i <= A.Count; i++)
        {
            for (int j = 0; j <= B.Count; j++)
            {
                for (int k = 0; k <= C.Count; k++)
                {
                    for (int l = 0; l <= D.Count; l++)
                    {
                        if (l == 0)
                        {
                            ans = Math.Max(dp[i, j, k, l, 0], ans);
                            continue;
                        }
                        if (dp[i, j, k, l - 1, 1] + w1 + 3 <= W)
                        {
                            dp[i, j, k, l, 0] = dp[i, j, k, l - 1, 0] + D[l - 1];
                            dp[i, j, k, l, 1] = dp[i, j, k, l - 1, 1] + w1 + 3;
                        }
                        else dp[i, j, k, l, 1] = W + 1;
                        ans = Math.Max(dp[i, j, k, l, 0], ans);
                    }
                }
            }
        }
        WriteLine(ans);

    }
}

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); } }
}