WV = $<.read.split.map(&:to_i).each_slice(2).to_a
N,W = WV.shift
WV.sort_by! {|w,v| [w,-v]}
I = {}
WV.each.with_index { |(w,*),i| break if w > W; I[w] ||= i}
A = Array.new W+1
A[0] = [0,0]
1.upto(W) {|n|
  m = A[n-1]
  I.each {|w,i|
    break  if n < w
    a,b = A[n-w]
    i += 1  while b[i] > 0
    next  if w < WV[i][0]
    b += 1 << i
    a += WV[i][1]
    m = [a,b]  if a > m[0]
  }
  A[n] = m
}
p A[W][0]