Problem:


Score : 500 points

Problem Statement

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 1

2 60
10 10
100 100

Sample Output 1

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.

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

We cannot order three dishes, in whatever order we place them.

Sample Input 4

10 100
15 23
20 18
13 17
24 12
18 29
19 27
23 21
18 20
27 15
22 25

Sample Output 4

145



Sample input:
2 60
10 10
100 100


Sample output:
110

Submission status: Time Limit Exceeded

Solution code:
import subprocess
import gzip, base64, os
import stat

gz = b'H4sIAAmV0F0C/+19e3xTRfb4TdJHgNIEFrU8lIABi0hpoGDrswUCN5BipYVlVbz0kdJq23Sbm1p8om1dY6yyrrvLd1/y2e+u+vO739+6rrurrEIRoeADeQgWukpFwBsKAipYRMjvnJm56dyb3AKuv+8fv88v0Mw9Z848zpkzZ87M3Mw85PbONptMgvoxCzcJCP1lZD6B8xn+sfIYCeByhQHwPVoYJaQAnMzR5Qv5mrCXZa2GVkZngb8k+Ms1UzjXnK8JRzE6N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n4oqS06CKltOjipbToQitf3iAH5GBlZVa5IElzZy6QvJ7iEkkS+m7fleRaqRxv2Q0ICVCSVOGXltX4y0prpArorgGpNNgklPtr62t8sq8ia3pe7pTERFLfnrMEgm9YLtB964pgbe1ySMJBUt9WNSOVpNkLCgrdYAxm4R3CpObsmXF9y+zZxe4SqaRghteNFJosKrQIcomkIM360fyCQs9MATe5VZz+YtP8fP5qyP4vgtXR0gsrywNBwrkQK+Ei70TV1yDhdcFx1Ux4obGGKivvgq5mJtd/xmfPXTKaQET93JmsrYPLJTDpJL6oVJs3vRKU4dgNn+SeUS0q7qpQXXS/N8PquYm7Y1Xfyn33mMYXY3CfcXyTGt49HFcavusCbV5TI8Vuav5uNzNrcyaXsyZgIP7mZl2N+r26+xaJ/KBGCgZ8FfE3Nutq0ODzxVWgv3uVtcnpvbK6DPq5O1zf6Q2vB0+k3nEX5yY0E4lu/07QkxLc7aunSnBdtF4xgD3VIsZdlh2vb+SO5Xg0d/u3Pv+KgF+qgpEWhEcv9U3Y59DaMRfqe7ylXG+32J3Aem3R3rKt5y7+fmo9i2WBAKcRsSva4xrjgu5tzs/n7h6Ov206oRWJu3o6PhPt9dyJcsF+ZJSDVA+sg/Mu3e1bziyf4UXzOtNLrmhOYCC+24Xruq7v81fq1Sl2Lb2WAzKWf7dLz3Vlkt/0xanQBd85nmBYvpYflkljNhp1d4P73BO1J3chvE4U5OW8RDe5x6vN93BjeYKBMQF3bISLM3Saa8Ihjl3bTSo3HdUe5vvxgwt3oXsCD4deyZ6gEvr76uNVuX55X8u7cuKvPdf7HdV1ArVr/wdY7g8yhowAAA=='
bin = gzip.decompress(base64.b64decode(gz))
p = os.path.dirname(__file__)
pymain = os.path.join(p, "a.out")
with open(pymain, "wb") as f:
    f.write(bin)
