n, w = gets.split.map(&:to_i)
wv = []
n.times do
  wv << gets.split.map(&:to_i)
end

ans = 0
dp = (n + 1).times.map{Hash.new(0)}
dp[0] = { 0 => 0 }
n.times do |i|
  dp[i].each do |k, v|
    if k + wv[i][0] <= w
      dp[i + 1][k + wv[i][0]] = v + wv[i][1]
      ans = v + wv[i][1] if ans < v + wv[i][1]
    end
    dp[i + 1][k] = v
  end
end

puts ans
