#!/usr/bin/env ruby
# opt(Items, w) = max{ V(w_max) + opt(Items-w_max, w-W(w_max)), opt(Items-w_max, w) }
# w_max = max{W(Items)}

n, w = STDIN.readline.strip.split(' ').map(&:to_i)
wv = STDIN.read.split("\n").map{|l| l.split(' ').map(&:to_i)}

def opt(w, wv, m)
  # puts "opt(#{w}, #{wv}, #{m}"
  return m[w] if m[w]
  wv = delete_and_sort_wv(w, wv)
  # puts "wv:#{wv}"
  return 0 if wv == []
  with = wv[-1][1] + opt(w-wv[-1][0], wv[0..-2], m)
  without = opt(w, wv[0..-2], m)
  # puts "with:#{with}, without:#{without}, m[w]:#{[with, without].max}"
  m[w] = [with, without].max
end

def delete_and_sort_wv(w, wv)
  wv.find_all{|wvi| w >= wvi[0]}.sort_by{|wvi| wvi[1]}
end

m = []
puts opt(w, wv, m)
