# dp[i][t_sum]: i番目までの品物で時間がt_sumを超えないように選んだときの、満足度の総和の最大値

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

items = [tuple(map(int, input().split())) for i in range(N)]



dp1 = [[0 for t_sum in range(T+1)] for i in range(N+2)]

dp2 = [[0 for t_sum in range(T+1)] for i in range(N+2)]



for i in range(N):

    for t_sum in range(T+1):

        if t_sum-items[i][0] >= 0:

            dp1[i+1][t_sum] = max(dp1[i+1][t_sum], dp1[i]

                                  [t_sum-items[i][0]]+items[i][1])

        dp1[i+1][t_sum] = max(dp1[i+1][t_sum], dp1[i][t_sum])



    for t_sum in range(T+1):

        if t_sum-items[N-1-i][0] >= 0:

            dp2[N-1-i][t_sum] = max(dp2[N-1-i][t_sum], dp2[N-i]

                                    [t_sum-items[N-1-i][0]]+items[N-1-i][1])

        dp2[N-1-i][t_sum] = max(dp2[N-1-i][t_sum], dp2[N-i][t_sum])





ans = 0

for i in range(N):

    for t in range(T):

        ans = max(ans, dp1[i][t]+dp2[i+1][T-1-t]+items[i][1])

print(ans)

# dp[i][t_sum]: i番目までの品物で時間がt_sumを超えないように選んだときの、満足度の総和の最大値

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

items = sorted([tuple(map(int, input().split()))

                for i in range(N)], key=lambda x: x[0])



dp1 = [[0 for t_sum in range(T+1)] for i in range(N+2)]



for i in range(N):

    for t_sum in range(T+1):

        if t_sum-items[i][0] >= 0:

            dp1[i+1][t_sum] = max(dp1[i+1][t_sum], dp1[i]

                                  [t_sum-items[i][0]]+items[i][1])

        dp1[i+1][t_sum] = max(dp1[i+1][t_sum], dp1[i][t_sum])



ans = 0

for i in range(N):

    for t in range(T):

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

print(ans)

# dp[i][t_sum]: i番目までの品物で時間がt_sumを超えないように選んだときの、満足度の総和の最大値
N, T = list(map(int, input().split()))
items = [tuple(map(int, input().split())) for i in range(N)]
dp1 = [[0 for t_sum in range(T + 1)] for i in range(N + 2)]
dp2 = [[0 for t_sum in range(T + 1)] for i in range(N + 2)]
for i in range(N):
    for t_sum in range(T + 1):
        if t_sum - items[i][0] >= 0:
            dp1[i + 1][t_sum] = max(
                dp1[i + 1][t_sum], dp1[i][t_sum - items[i][0]] + items[i][1]
            )
        dp1[i + 1][t_sum] = max(dp1[i + 1][t_sum], dp1[i][t_sum])
    for t_sum in range(T + 1):
        if t_sum - items[N - 1 - i][0] >= 0:
            dp2[N - 1 - i][t_sum] = max(
                dp2[N - 1 - i][t_sum],
                dp2[N - i][t_sum - items[N - 1 - i][0]] + items[N - 1 - i][1],
            )
        dp2[N - 1 - i][t_sum] = max(dp2[N - 1 - i][t_sum], dp2[N - i][t_sum])
ans = 0
for i in range(N):
    for t in range(T):
        ans = max(ans, dp1[i][t] + dp2[i + 1][T - 1 - t] + items[i][1])
print(ans)

# dp[i][t_sum]: i番目までの品物で時間がt_sumを超えないように選んだときの、満足度の総和の最大値
N, T = list(map(int, input().split()))
items = sorted([tuple(map(int, input().split())) for i in range(N)], key=lambda x: x[0])
dp1 = [[0 for t_sum in range(T + 1)] for i in range(N + 2)]
for i in range(N):
    for t_sum in range(T + 1):
        if t_sum - items[i][0] >= 0:
            dp1[i + 1][t_sum] = max(
                dp1[i + 1][t_sum], dp1[i][t_sum - items[i][0]] + items[i][1]
            )
        dp1[i + 1][t_sum] = max(dp1[i + 1][t_sum], dp1[i][t_sum])
ans = 0
for i in range(N):
    for t in range(T):
        ans = max(ans, dp1[i][t] + items[i][1])
print(ans)

-items = [tuple(map(int, input().split())) for i in range(N)] +items = sorted([tuple(map(int, input().split())) for i in range(N)], key=lambda x: x[0]) -dp2 = [[0 for t_sum in range(T + 1)] for i in range(N + 2)] -    for t_sum in range(T + 1): -        if t_sum - items[N - 1 - i][0] >= 0: -            dp2[N - 1 - i][t_sum] = max( -                dp2[N - 1 - i][t_sum], -                dp2[N - i][t_sum - items[N - 1 - i][0]] + items[N - 1 - i][1], -            ) -        dp2[N - 1 - i][t_sum] = max(dp2[N - 1 - i][t_sum], dp2[N - i][t_sum]) -        ans = max(ans, dp1[i][t] + dp2[i + 1][T - 1 - t] + items[i][1]) +        ans = max(ans, dp1[i][t] + items[i][1])