N, W = gets.split.map(&:to_i)

h = Hash.new(0)
h[0] = 0
N.times do 
    f = h.clone
    w, v = gets.split.map(&:to_i)

    h.each do |key, sum|
        if key + w <= W && f[key + w] < sum + v
            f[key + w] = sum + v
        end
    end

    h = f
end

puts h.values.max