#coding:utf-8

import bisect

import sys

sys.setrecursionlimit(10**6)

write = sys.stdout.write

dbg = lambda *something : print(*something) if DEBUG else 0

DEBUG = True







def main(given = sys.stdin.readline):

    input = lambda : given().rstrip()

    LMIIS = lambda : list(map(int,input().split()))

    II = lambda : int(input())

    XLMIIS = lambda x : [LMIIS() for _ in range(x)]



    n, t = LMIIS()

    ab = XLMIIS(n)



    dp1 = [[0]*t for _ in range(n)]

    for i in range(1, n):

        for j in range(1, t):

            if j < ab[i-1][0]:

                dp1[i][j] = dp1[i-1][j]

            else:

                dp1[i][j] = max(dp1[i-1][j], dp1[i-1][j-ab[i-1][0]] + ab[i-1][1])





    dp2 = [[0]*t for _ in range(n)]

    for i in range(1, n):

        for j in range(1, t):

            if j < ab[n - i][0]:

                dp2[i][j] = dp2[i-1][j]

            else:

                dp2[i][j] = max(dp2[i-1][j], dp2[i-1][j-ab[n-i][0]] + ab[n-i][1])



    ans = 0

    for i in range(n):

        for j in range(t - 1):

            ans = max(ans, dp1[i][j] + dp2[n - i - 1][t - j - 1] + ab[i][1])

    print(ans)



if __name__ == '__main__':

    main()

#coding:utf-8

import bisect

import sys

sys.setrecursionlimit(10**6)

write = sys.stdout.write

dbg = lambda *something : print(*something) if DEBUG else 0

DEBUG = True







def main(given = sys.stdin.readline):

    input = lambda : given().rstrip()

    LMIIS = lambda : list(map(int,input().split()))

    II = lambda : int(input())

    XLMIIS = lambda x : [LMIIS() for _ in range(x)]



    n, t = LMIIS()

    ab = XLMIIS(n)

    ab.sort()



    dp1 = [[0]*t for _ in range(n+1)]

    for i in range(1, n+1):

        for j in range(1, t):

            if j < ab[i-1][0]:

                dp1[i][j] = dp1[i-1][j]

            else:

                dp1[i][j] = max(dp1[i-1][j], dp1[i-1][j-ab[i-1][0]] + ab[i-1][1])



    ans = 0

    for i in range(n):

        ans = max(ans, dp1[i][t-1] + ab[i][1])

    print(ans)



if __name__ == '__main__':

    main()

# coding:utf-8
import bisect
import sys

sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something: print(*something) if DEBUG else 0
DEBUG = True


def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int, input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    n, t = LMIIS()
    ab = XLMIIS(n)
    dp1 = [[0] * t for _ in range(n)]
    for i in range(1, n):
        for j in range(1, t):
            if j < ab[i - 1][0]:
                dp1[i][j] = dp1[i - 1][j]
            else:
                dp1[i][j] = max(
                    dp1[i - 1][j], dp1[i - 1][j - ab[i - 1][0]] + ab[i - 1][1]
                )
    dp2 = [[0] * t for _ in range(n)]
    for i in range(1, n):
        for j in range(1, t):
            if j < ab[n - i][0]:
                dp2[i][j] = dp2[i - 1][j]
            else:
                dp2[i][j] = max(
                    dp2[i - 1][j], dp2[i - 1][j - ab[n - i][0]] + ab[n - i][1]
                )
    ans = 0
    for i in range(n):
        for j in range(t - 1):
            ans = max(ans, dp1[i][j] + dp2[n - i - 1][t - j - 1] + ab[i][1])
    print(ans)


if __name__ == "__main__":
    main()

# coding:utf-8
import bisect
import sys

sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something: print(*something) if DEBUG else 0
DEBUG = True


def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int, input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    n, t = LMIIS()
    ab = XLMIIS(n)
    ab.sort()
    dp1 = [[0] * t for _ in range(n + 1)]
    for i in range(1, n + 1):
        for j in range(1, t):
            if j < ab[i - 1][0]:
                dp1[i][j] = dp1[i - 1][j]
            else:
                dp1[i][j] = max(
                    dp1[i - 1][j], dp1[i - 1][j - ab[i - 1][0]] + ab[i - 1][1]
                )
    ans = 0
    for i in range(n):
        ans = max(ans, dp1[i][t - 1] + ab[i][1])
    print(ans)


if __name__ == "__main__":
    main()

-    dp1 = [[0] * t for _ in range(n)] -    for i in range(1, n): +    ab.sort() +    dp1 = [[0] * t for _ in range(n + 1)] +    for i in range(1, n + 1): -    dp2 = [[0] * t for _ in range(n)] -    for i in range(1, n): -        for j in range(1, t): -            if j < ab[n - i][0]: -                dp2[i][j] = dp2[i - 1][j] -            else: -                dp2[i][j] = max( -                    dp2[i - 1][j], dp2[i - 1][j - ab[n - i][0]] + ab[n - i][1] -                ) -        for j in range(t - 1): -            ans = max(ans, dp1[i][j] + dp2[n - i - 1][t - j - 1] + ab[i][1]) +        ans = max(ans, dp1[i][t - 1] + ab[i][1])