os.chmod(pymain, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
if p == "":
    pymain = "./" + pymain
subprocess.call(pymain)


# compiled by pythran
'''
#pythran export main2()
import numpy as np
i8 = np.int64
i4 = np.int32

def solve(A, B, N, T):
    ret = 0
    d = np.empty(T, i8)
    for k in range(N):
        for i in range(T):
            d[i] = -1
        d[0] = 0
        for i in range(N):
            if i == k:
                continue
            for j in range(T - A[i] - 1, -1, -1):
                if d[j] >= 0:
                    d[j + A[i]] = max(d[j + A[i]], d[j] + B[i])
            ret = max(ret, np.max(d) + B[k])
    return ret

def main2():
    stdin = open('/dev/stdin')
    N, T = map(int, stdin.readline().split())
    A = np.empty(N, i8)
    B = np.empty(N, i8)
    for i in range(N):
        A[i], B[i] = map(int, stdin.readline().split())
    ans = solve(A, B, N, T)
    print(ans)
'''

Submission status: Time Limit Exceeded

Solution code:
import itertools
J=input().split()
AB=[]
for i in range(int(J[0])):
    ab=input().split()
    AB.append([int(ab[0]),int(ab[1])])
    
def Hyo(ab):
    E=0;T=0
    for i in range(int(J[0])):
        if int(J[1])-0.5<T:break
        E+=ab[i][1];T+=ab[i][0]
        
    return E

ABS=sorted(itertools.permutations(AB),key=Hyo)
print(Hyo(ABS[-1]))

Submission status: Time Limit Exceeded

Solution code:
N,T = map(int,input().split())
C = []
for i in range(N):
    a,b = map(int,input().split())
    C.append([a,b])
C.sort()
dp = [[0 for i in range(T)]for j in range(N+1)]
for i in range(1,N+1):
    a,b = C[i-1][0],C[i-1][1]
    for t in range(1,T):
        p = dp[i-1][t]
        if t >= a:
            q = dp[i-1][t-a] + b
            dp[i][t] = max(p,q)
        else:
            dp[i][t] = p
M = 0
for i in range(1,N+1):
    M = max(M,C[i-1][1]+dp[i-1][T-1])
print(M)

Submission status: Time Limit Exceeded

Solution code:
N,T = map(int,input().split())
import numpy as np
A = []
B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
arg = np.argsort(A)
A = np.array(A)[arg]
B = np.array(B)[arg]
dp = [[0 for i in range(T)]for j in range(N+1)]
for i in range(1,N+1):
    a,b = A[i-1],B[i-1]
    for t in range(1,T):
        p = dp[i-1][t]
        if t >= a:
            q = dp[i-1][t-a] + b
            dp[i][t] = max(p,q)
        else:
            dp[i][t] = p
M = 0
for i in range(1,N+1):
    M = max(M,B[i-1]+dp[i-1][T-1])
print(M)

Submission status: Time Limit Exceeded

Solution code:
N,T = map(int,input().split())
import bisect
C = []
for i in range(N):
    a,b = map(int,input().split())
    C.append([a,b])
C.sort()
dp = [[0 for i in range(T)]for j in range(N+1)]
for i in range(1,N+1):
    a,b = C[i-1][0],C[i-1][1]
    for t in range(1,T):
        p = dp[i-1][t]
        if t >= a:
            q = dp[i-1][t-a] + b
            dp[i][t] = max(p,q)
        else:
            dp[i][t] = p
M = 0
for i in range(1,N+1):
    M = max(M,C[i-1][1]+dp[i-1][T-1])
print(M)

Submission status: Time Limit Exceeded

Solution code:
N,T = map(int,input().split())
import bisect
C = []
for i in range(N):
    a,b = map(int,input().split())
    index = bisect.bisect_left(C,[a,b])
    C.insert(index,[a,b])
C = sorted(C)
dp = [[0 for i in range(T)]for j in range(N+1)]
for i in range(1,N+1):
    a,b = C[i-1][0],C[i-1][1]
    for t in range(1,T):
        p = dp[i-1][t]
        if t >= a:
            q = dp[i-1][t-a] + b
            dp[i][t] = max(p,q)
        else:
            dp[i][t] = p
M = 0
for i in range(1,N+1):
    M = max(M,C[i-1][1]+dp[i-1][T-1])
print(M)

Submission status: Time Limit Exceeded

Solution code:
N,T = map(int,input().split())
import copy
A = []
B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
C = zip(A,B)
C = sorted(C)
A,B = zip(*C)
A = list(A)
B = list(B)
dp = [[0 for i in range(T)]for j in range(N+1)]
for i in range(1,N+1):
    a,b = A[i-1],B[i-1]
    for t in range(1,T):
        p = dp[i-1][t]
        if t >= a:
            q = dp[i-1][t-a] + b
            dp[i][t] = max(p,q)
        else:
            dp[i][t] = p
M = 0
for i in range(1,N+1):
    M = max(M,B[i-1]+dp[i-1][T-1])
print(M)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)


