import java.math.*;
import java.util.*;
object MainKt {
   fun solve(i:Int,j:Int,w:List<Int>,v:List<Int>,mem: HashMap<Pair<Pair<Int,Int>,Pair<List<Int>,List<Int>>>,Int>):Int=mem.getOrPut(Pair(Pair(i,j),Pair(w,v))){
    if(i==0) {
      0
    }
    else {
    Math.max(solve(i-1,j,w,v,mem),if(w[i-1] <=j) {solve(i-1,j-w[i-1],w,v,mem)+v[i-1]} else {0}) 
    }
   } 
   
    
  @JvmStatic fun main(args:Array<String>) {
    val (N,W)=readLine()!!.split(" ").map(String::toInt)
    val (weight,value)=(1..N).map {
      val (w,v)=readLine()!!.split(" ").map(String::toInt)
      Pair(w,v)
    }.unzip()
    val memo=hashMapOf<Pair<Pair<Int,Int>,Pair<List<Int>,List<Int>>>,Int>()
    println(solve(N,W,weight,value,memo))
  }
}
