def knapsack(n, w, info):

  dp = [[0]*(w + 1) for i in range(n + 1)]



  for i in range(n):

    for j in range(w + 1):

      weight, value = info[i]

      if j < weight:

        dp[i + 1][j] = dp[i][j]

      else:

        dp[i + 1][j] = max(dp[i][j], dp[i][j - weight] + value)

  return dp



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

food = []

for _ in range(N):

  food.append(list(map(int,input().split())))

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

dp = knapsack(N,T,food)



C = [0]*(N+1)

for i in range(N)[::-1]:

  C[i] = max(C[i+1], food[i][1])



ans = 0

for i in range(N+1):

  ans = max(dp[i][-2] + C[i], ans)



print(ans)

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

foods = []

for _ in range(N):

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

  foods.append([a,b])



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



# dp[i][j] = 1～i 番目の料理で j 分以内に完食できる美味しさの合計の最大値

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



for i in range(N):

  a,b = foods[i]

  for j in range(T+1):

    if j >= a:

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

    else:

      dp[i+1][j] = dp[i][j]



# for d in dp:

#   print(d)



ans = 0

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

  b = foods[i-1][1]

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



print((max(ans, dp[-1][-1])))
def knapsack(n, w, info):
    dp = [[0] * (w + 1) for i in range(n + 1)]
    for i in range(n):
        for j in range(w + 1):
            weight, value = info[i]
            if j < weight:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i][j], dp[i][j - weight] + value)
    return dp


N, T = list(map(int, input().split()))
food = []
for _ in range(N):
    food.append(list(map(int, input().split())))
food.sort(key=lambda x: x[0])
dp = knapsack(N, T, food)
C = [0] * (N + 1)
for i in range(N)[::-1]:
    C[i] = max(C[i + 1], food[i][1])
ans = 0
for i in range(N + 1):
    ans = max(dp[i][-2] + C[i], ans)
print(ans)

N, T = list(map(int, input().split()))
foods = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    foods.append([a, b])
foods.sort(key=lambda x: x[0])
# dp[i][j] = 1～i 番目の料理で j 分以内に完食できる美味しさの合計の最大値
dp = [[0] * (T + 1) for _ in range(N + 1)]
for i in range(N):
    a, b = foods[i]
    for j in range(T + 1):
        if j >= a:
            dp[i + 1][j] = max(dp[i][j], dp[i][j - a] + b)
        else:
            dp[i + 1][j] = dp[i][j]
# for d in dp:
#   print(d)
ans = 0
for i in range(1, N + 1):
    b = foods[i - 1][1]
    ans = max(ans, dp[i][-1], dp[i - 1][-2] + b)
print((max(ans, dp[-1][-1])))

-def knapsack(n, w, info): -    dp = [[0] * (w + 1) for i in range(n + 1)] -    for i in range(n): -        for j in range(w + 1): -            weight, value = info[i] -            if j < weight: -                dp[i + 1][j] = dp[i][j] -            else: -                dp[i + 1][j] = max(dp[i][j], dp[i][j - weight] + value) -    return dp - - -food = [] +foods = [] -    food.append(list(map(int, input().split()))) -food.sort(key=lambda x: x[0]) -dp = knapsack(N, T, food) -C = [0] * (N + 1) -for i in range(N)[::-1]: -    C[i] = max(C[i + 1], food[i][1]) +    a, b = list(map(int, input().split())) +    foods.append([a, b]) +foods.sort(key=lambda x: x[0]) +# dp[i][j] = 1～i 番目の料理で j 分以内に完食できる美味しさの合計の最大値 +dp = [[0] * (T + 1) for _ in range(N + 1)] +for i in range(N): +    a, b = foods[i] +    for j in range(T + 1): +        if j >= a: +            dp[i + 1][j] = max(dp[i][j], dp[i][j - a] + b) +        else: +            dp[i + 1][j] = dp[i][j] +# for d in dp: +#   print(d) -for i in range(N + 1): -    ans = max(dp[i][-2] + C[i], ans) -print(ans) +for i in range(1, N + 1): +    b = foods[i - 1][1] +    ans = max(ans, dp[i][-1], dp[i - 1][-2] + b) +print((max(ans, dp[-1][-1])))