Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.
The restaurant has the following rules:
 - You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
 - You cannot order the same kind of dish more than once.
 - Until you finish eating the dish already served, you cannot order a new dish.
 - After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices?

-----Constraints-----
 - 2 \leq N \leq 3000
 - 1 \leq T \leq 3000
 - 1 \leq A_i \leq 3000
 - 1 \leq B_i \leq 3000
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N

-----Output-----
Print the maximum possible happiness Takahashi can achieve.

-----Sample Input-----
2 60
10 10
100 100

-----Sample Output-----
110

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.
Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

Difficulty: MEDIUM_HARD

Solutions:
def main():
	(N, T) = list(map(int, input().split()))
	AB = [list(map(int, input().split())) for _ in range(N)]
	dp = [-1] * (T + 3000)
	dp[0] = 0
	for (a, b) in sorted(AB):
		for i in range(T - 1, -1, -1):
			if dp[i] == -1:
				continue
			if dp[i + a] < dp[i] + b:
				dp[i + a] = dp[i] + b
	print(max(dp))
main()


import sys
import numpy as np
from operator import itemgetter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return list(map(int, input().split()))

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
(N, T) = MAP()
AB = []
for i in range(N):
	(a, b) = MAP()
	AB.append((a, b))
AB.sort(key=itemgetter(1), reverse=True)
AB.sort(key=itemgetter(0))
TMAX = 6007
dp = np.zeros((N + 1, TMAX), dtype=np.int64)
for i in range(N):
	(a, b) = AB[i]
	dp[i + 1] = dp[i]
	dp[i + 1, a:a + T] = np.maximum(dp[i + 1, a:a + T], dp[i, :T] + b)
print(dp[N].max())


import numpy as np
(N, T) = map(int, input().split())
data = []
for i in range(N):
	(a, b) = map(int, input().split())
	data.append((a, b))
data.sort()
data = np.array(data)
dp = np.zeros(T)
ans = 0
for (a, b) in data:
	ans = max(ans, dp[-1] + b)
	if a < T:
		newtable = dp[:T - a] + b
		dp[a:] = np.maximum(dp[a:], newtable)
print(int(ans))


(n, t) = map(int, input().split())
ab = []
for _ in range(n):
	ab.append(list(map(int, input().split())))
ab.sort(key=lambda x: x[0])
dp = [-1] * (t + 1)
dp[0] = 0
for (a, b) in ab:
	tmp = dp[:]
	for i in range(t):
		if dp[i] >= 0 and dp[i] + b > tmp[min(i + a, t)]:
			tmp[min(i + a, t)] = dp[i] + b
	dp = tmp
print(max(dp))


def main():
	(N, T) = list(map(int, input().split()))
	L = [[int(x) for x in input().split()] for _ in range(N)]
	L.sort(key=lambda x: x[0])
	dp = [0] + [-1] * (T + L[-1][0])
	for (a, b) in L:
		for j in range(T - 1, -1, -1):
			if dp[j] >= 0 and dp[j + a] < dp[j] + b:
				dp[j + a] = dp[j] + b
	print(max(dp))

def __starting_point():
	main()
__starting_point()


(N, T) = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]
dp = [-1] * 6001
dp[0] = 0
for (a, b) in sorted(X):
	for j in reversed(range(T)):
		if dp[j] == -1:
			continue
		dp[j + a] = max(dp[j + a], dp[j] + b)
print(max(dp))


def solve():
	from sys import stdin
	f_i = stdin
	(N, T) = map(int, f_i.readline().split())
	AB = [tuple(map(int, f_i.readline().split())) for i in range(N)]
	AB.sort()
	dp = [[0] * T for i in range(N + 1)]
	for (i, AB_i) in enumerate(AB, start=1):
		(A_i, B_i) = AB_i
		dp_i = dp[i]
		dp_pre = dp[i - 1]
		dp_i[:A_i] = dp_pre[:A_i]
		for (j, t) in enumerate(zip(dp_pre[A_i:], dp_pre), start=A_i):
			(x, y) = t
			if x < y + B_i:
				dp_i[j] = y + B_i
			else:
				dp_i[j] = x
	ans = max((dp[k][-1] + AB[k][1] for k in range(N)))
	print(ans)
solve()


import sys

def solve():
	readline = sys.stdin.buffer.readline
	mod = 10 ** 9 + 7
	(n, t) = list(map(int, readline().split()))
	ab = [list(map(int, readline().split())) for _ in range(n)]
	ab.sort()
	dp = [0] * t
	mt = 0
	for (a, b) in ab:
		mt = max(mt, dp[-1] + b)
		for i in range(t - a - 1, -1, -1):
			dp[i + a] = max(dp[i + a], dp[i] + b)
	print(mt)
solve()


(n, t) = map(int, input().split())
dishes = []
for _ in range(n):
	(time, point) = map(int, input().split())
	dishes.append([time, point])
dishes.sort()
DP = [0] * t
candidates = []
for (time, point) in dishes:
	candidates.append(max(DP) + point)
	for i in range(t - 1, time - 1, -1):
		DP[i] = max(DP[i], DP[i - time] + point)
print(max(candidates))


def main():
	(N, T) = map(int, input().split())
	ab = [tuple(map(int, input().split())) for i in range(N)]
	ab.sort(key=lambda x: x[0])
	t = ab[-1][0]
	dp = [-1] * (T + t + 1)
	dp[0] = 0
	for (a, b) in ab:
		for i in range(T - 1, -1, -1):
			if dp[i] < 0:
				continue
			if dp[i + a] < dp[i] + b:
				dp[i + a] = dp[i] + b
	ans = max(dp)
	print(ans)
main()


import numpy as np
import sys
mod = 10 ** 9 + 7
eps = 10 ** (-7)

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_str():
	return list(sys.stdin.readline().split())
(N, T) = inpl()
dp = np.zeros(6001, int)
ABs = [inpl() for _ in range(N)]
ABs.sort()
for (A, B) in ABs:
	dp[A:T + A] = np.maximum(dp[A:T + A], dp[:T] + B)
print(max(dp))


import numpy as np
(N, T) = list(map(int, input().split()))
T -= 1
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])
AB.sort(key=lambda x: x[0], reverse=True)
dp = [[0] * (T + 1) for _ in range(N + 1)]
dp = np.zeros(T + 1, dtype=np.int32)
ans = AB[N - 1][1]
for i in range(N - 1, 0, -1):
	if AB[i][0] <= T + 1:
		dp[AB[i][0]:] = np.maximum(dp[AB[i][0]:], dp[:T + 1 - AB[i][0]] + AB[i][1])
	tmp = dp[T] + AB[i - 1][1]
	ans = max(ans, tmp)
print(ans)