import java.lang.Math

fun main(args: Array<String>) {
    val (N, W) = readInputLine().split(" ").map { it.toInt() }

    val dp = Array(2) { sortedMapOf(Pair(0, 0)) }

    for (i in 0 until N) {
        val (w, v) = readInputLine().split(" ").map { it.toInt() }

        for (k in dp[i % 2].keys) {
            dp[(i + 1) % 2][k] = Math.max(dp[i % 2][k]!!, dp[(i + 1) % 2][k] ?: 0)
            if (k + w <= W) {
                dp[(i + 1) % 2][k + w] = Math.max(dp[i % 2][k]!! + v, dp[(i + 1) % 2][k + w] ?: 0)
            }
        }
    }

    var ans = 0

    for ((k, v) in dp[N % 2]) {
        ans = Math.max(ans, v)
    }

    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}
