fun main(args: Array<String>) {
    val (n, totalTime) = readLine()!!.split(' ').map(String::toInt)
    val abArray = Array(n + 1) { Pair(0, 0) }
    (1..n).forEach {
        val (x, y) = readLine()!!.split(' ').map(String::toInt)
        abArray[it] = Pair(x, y)
    }

    val dp = Array(n + 1) { LongArray(totalTime + 1) { 0L } }
    val prev = Array(n + 1) { IntArray(totalTime + 1) { 0 } }
    for (i in 1..n) {
        val (time, value) = abArray[i]
        for (cur in 0 until totalTime) {
            if (cur >= time) {
                if (dp[i][cur] < dp[i - 1][cur - time] + value) {
                    dp[i][cur] = dp[i - 1][cur - time] + value
                    prev[i][cur] = cur - time
                }
            }
            if (dp[i][cur] < dp[i - 1][cur]) {
                dp[i][cur] = dp[i - 1][cur]
                prev[i][cur] = cur
            }
        }
    }

    var curW = totalTime - 1
    for (i in n downTo 1) {
        val t1 = prev[i][curW]
        val t2 = curW - abArray[i].first
        if (t1 == t2) {
            abArray[i] = Pair(0, 0)
        }
        curW = prev[i][curW]
    }
    val max = abArray.maxBy { it.second }?.second ?: 0

    println(dp[n][totalTime - 1] + max)
}