fun main(args: Array<String>) {
    val (n,w) = readLine()!!.split(" ").map { it.toInt() }
    val wv = (1..n).map { readLine()!!.split(" ").map { it.toInt() } }
    val w1 = wv[0][0]+0L

    val items = Array(4){ i ->
        intArrayOf(0, *(wv.filter { it[0]+0L==w1+i }.map { it[1] }.sortedDescending().toIntArray()) )}
    items.forEach { for(i in 1..it.lastIndex) it[i]+=it[i-1] }

    var max = 0
    for(i0 in 0..items[0].lastIndex){
        for(i1 in 0..items[1].lastIndex) {
            for (i2 in 0..items[2].lastIndex) {
                for (i3 in 0..items[3].lastIndex) {
                    if(w1*i0+(w1+1)*i1+(w1+2)*i2+(w1+3)*i3<=w) {
                        max = Math.max(max, items[0][i0] + items[1][i1] + items[2][i2] + items[3][i3])
                    }
                }
            }
        }
    }
    println(max)
}