def solve(N: int, T: int, A: "List[int]", B: "List[int]"):
    max_t = T + max(A)
    dp = [-1] * (max_t + 1)
    dp[0] = 0
    p = []
    for i in range(N):
        p.append((A[i], B[i]))
    p.sort()
    for i in range(N):
        for t in range(T - 1, -1, -1):
            if dp[t] >= 0:
                dp[t + p[i][0]] = max(dp[t + p[i][0]], dp[t] + p[i][1])
    ret = 0
    for v in dp:
        ret = max(ret, v)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, T, A, B)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, T = map(int, input().split())
AB = []
for i in range(N):
    AB.append(tuple(map(int, input().split())))

AB.sort()

ans = 0
dp = [0] * T

for a, b in AB:
    ans = max(ans, dp[-1] + b)
    for j in range(T-1, -1, -1):
        if j >= a:
            dp[j] = max(dp[j], dp[j-a] + b)

print(ans)

Submission status: Time Limit Exceeded

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

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

for i, (a, b) in enumerate(AB):
    for t in range(T - 1, -1, -1):
        dp[min(T, t + a)] = max(dp[min(T, t + a)], dp[t] + b)

print(dp[T])

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n, t = ril()
ls = rils(n)
ls.sort()

lim = t + 3001
values = [0] * lim
for a, b in ls:
    for j in reversed(range(lim - a)):
        if j < t:
            values[j + a] = max(values[j + a], values[j] + b)
print(max(values))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

n, t = map(int, input().split())
menu = []

for _ in range(n):
    a, b = map(int, input().split())
    menu.append((a, b))

menu = sorted(menu)

dp = defaultdict(int)
dp[0] = 0

score = 0

for a, b in menu:
    for time, utility in list(dp.items()):
        if time + a >= t:
            score = max(score, utility + b)
        else:
            dp[time + a] = max(dp[time + a], utility + b)

print(max(score, max(dp.values())))

Submission status: Time Limit Exceeded

Solution code:
n,t=[int(j) for j in input().split()]
ab=[]
ans=[]
dp=[0]*t
for i in range(n):
    tmp=[int(j) for j in input().split()]
    ab.append(tmp)
ab.sort()
for a,b in ab:
    for i in range(t)[::-1]:
        if dp[i]!=0 or i==0:
            if i+a>=t:
                ans.append(dp[i]+b)
            else:
                dp[i+a]=max(dp[i+a],dp[i]+b)
if ans:
    print(max(ans))
else:
    print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
#
import sys
import math
import numpy as np
import itertools
# いくつか入力
n,t = (int(i) for i in input().split())

# n行の複数列ある数値を二次元配列へ。x[n][?]
ab = [[int(i) for i in input().split()] for i in range(n)]
# 0:weight 1:value

ab = sorted(ab,key=lambda x: x[1])

dp = np.zeros((n+1,t),int)
dp2 = np.zeros((n+1,t),int)

for i in range(n):
    w = ab[i][0]
    dp[i+1][w:] = np.fmax(dp[i][:-w] + ab[i][1], dp[i][w:])
    dp2[i+1][w:] = np.fmax(dp2[i][:-w] + ab[i][1],dp[i][w:] + ab[i][1], dp2[i][w:])
    dp[i+1][:w] = dp[i][:w]
    dp2[i+1][:w] = np.fmax(dp[i][:w] + ab[i][1], dp2[i][:w])

answer =0
for i in range(n+1):
    for w in range(t):
        answer = max(answer, dp2[i][w])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
