import sys



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

S = sys.stdin.readlines()

A = []

B = []

for s in S:

    a, b = list(map(int, s.split()))

    A.append(a)

    B.append(b)



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

for i in range(N):

    for j in range(T):

        here = dp1[i][j]

        if j < A[i]:

            dp1[i + 1][j] = here

        else:

            dp1[i + 1][j] = max(here, dp1[i][j - A[i]] + B[i])



dp2 = [[0] * T for _ in range(N + 2)]

for i in range(N, 0, -1):

    for j in range(T):

        here = dp2[i + 1][j]

        if j < A[i - 1]:

            dp2[i][j] = here

        else:

            dp2[i][j] = max(here, dp2[i + 1][j - A[i - 1]] + B[i - 1])



ans = 0

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

    t = 0

    for j in range(T):

        t2 = dp1[i - 1][j] + dp2[i + 1][T - 1 - j]

        t = max(t, t2)

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



print(ans)

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

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

D.sort()

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



for i in range(N):

    A, B = D[i]

    for j in range(T):

        here = dp[i][j]

        if j < A:

            dp[i + 1][j] = here

        else:

            dp[i + 1][j] = max(here, dp[i][j - A] + B)



maxB = [0] * (N + 1)

t = 0

for i in range(N - 1, -1, -1):

    t = max(t, D[i][1])

    maxB[i] = t



ans = 0

for i in range(N):

    ans = max(ans, dp[i + 1][T - 1] + maxB[i + 1])



print(ans)

import sys

N, T = list(map(int, input().split()))
S = sys.stdin.readlines()
A = []
B = []
for s in S:
    a, b = list(map(int, s.split()))
    A.append(a)
    B.append(b)
dp1 = [[0] * T for _ in range(N + 1)]
for i in range(N):
    for j in range(T):
        here = dp1[i][j]
        if j < A[i]:
            dp1[i + 1][j] = here
        else:
            dp1[i + 1][j] = max(here, dp1[i][j - A[i]] + B[i])
dp2 = [[0] * T for _ in range(N + 2)]
for i in range(N, 0, -1):
    for j in range(T):
        here = dp2[i + 1][j]
        if j < A[i - 1]:
            dp2[i][j] = here
        else:
            dp2[i][j] = max(here, dp2[i + 1][j - A[i - 1]] + B[i - 1])
ans = 0
for i in range(1, N + 1):
    t = 0
    for j in range(T):
        t2 = dp1[i - 1][j] + dp2[i + 1][T - 1 - j]
        t = max(t, t2)
    ans = max(ans, t + B[i - 1])
print(ans)

N, T = list(map(int, input().split()))
D = [tuple(map(int, input().split())) for _ in range(N)]
D.sort()
dp = [[0] * T for _ in range(N + 1)]
for i in range(N):
    A, B = D[i]
    for j in range(T):
        here = dp[i][j]
        if j < A:
            dp[i + 1][j] = here
        else:
            dp[i + 1][j] = max(here, dp[i][j - A] + B)
maxB = [0] * (N + 1)
t = 0
for i in range(N - 1, -1, -1):
    t = max(t, D[i][1])
    maxB[i] = t
ans = 0
for i in range(N):
    ans = max(ans, dp[i + 1][T - 1] + maxB[i + 1])
print(ans)

-import sys - -S = sys.stdin.readlines() -A = [] -B = [] -for s in S: -    a, b = list(map(int, s.split())) -    A.append(a) -    B.append(b) -dp1 = [[0] * T for _ in range(N + 1)] +D = [tuple(map(int, input().split())) for _ in range(N)] +D.sort() +dp = [[0] * T for _ in range(N + 1)] +    A, B = D[i] -        here = dp1[i][j] -        if j < A[i]: -            dp1[i + 1][j] = here +        here = dp[i][j] +        if j < A: +            dp[i + 1][j] = here -            dp1[i + 1][j] = max(here, dp1[i][j - A[i]] + B[i]) -dp2 = [[0] * T for _ in range(N + 2)] -for i in range(N, 0, -1): -    for j in range(T): -        here = dp2[i + 1][j] -        if j < A[i - 1]: -            dp2[i][j] = here -        else: -            dp2[i][j] = max(here, dp2[i + 1][j - A[i - 1]] + B[i - 1]) +            dp[i + 1][j] = max(here, dp[i][j - A] + B) +maxB = [0] * (N + 1) +t = 0 +for i in range(N - 1, -1, -1): +    t = max(t, D[i][1]) +    maxB[i] = t -for i in range(1, N + 1): -    t = 0 -    for j in range(T): -        t2 = dp1[i - 1][j] + dp2[i + 1][T - 1 - j] -        t = max(t, t2) -    ans = max(ans, t + B[i - 1]) +for i in range(N): +    ans = max(ans, dp[i + 1][T - 1] + maxB[i + 1])