import scala.collection.mutable

object Main extends App {
  val Array(n, weight) = io.StdIn.readLine().split(' ').map(_.toInt)
  val items = Array.fill(n)(io.StdIn.readLine().split(' ').map(_.toInt))
  val dp: Array[mutable.HashMap[Long, Long]] = Array.fill(n+1)(mutable.HashMap.empty)
  dp(0)(0) = 0

  for (i <- 0 until n)
    for ((w, v) <- dp(i)) {
      if (w+items(i)(0) <= weight) {
        val current = dp(i+1).getOrElse[Long](w+items(i)(0), -1)
        val tmp = v + items(i)(1)
        if (current < tmp) dp(i+1)(w+items(i)(0)) = tmp
      }
      if (dp(i+1).getOrElse[Long](w, -1) < v) dp(i+1)(w) = v
    }
  println(dp(n).values.max)
}