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.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Program
{
    #region Standard I/O
    readonly TextReader input;
    readonly TextWriter output;

    X[] ReadLine<X>(Func<string, X> func)
    {
        return input.ReadLine().Split(' ').Select(func).ToArray();
    }

    void WriteLineOne(object obj)
    {
        output.WriteLine("{0}", obj);
    }

    void WriteLineMany(params object[] objects)
    {
        output.WriteLine(string.Join(" ", objects.Select(obj => obj.ToString())));
    }
    #endregion

    int n;
    long weight;
    long[] ws;
    long[] vs;

    /// <summary>
    /// 重さの合計が weight を超えない範囲で
    /// 与えられた n 個のものを最大 r 個まで選び、
    /// 価値の合計を最大化するときの、最大値を計算する。
    /// </summary>
    public static long Knapsack(int n, int r, int weight, int[] ws, long[] vs)
    {
        // dp[i, j, w] = v ⇔
        // 前から i 個のものを、最大 j 個まで選ぶ、
        // 合計が w 以下になる選び方の、最大の価値が v である。
        var dp = new long[n + 1, r + 1, weight + 1];

        for (var i = 0; i <= n; i++)
        {
            for (var j = 0; j <= r; j++)
            {
                //for (var w = weight; w >= 0; w--)
                for (var w = 0; w <= weight; w++)
                {
                    if (i < n && j < r)
                    {
                        if (w + ws[i] <= weight)
                        {
                            dp[i + 1, j + 1, w + ws[i]] =
                                Math.Max(dp[i + 1, j + 1, w + ws[i]], dp[i, j, w] + vs[i]);
                        }

                        dp[i + 1, j + 1, w] = Math.Max(dp[i + 1, j + 1, w], dp[i, j, w]);
                    }

                    if (i < n)
                    {
                        dp[i + 1, j, w] = Math.Max(dp[i + 1, j, w], dp[i, j, w]);
                    }

                    if (j < r)
                    {
                        dp[i, j + 1, w] = Math.Max(dp[i, j + 1, w], dp[i, j, w]);
                    }
                }
            }
        }

        return Enumerable.Range(0, weight + 1).Select(w => dp[n, r, w]).Max();
    }

    public void Run()
    {
        {
            var firstLine = ReadLine(long.Parse);
            n = (int)firstLine[0];
            weight = firstLine[1];

            ws = new long[n];
            vs = new long[n];
            for (var i = 0; i < n; i++)
            {
                var line = ReadLine(long.Parse);

                ws[i] = line[0];
                vs[i] = line[1];
            }
        }

        var maxValue = long.MinValue;
        var us = ws.Select(w => (int)(w - ws[0])).ToArray();
        var totalRestWeights = us.Sum();
        var sortedValues = vs.OrderByDescending(v => v).ToArray();

        for (var r = 0; r <= n; r++)
        {
            var rest = weight - r * ws[0];
            if (rest < 0) continue;
            if (rest >= totalRestWeights)
            {
                maxValue = Math.Max(maxValue, new ArraySegment<long>(sortedValues, 0, r).Sum());
                continue;
            }

            // rest < total <= 3 * (n - 1)

            maxValue = Math.Max(maxValue, Knapsack(n, r, (int)rest, us, vs));
        }

        WriteLineOne(maxValue);
    }

    public Program(TextReader input, TextWriter output)
    {
        this.input = input;
        this.output = output;
    }

    public static void Main(string[] args)
    {
        new Program(Console.In, Console.Out).Run();
    }
}