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 Ruby (2.3.3) (judge verdict: Accepted)

num, weight = gets.split.map(&:to_i)
things = []
num.times { things << gets.split.map(&:to_i) }
w_range = (things.first.first..things.first.first + 3).to_a
things_1 = []
things_2 = []
things_3 = []
things_4 = []
things.each do |w, v|
  case w_range.find_index(w)
  when 0
    things_1 << v
  when 1
    things_2 << v
  when 2
    things_3 << v
  when 3
    things_4 << v
  end
end
things_1.sort! { |a, b| b <=> a }
things_2.sort! { |a, b| b <=> a }
things_3.sort! { |a, b| b <=> a }
things_4.sort! { |a, b| b <=> a }
cumulative_things_1 = [0]
cumulative_things_2 = [0]
cumulative_things_3 = [0]
cumulative_things_4 = [0]
things_1.each { |v| cumulative_things_1 << cumulative_things_1.last + v }
things_2.each { |v| cumulative_things_2 << cumulative_things_2.last + v }
things_3.each { |v| cumulative_things_3 << cumulative_things_3.last + v }
things_4.each { |v| cumulative_things_4 << cumulative_things_4.last + v }
ans = []
cumulative_things_1.each_with_index do |v1, i1|
  cumulative_things_2.each_with_index do |v2, i2|
    cumulative_things_3.each_with_index do |v3, i3|
      cumulative_things_4.each_with_index do |v4, i4|
        ans << v1 + v2 + v3 + v4 if i1 * w_range[0] + i2 * w_range[1] + i3 * w_range[2] + i4 * w_range[3] <= weight
      end
    end
  end
end
puts ans.max