N,W = gets.split.map(&:to_i)
value = Array.new(N)
weight = Array.new(N)

N.times {|i| value[i], weight[i] = gets.split.map(&:to_i) } 

dp = Array.new(N+1) {Array.new(W+1){0}}

(0...N).each do |i|
  (0..W).each do |w|
    if w >= weight[i]
      dp[i+1][w] = [dp[i+1][w-weight[i]] + value[i], dp[i][w]].max
    else
      # w よりも weight[i] が大きいとき、品物iが選ばれていることはありえない
      dp[i+1][w] = dp[i][w]
    end
  end
end

puts dp[N][W]


