fun main(args: Array<String>) = yorukatsu8e()

fun yorukatsu8e() {
    val (n, t) = readLine()!!.split(' ').map { it.toInt() }
    val foods = (1..n).map {
        readLine()!!.split(' ').map { it.toInt() }.let { Food(it[0], it[1]) }
    }.sortedBy { it.time }

    val dp = Array(n + 1) { IntArray(t) }
    var answer = 0

    for (i in 1..n) {
        val food = foods[i - 1]
        for (time in 0 until t) {
            dp[i][time] = dp[i - 1][time]
            answer = Math.max(answer, dp[i][time] + food.point)
            if (time - food.time >= 0) {
                dp[i][time] = Math.max(
                    dp[i][time],
                    dp[i - 1][time - food.time] + food.point
                )
            }
        }
    }

    println(answer)
}

private data class Food(val time: Int, val point: Int)
