def max(a, b)
  a > b ? a : b
end

N, W = gets.split.map(&:to_i)
WV = N.times.map{gets.split.map(&:to_i)}.sort_by{|a|[a[0], a[1]]}
#N = 100
#W = 10 ** 5
#WV = N.times.map{[rand(1..W-1), rand(1..10**9)]}.sort_by{|a|[a[0], a[1]]}

dp = Array.new(W + 1, 0)
dp[0] = 0
N.times do |i|
  w, v = WV[i]
  W.downto(w) do |nw|
    nv = dp[nw - w] + v
    dp[nw] = max(dp[nw], nv)
  end
end

p dp.max