import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

input=lambda :sys.stdin.readline().rstrip()

def resolve():

    n,T=list(map(int,input().split()))

    AB=[tuple(map(int,input().split())) for _ in range(n)]

    ans=-INF



    # 何も考えずやる

    dp=[0]*T

    for i in range(n):

        a,b=AB[i]

        ndp=dp[:]

        for t in range(T):

            if(t+a<T):

                ndp[t+a]=max(ndp[t+a],dp[t]+b)

            else:

                ans=max(ans,dp[t]+b)

        dp=ndp

    ans=max(ans,max(dp))



    # 時間が最もかかるもののうち、価値が最大のものを最後にする

    AB.sort()

    la,lb=AB[-1]



    dp=[0]*T

    for i in range(n-1):

        a,b=AB[i]

        ndp=dp[:]

        for t in range(T):

            if(t+a<T):

                ndp[t+a]=max(ndp[t+a],dp[t]+b)

            else:

                ans=max(ans,dp[t]+b)

        dp=ndp

    ans=max(ans,max(dp)+lb)



    # 価値が最大のもののうち、時間が最もかかるものを最後にする

    AB.sort(lambda x:(x[1],x[0]))

    la,lb=AB[-1]



    dp=[0]*T

    for i in range(n-1):

        a,b=AB[i]

        ndp=dp[:]

        for t in range(T):

            if(t+a<T):

                ndp[t+a]=max(ndp[t+a],dp[t]+b)

            else:

                ans=max(ans,dp[t]+b)

        dp=ndp

    ans=max(ans,max(dp)+lb)



    print(ans)

resolve()
import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

input=lambda :sys.stdin.readline().rstrip()

def resolve():

    n,T=list(map(int,input().split()))

    AB=[tuple(map(int,input().split())) for _ in range(n)]

    AB.sort()



    dp=[0]*T

    ans=-INF

    for i in range(n):

        a,b=AB[i]

        ndp=dp[:]

        for t in range(T):

            if(t+a<T):

                ndp[t+a]=max(ndp[t+a],dp[t]+b)

            else:

                ans=max(ans,dp[t]+b)

        dp=ndp

    print((max(ans,max(dp))))

resolve()
import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n, T = list(map(int, input().split()))
    AB = [tuple(map(int, input().split())) for _ in range(n)]
    ans = -INF
    # 何も考えずやる
    dp = [0] * T
    for i in range(n):
        a, b = AB[i]
        ndp = dp[:]
        for t in range(T):
            if t + a < T:
                ndp[t + a] = max(ndp[t + a], dp[t] + b)
            else:
                ans = max(ans, dp[t] + b)
        dp = ndp
    ans = max(ans, max(dp))
    # 時間が最もかかるもののうち、価値が最大のものを最後にする
    AB.sort()
    la, lb = AB[-1]
    dp = [0] * T
    for i in range(n - 1):
        a, b = AB[i]
        ndp = dp[:]
        for t in range(T):
            if t + a < T:
                ndp[t + a] = max(ndp[t + a], dp[t] + b)
            else:
                ans = max(ans, dp[t] + b)
        dp = ndp
    ans = max(ans, max(dp) + lb)
    # 価値が最大のもののうち、時間が最もかかるものを最後にする
    AB.sort(lambda x: (x[1], x[0]))
    la, lb = AB[-1]
    dp = [0] * T
    for i in range(n - 1):
        a, b = AB[i]
        ndp = dp[:]
        for t in range(T):
            if t + a < T:
                ndp[t + a] = max(ndp[t + a], dp[t] + b)
            else:
                ans = max(ans, dp[t] + b)
        dp = ndp
    ans = max(ans, max(dp) + lb)
    print(ans)


resolve()

import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n, T = list(map(int, input().split()))
    AB = [tuple(map(int, input().split())) for _ in range(n)]
    AB.sort()
    dp = [0] * T
    ans = -INF
    for i in range(n):
        a, b = AB[i]
        ndp = dp[:]
        for t in range(T):
            if t + a < T:
                ndp[t + a] = max(ndp[t + a], dp[t] + b)
            else:
                ans = max(ans, dp[t] + b)
        dp = ndp
    print((max(ans, max(dp))))


resolve()

+    AB.sort() +    dp = [0] * T -    # 何も考えずやる -    dp = [0] * T -    ans = max(ans, max(dp)) -    # 時間が最もかかるもののうち、価値が最大のものを最後にする -    AB.sort() -    la, lb = AB[-1] -    dp = [0] * T -    for i in range(n - 1): -        a, b = AB[i] -        ndp = dp[:] -        for t in range(T): -            if t + a < T: -                ndp[t + a] = max(ndp[t + a], dp[t] + b) -            else: -                ans = max(ans, dp[t] + b) -        dp = ndp -    ans = max(ans, max(dp) + lb) -    # 価値が最大のもののうち、時間が最もかかるものを最後にする -    AB.sort(lambda x: (x[1], x[0])) -    la, lb = AB[-1] -    dp = [0] * T -    for i in range(n - 1): -        a, b = AB[i] -        ndp = dp[:] -        for t in range(T): -            if t + a < T: -                ndp[t + a] = max(ndp[t + a], dp[t] + b) -            else: -                ans = max(ans, dp[t] + b) -        dp = ndp -    ans = max(ans, max(dp) + lb) -    print(ans) +    print((max(ans, max(dp))))