#
import sys
import math
import numpy as np
import itertools
# いくつか入力
n,t = (int(i) for i in input().split())  

# n行の複数列ある数値を二次元配列へ。x[n][?]
ab = [[int(i) for i in input().split()] for i in range(n)]
# 0:weight 1:value

ab = sorted(ab,key=lambda x: x[1])

dp = np.zeros((n+1,t),int)
dp2 = np.zeros((n+1,t),int)


for i in range(n):
    for w in range(t):
        if w >= ab[i][0] :
            dp[i+1][w] = max(dp[i][w-ab[i][0]] + ab[i][1], dp[i][w])
        else:
            dp[i+1][w] = dp[i][w]
        if w >= ab[i][0] :
            dp2[i+1][w] = max(dp2[i][w-ab[i][0]] + ab[i][1],dp[i][w] + ab[i][1], dp[i][w])
        else:
            dp2[i+1][w] = max(dp[i][w] + ab[i][1], dp[i][w])

answer =0
for i in range(n+1):
    for w in range(t):
        answer = max(answer, dp2[i][w])
print(answer)

#print(dp2)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict

n, t = map(int, input().split())
knapsack = defaultdict(lambda: 0)
knapsack[0] = 0
tmp_ans = 0
cuisines = [tuple(map(int, line.split())) for line in sys.stdin]
cuisines.sort()
for a, b in cuisines:
    for time, satisfied in list(knapsack.items()):
        if time + a >= t:
            tmp_ans = max(tmp_ans, satisfied + b)
            continue
        knapsack[time + a] = max(knapsack[time + a], satisfied + b)

print(max(tmp_ans, max(knapsack.values())))

Submission status: Time Limit Exceeded

Solution code:
#
import sys
import math
import numpy as np
import itertools
# いくつか入力
n,t = (int(i) for i in input().split())  

# n行の複数列ある数値を二次元配列へ。x[n][?]
ab = [[int(i) for i in input().split()] for i in range(n)]
# 0:weight 1:value

ab = sorted(ab,key=lambda x: x[1])

dp = [[0 for i in range(t)] for j in range(n+1)]
dp2 = [[0 for i in range(t)] for j in range(n+1)]


for i in range(n):
    for w in range(t):
        if w >= ab[i][0] :
            dp[i+1][w] = max(dp[i][w-ab[i][0]] + ab[i][1], dp[i][w])
        else:
            dp[i+1][w] = dp[i][w]
        if w >= ab[i][0] :
            dp2[i+1][w] = max(dp2[i][w-ab[i][0]] + ab[i][1],dp[i][w] + ab[i][1], dp[i][w])
        else:
            dp2[i+1][w] = max(dp[i][w] + ab[i][1], dp[i][w])

answer =0
for i in range(n+1):
    for w in range(t):
        answer = max(answer, dp2[i][w])
print(answer)

#print(dp2)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
import bisect
from heapq import heappush, heappop
from scipy.sparse.csgraph import floyd_warshall

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, T = MAP()
wv = [LIST() for _ in range(N)]
dp1 = [[0]*T for _ in range(N+1)]
dp2 = [[0]*T for _ in range(N+1)]


for i in range(N):
	for j in range(T):
		if j < wv[i][0]:
			dp1[i+1][j] = dp1[i][j]
		else:
			dp1[i+1][j] = max(dp1[i][j], dp1[i][j-wv[i][0]]+wv[i][1])

wv = wv[::-1]

for i in range(N):
	for j in range(T):
		if j < wv[i][0]:
			dp2[i+1][j] = dp2[i][j]
		else:
			dp2[i+1][j] = max(dp2[i][j], dp2[i][j-wv[i][0]]+wv[i][1])

wv = wv[::-1]
ans = 0
for i in range(1, N+1):
	tmp = 0
	for j in range(T):
		tmp = max(tmp, dp1[i-1][j] + dp2[N-i][T-1-j])
	ans = max(ans, tmp+wv[i-1][1])

print(ans)