import sys



read = sys.stdin.buffer.read





def main():

    N, T, *AB = list(map(int, read().split()))

    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]



    D.sort()



    dp = [[0] * T for _ in range(N + 1)]

    for i, (a, b) in enumerate(D):

        for t in range(T):

            if 0 <= t - a:

                dp[i + 1][t] = dp[i][t - a] + b

            if dp[i + 1][t] < dp[i][t]:

                dp[i + 1][t] = dp[i][t]



    ans = 0

    for i in range(N - 1):

        if ans < dp[i + 1][T - 1] + D[i + 1][1]:

            ans = dp[i + 1][T - 1] + D[i + 1][1]



    print(ans)

    return





if __name__ == '__main__':

    main()

import sys



read = sys.stdin.buffer.read





def main():

    N, T, *AB = list(map(int, read().split()))

    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]



    D.sort()



    dp = [0] * T

    max_sum = [0] * N

    for i, (a, b) in enumerate(D):

        for t in range(T - 1, -1, -1):

            if t >= a and dp[t] < dp[t - a] + b:

                dp[t] = dp[t - a] + b

        max_sum[i] = dp[T - 1]



    ans = 0

    for i in range(N - 1):

        if ans < max_sum[i] + D[i + 1][1]:

            ans = max_sum[i] + D[i + 1][1]



    print(ans)

    return





if __name__ == '__main__':

    main()

import sys

read = sys.stdin.buffer.read


def main():
    N, T, *AB = list(map(int, read().split()))
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]
    D.sort()
    dp = [[0] * T for _ in range(N + 1)]
    for i, (a, b) in enumerate(D):
        for t in range(T):
            if 0 <= t - a:
                dp[i + 1][t] = dp[i][t - a] + b
            if dp[i + 1][t] < dp[i][t]:
                dp[i + 1][t] = dp[i][t]
    ans = 0
    for i in range(N - 1):
        if ans < dp[i + 1][T - 1] + D[i + 1][1]:
            ans = dp[i + 1][T - 1] + D[i + 1][1]
    print(ans)
    return


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.buffer.read


def main():
    N, T, *AB = list(map(int, read().split()))
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]
    D.sort()
    dp = [0] * T
    max_sum = [0] * N
    for i, (a, b) in enumerate(D):
        for t in range(T - 1, -1, -1):
            if t >= a and dp[t] < dp[t - a] + b:
                dp[t] = dp[t - a] + b
        max_sum[i] = dp[T - 1]
    ans = 0
    for i in range(N - 1):
        if ans < max_sum[i] + D[i + 1][1]:
            ans = max_sum[i] + D[i + 1][1]
    print(ans)
    return


if __name__ == "__main__":
    main()

-    dp = [[0] * T for _ in range(N + 1)] +    dp = [0] * T +    max_sum = [0] * N -        for t in range(T): -            if 0 <= t - a: -                dp[i + 1][t] = dp[i][t - a] + b -            if dp[i + 1][t] < dp[i][t]: -                dp[i + 1][t] = dp[i][t] +        for t in range(T - 1, -1, -1): +            if t >= a and dp[t] < dp[t - a] + b: +                dp[t] = dp[t - a] + b +        max_sum[i] = dp[T - 1] -        if ans < dp[i + 1][T - 1] + D[i + 1][1]: -            ans = dp[i + 1][T - 1] + D[i + 1][1] +        if ans < max_sum[i] + D[i + 1][1]: +            ans = max_sum[i] + D[i + 1][1]