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

ls = []

for i in range(N):

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

  ls += [(a,b)]

ls.sort(key=lambda x:x[0])



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



for i in range(N):

  a,b = ls[i]

  for j in range(T-1, a-1, -1):

    dp[i+1][j] = max(dp[i][j], dp[i][j-a]+b)



last = [0]*N

last[-1] = ls[-1][1]

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

  last[i] = max(last[i+1], ls[i][1])



ans = 0



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

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



print(ans)

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

ls = []

for i in range(N):

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

  ls += [(a,b)]

ls.sort(key=lambda x:x[0])

t = [[0]*T for i in range(N+1)]

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

  a, b = ls[i-1]

  for j in range(T):

    if j-a>=0:

      t[i][j] = max(t[i-1][j-a]+b,t[i-1][j])

last = [0]

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

  last += [max(last[-1], ls[i][-1])]

ans = 0

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

  j = N-i

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

print(ans)
N, T = list(map(int, input().split()))
ls = []
for i in range(N):
    a, b = list(map(int, input().split()))
    ls += [(a, b)]
ls.sort(key=lambda x: x[0])
dp = [[0] * (T) for i in range(N + 1)]
for i in range(N):
    a, b = ls[i]
    for j in range(T - 1, a - 1, -1):
        dp[i + 1][j] = max(dp[i][j], dp[i][j - a] + b)
last = [0] * N
last[-1] = ls[-1][1]
for i in range(N - 2, -1, -1):
    last[i] = max(last[i + 1], ls[i][1])
ans = 0
for i in range(1, N + 1):
    ans = max(ans, max(dp[i - 1]) + last[i - 1])
print(ans)

N, T = list(map(int, input().split()))
ls = []
for i in range(N):
    a, b = list(map(int, input().split()))
    ls += [(a, b)]
ls.sort(key=lambda x: x[0])
t = [[0] * T for i in range(N + 1)]
for i in range(1, N + 1):
    a, b = ls[i - 1]
    for j in range(T):
        if j - a >= 0:
            t[i][j] = max(t[i - 1][j - a] + b, t[i - 1][j])
last = [0]
for i in range(N - 1, -1, -1):
    last += [max(last[-1], ls[i][-1])]
ans = 0
for i in range(1, N + 1):
    j = N - i
    ans = max(ans, last[j] + t[i][-1])
print(ans)

-dp = [[0] * (T) for i in range(N + 1)] -for i in range(N): -    a, b = ls[i] -    for j in range(T - 1, a - 1, -1): -        dp[i + 1][j] = max(dp[i][j], dp[i][j - a] + b) -last = [0] * N -last[-1] = ls[-1][1] -for i in range(N - 2, -1, -1): -    last[i] = max(last[i + 1], ls[i][1]) +t = [[0] * T for i in range(N + 1)] +for i in range(1, N + 1): +    a, b = ls[i - 1] +    for j in range(T): +        if j - a >= 0: +            t[i][j] = max(t[i - 1][j - a] + b, t[i - 1][j]) +last = [0] +for i in range(N - 1, -1, -1): +    last += [max(last[-1], ls[i][-1])] -    ans = max(ans, max(dp[i - 1]) + last[i - 1]) +    j = N - i +    ans = max(ans, last[j] + t[i][-1])