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

a = [0]

b = [0]

for i in range(n):

    a_, b_ = list(map(int, input().split()))

    a.append(a_)

    b.append(b_)



dp1 = [[0] * t for _ in range(n+2)]

dp2 = [[0] * t for _ in range(n+2)]



for i in range(n):

    for j in range(t):

        if a[i+1] > j:

            dp1[i+1][j] = dp1[i][j]

        else:

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



for i in range(n+1, 1, -1):

    for j in range(t):

        if a[i-1] > j:

            dp2[i-1][j] = dp2[i][j]

        else:

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



ans = 0

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

    for j in range(t):

        total = b[i] +dp1[i-1][j] + dp2[i+1][t-1-j]

        ans = max(ans, total)

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

AB = sorted([list(map(int, input().split())) for _ in range(N)])



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



for i in range(1, T):

    for j in range(N-1):

        if AB[j][0] <= i:

            dp[i][j] = max(dp[i][j-1], dp[i-AB[j][0]][j-1] + AB[j][1])

        else:

            dp[i][j] = dp[i][j-1]



ans = 0

for j in range(1, N):

    ans = max(ans, dp[-1][j-1] + AB[j][1])



print(ans)
n, t = list(map(int, input().split()))
a = [0]
b = [0]
for i in range(n):
    a_, b_ = list(map(int, input().split()))
    a.append(a_)
    b.append(b_)
dp1 = [[0] * t for _ in range(n + 2)]
dp2 = [[0] * t for _ in range(n + 2)]
for i in range(n):
    for j in range(t):
        if a[i + 1] > j:
            dp1[i + 1][j] = dp1[i][j]
        else:
            dp1[i + 1][j] = max(dp1[i][j], dp1[i][j - a[i + 1]] + b[i + 1])
for i in range(n + 1, 1, -1):
    for j in range(t):
        if a[i - 1] > j:
            dp2[i - 1][j] = dp2[i][j]
        else:
            dp2[i - 1][j] = max(dp2[i][j], dp2[i][j - a[i - 1]] + b[i - 1])
ans = 0
for i in range(1, n + 1):
    for j in range(t):
        total = b[i] + dp1[i - 1][j] + dp2[i + 1][t - 1 - j]
        ans = max(ans, total)
print(ans)

N, T = list(map(int, input().split()))
AB = sorted([list(map(int, input().split())) for _ in range(N)])
dp = [[0] * N for _ in range(T)]
for i in range(1, T):
    for j in range(N - 1):
        if AB[j][0] <= i:
            dp[i][j] = max(dp[i][j - 1], dp[i - AB[j][0]][j - 1] + AB[j][1])
        else:
            dp[i][j] = dp[i][j - 1]
ans = 0
for j in range(1, N):
    ans = max(ans, dp[-1][j - 1] + AB[j][1])
print(ans)

-n, t = list(map(int, input().split())) -a = [0] -b = [0] -for i in range(n): -    a_, b_ = list(map(int, input().split())) -    a.append(a_) -    b.append(b_) -dp1 = [[0] * t for _ in range(n + 2)] -dp2 = [[0] * t for _ in range(n + 2)] -for i in range(n): -    for j in range(t): -        if a[i + 1] > j: -            dp1[i + 1][j] = dp1[i][j] +N, T = list(map(int, input().split())) +AB = sorted([list(map(int, input().split())) for _ in range(N)]) +dp = [[0] * N for _ in range(T)] +for i in range(1, T): +    for j in range(N - 1): +        if AB[j][0] <= i: +            dp[i][j] = max(dp[i][j - 1], dp[i - AB[j][0]][j - 1] + AB[j][1]) -            dp1[i + 1][j] = max(dp1[i][j], dp1[i][j - a[i + 1]] + b[i + 1]) -for i in range(n + 1, 1, -1): -    for j in range(t): -        if a[i - 1] > j: -            dp2[i - 1][j] = dp2[i][j] -        else: -            dp2[i - 1][j] = max(dp2[i][j], dp2[i][j - a[i - 1]] + b[i - 1]) +            dp[i][j] = dp[i][j - 1] -for i in range(1, n + 1): -    for j in range(t): -        total = b[i] + dp1[i - 1][j] + dp2[i + 1][t - 1 - j] -        ans = max(ans, total) +for j in range(1, N): +    ans = max(ans, dp[-1][j - 1] + AB[j][1])