n, _w = gets.split.map(&:to_i)
 
dp = Hash.new(0)
dp[0] = 0
n.times do |i|
  prev = dp.dup
  w, v = gets.split.map(&:to_i)
  prev.each do |key, value|
    nw = key + w
    nv = value + v
    if nw <= _w && nv > dp[nw]
      dp[nw] = nv
    end
  end
end

ans = 0
dp.each {|_, v| ans = v > ans ? v : ans }
puts ans

