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 sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())

def knapsack(N, W, weight, value):
	# 初期化
	inf = float("inf")
	dp = [[-inf for i in range(W + 1)] for j in range(N + 1)]
	for i in range(W + 1): dp[0][i] = 0
	
	# DP
	for i in range(N):
		for w in range(W + 1):
			if weight[i] <= w:  # dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
				dp[i + 1][w] = max(dp[i][w - weight[i]] + value[i], dp[i][w])
			else:  # 入る可能性はない
				dp[i + 1][w] = dp[i][w]
	return dp


def main():
	n, t = map(int, input().split())
	AB = []
	A = []
	B = []
	for _ in range(n):
		a, b = map(int, input().split())
		AB.append((a, b))
	AB.sort()
	for a,b in AB:
		A.append(a)
		B.append(b)
	dp= knapsack(n, t, A, B)
	
	ans=0
	for i in range(n):
		ans=max(ans,dp[i][t-1] + AB[i][1])
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
def knapsack_DP(N, W, weight, value):
    dp = [0 for _ in range(W + 1)] 
    ans=0
    for i in range(N):
        ans=max(ans, max(dp)+value[i])
        dp_tmp = [] 
        for w in range(W + 1):
            if (w >= weight[i]): 
                dp_next = max(dp[w-weight[i]] + value[i], dp[w]) 
            else:
                dp_next = dp[w] 
            dp_tmp.append(dp_next)
        dp = dp_tmp 
    return ans

if __name__=="__main__":
    N, W = map(int, input().split())
    weight = []
    value = []
    for i in range(N):
        w, v= map(int, input().split())
        weight.append(w)
        value.append(v)
    wv=((weight[i],value[i]) for i in range(N))
    wv=sorted(wv, key=lambda x:x[0])
    weight=[wv[i][0] for i in range(N)]
    value=[wv[i][1] for i in range(N)]
    print(knapsack_DP(N, W-1, weight, value))

Submission status: Time Limit Exceeded

Solution code:
N,T=list(map(int,input().split()))
AB=[list(map(int,input().split())) for i in range(N)]
AB.sort()
A=[AB[i][0] for i in range(N)]
B=[AB[i][1] for i in range(N)]

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

for i in range(N):
    popA=A[i]
    popB=B[i]
    if i == 0:
        popAl=A[1:]
        popBl=B[1:]
    if i == N-1:
        popAl=A[:-1]
        popBl=B[:-1]
    else:
        popAl=A[:i]+A[i+1:]
        popBl=B[:i]+B[i+1:]
    
    for j in range(N-1):
        for k in range(T):
            if popAl[j] > k:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k],dp[j][k-popAl[j]]+popBl[j])
    ansl[i]=dp[-1][-1]+popB
print(max(ansl))

Submission status: Time Limit Exceeded

Solution code:
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
import numpy as np
 
dp1 = np.array([[0]*T for _ in range(N+1)])
rev_AB = AB[::-1]
dp2 = np.array([[0]*T for _ in range(N+1)])


for i,(ab,rev_ab) in enumerate(zip(AB,rev_AB),1):
  a = ab[0]
  rev_a = rev_ab[0]
  b = ab[1]
  rev_b = rev_ab[1]
  
  dp1[i,:a] = dp1[i-1,:a]
  dp2[i,:rev_a] = dp2[i-1,:rev_a]
  dp1[i,a:] = np.fmax(dp1[i-1,a:],dp1[i-1,:-a]+b)
  dp2[i,rev_a:] = np.fmax(dp2[i-1,rev_a:],dp2[i-1,:-rev_a]+rev_b)

      
lis = [0]*N
for i in range(1,N+1):
  l_1 = dp1[i-1]
  l_2 = dp2[N-i]
  b = AB[i-1][1]
  for t in range(T):
    lis[i-1] = max(l_1[t]+l_2[T-1-t]+b,lis[i-1])
 
    
print(max(lis))

Submission status: Time Limit Exceeded

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

dp1 = [[0]*T for _ in range(N+1)]
rev_AB = AB[::-1]
dp2 = [[0]*T for _ in range(N+1)]
for i,(ab,rev_ab) in enumerate(zip(AB,rev_AB),1):
  a = ab[0]
  rev_a = rev_ab[0]
  b = ab[1]
  rev_b = rev_ab[1]
  l_1 = dp1[i-1]
  l_2 = dp2[i-1]
  for t in range(1,T):
    if a>t:
      dp1[i][t]=l_1[t]
      
    else:
      dp1[i][t] = max(l_1[t],l_1[t-a]+b)
      
    if rev_a>t:
      dp2[i][t]=l_2[t]
    
    else:
      dp2[i][t] = max(l_2[t],l_2[t-rev_a]+rev_b)
      
      
    

      
lis = [0]*N
for i in range(1,N+1):
  l_1 = dp1[i-1]
  l_2 = dp2[N-i]
  b = AB[i-1][1]
  for t in range(T):
    lis[i-1] = max(l_1[t]+l_2[T-1-t]+b,lis[i-1])

    
print(max(lis))

Submission status: Time Limit Exceeded

Solution code:
n,t=map(int,input().split())
ab=[]
for i in range(n):
    ab.append(list(map(int,input().split())))
ab_r=list(reversed(ab))

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

for i in range(n):
    for j in range(t-1):
        if ab[i][0] <= j:
            dp1[i+1][j] = max(dp1[i][j],dp1[i][j-ab[i][0]]+ab[i][1])
        else:
            dp1[i+1][j] = dp1[i][j]
            
for i in range(n):
    for j in range(t-1):
        if ab_r[i][0] <= j:
            dp2[i+1][j] = max(dp2[i][j],dp2[i][j-ab_r[i][0]]+ab_r[i][1])
        else:
            dp2[i+1][j] = dp2[i][j]
            
m=0
for i in range(1,n+1):
    for j in range(t-1):
        m=max(m,dp1[i-1][j]+dp2[n-i][t-1-j]+ab[i-1][1])
print(m)

Submission status: Time Limit Exceeded

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

dp1 = [[0]*T for _ in range(N+1)]
for i,ab in enumerate(AB,1):
  for t in range(1,T):
    a = ab[0]
    if a>t:
      dp1[i][t]=dp1[i-1][t]
      
    else:
      dp1[i][t] = max(dp1[i-1][t],dp1[i-1][t-a]+ab[1])

rev_AB = AB[::-1]
dp2 = [[0]*T for _ in range(N+1)]
for i,ab in enumerate(rev_AB,1):
  for t in range(1,T):
    a = ab[0]
    if a>t:
      dp2[i][t]=dp2[i-1][t]
      
    else:
      dp2[i][t] = max(dp2[i-1][t],dp2[i-1][t-a]+ab[1])
      
lis = [0]*N
for i in range(1,N+1):
  for t in range(T):
    lis[i-1] = max(dp1[i-1][t]+dp2[N-i][T-1-t]+AB[i-1][1],lis[i-1])

    
print(max(lis))

Submission status: Time Limit Exceeded

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

dp1 = [[0]*T for _ in range(N+1)]
for i,ab in enumerate(AB,1):
  for t in range(1,T):
    a = ab[0]
    if a>t:
      dp1[i][t]=dp1[i-1][t]
      
    else:
      dp1[i][t] = max(dp1[i-1][t],dp1[i-1][t-a]+ab[1])

rev_AB = AB[::-1]
dp2 = [[0]*T for _ in range(N+1)]
for i,ab in enumerate(rev_AB,1):
  for t in range(1,T):
    a = ab[0]
    if a>t:
      dp2[i][t]=dp2[i-1][t]
      
    else:
      dp2[i][t] = max(dp2[i-1][t],dp2[i-1][t-a]+ab[1])
      
lis = [0]*N
for i in range(1,N+1):
  for t in range(T):
    lis[i-1] = max(dp1[i-1][t]+dp2[N-i][T-1-t]+AB[i-1][1],lis[i-1])

    
print(max(lis))

Submission status: Time Limit Exceeded

Solution code:
def knapsack_DP(N, W, weight, value):
    dp = [0 for _ in range(W + 1)] 

    for i in range(N):
        dp_tmp = [] 
        for w in range(W + 1):
            if (w >= weight[i]): 
                dp_next = max(dp[w-weight[i]] + value[i], dp[w]) 
            else:
                dp_next = dp[w] 
            dp_tmp.append(dp_next)
        dp = dp_tmp 
    return dp[-1]

if __name__=="__main__":
    N, W = map(int, input().split())
    weight = []
    value = []
    for i in range(N):
        w, v= map(int, input().split())
        weight.append(w)
        value.append(v)
    wv=((weight[i],value[i]) for i in range(N))
    wv=sorted(wv)
    weight=[wv[i][0] for i in range(N)]
    value=[wv[i][1] for i in range(N)]
    ans=0
    for i in range(1,N):
        if i==0:
          wtmp=weight[1:]
          vtmp=value[1:]
        elif i==N-1:
          wtmp=weight[:N-1]
          vtmp=value[:N-1]
        else:
          wtmp=weight[:i]+weight[i+1:]
          vtmp=value[:i]+value[i+1:]
        ans=max(knapsack_DP(N-1, W-1, wtmp, vtmp)+value[i], ans)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,t = map(int,input().split())
now = [0 for _ in range(t+1)]
wv = []
for _ in range(n):
  wv.append(list(map(int,input().split())))
wv.sort(key=lambda x:x[0])
for i in range(n):
  w,v = wv[i]
  last = now
  now = []
  for f in range(t):
    if f - w >= 0:
      now.append(max(last[f],last[f-w]+v))
    else:
      now.append(last[f])
  now.append(max(last[-2]+v,last[-1]))
  #print(now)
print(now[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n,t = map(int,input().split())
data = [list(map(int,input().split())) for _ in range(n)]

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

q = 0
for i in range(1,n+1):
    q = max(q,data[i-1][0])
    dp2[1][i] = q

for i in range(1,t+1):
    for j in range(1,n+1):
        if i-data[j-1][0] >= 0:
            dp1[i][j] = max(dp1[i][j-1],dp1[i-data[j-1][0]][j-1]+data[j-1][1])
            dp2[i][j] = max(dp2[i][j-1],dp2[i-data[j-1][0]][j-1]+data[j-1][1],dp1[i-1][j-1]+data[j-1][1])
        else:
            dp1[i][j] = dp1[i][j-1]
            dp2[i][j] = max(dp2[i][j-1],dp1[i-1][j-1]+data[j-1][1])


print(dp2[t][n])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N,T = map(int,input().split())
data = [list(map(int,input().split())) for _ in range(N)]

dp1 = [[0]*(N+1) for t in range(T+1)] #dp1[t][n] := (data[:n]の中からtまでに食べ切る範囲で美味しく）
dp2 = [[0]*(N+1) for t in range(T+1)] #dp1[t][n] := (data[:n]の中からt-1までに注文する範囲で美味しく)


for t in range(1,T+1):
    for n in range(1,N+1):
        if t-data[n-1][0] >= 0:
            dp1[t][n] = max(dp1[t][n-1],dp1[t-data[n-1][0]][n-1]+data[n-1][1])
            dp2[t][n] = max(dp2[t][n-1],dp2[t-data[n-1][0]][n-1]+data[n-1][1],dp1[t-1][n-1]+data[n-1][1])
        else:
            dp1[t][n] = dp1[t][n-1]
            dp2[t][n] = max(dp2[t][n-1],dp1[t-1][n-1]+data[n-1][1])


print(dp2[T][N])

Submission status: Time Limit Exceeded

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

dp1 = [[0 for n in range(N+1)] for t in range(T+1)] #dp1[t][n] := (data[:n]の中からtまでに食べ切る範囲で美味しく）
dp2 = [[0 for n in range(N+1)] for t in range(T+1)] #dp1[t][n] := (data[:n]の中からt-1までに注文する範囲で美味しく)


for t in range(1,T+1):
    for n in range(1,N+1):
        if t-data[n-1][0] >= 0:
            dp1[t][n] = max(dp1[t][n-1],dp1[t-data[n-1][0]][n-1]+data[n-1][1])
            dp2[t][n] = max(dp2[t][n-1],dp2[t-data[n-1][0]][n-1]+data[n-1][1],dp1[t-1][n-1]+data[n-1][1])
        else:
            dp1[t][n] = dp1[t][n-1]
            dp2[t][n] = max(dp2[t][n-1],dp1[t-1][n-1]+data[n-1][1])


print(dp2[T][N])

Submission status: Time Limit Exceeded

Solution code:
n,t = map(int,input().split())
ab = [[int(i) for i in input().split()] for j in range(n)]

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

ans = 0

for h in range(n):
    dp = [[0 for i in range(t)] for j in range(n+1)]
    for i in range(n):
        if h == 0 and i == 0:
            continue
        if h == i:
            dp[i+1] = dp[i]
            continue
        for j in range(t):
            #print(h,i)
            dp[i+1][j] = dp[i][j]
            #print(j,ab[i][0])
            if j < ab[i][0]:
                #print(j)
                continue
            #if dp[i][j] < dp[i][j-ab[i][0]] + ab[i][1]:
            #    dp[i+1][j] = dp[i][j-ab[i][0]] + ab[i][1]
            dp[i+1][j] = max(dp[i][j],dp[i][j-ab[i][0]] + ab[i][1])
            #print(dp)
    #print(h,dp)
    ans = max(ans,dp[n][t-1]+ab[h][1])


print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

N, T = [int(i) for i in input().split()]

data = []
for i in range(N):
    data.append([int(i) for i in input().split()])

data.sort(key=itemgetter(0))

dp = [dict() for _ in range(N)]
for i in range(T):
    dp[0][i] = 0

dp[0][T] = data[0][1]
for i in range(1, N):
    time = data[i-1][0]
    value = data[i-1][1]
    add_value = data[i][1]
    for j in range(T):
        if j >= time:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-time] + value)
        else:
            dp[i][j] = dp[i-1][j]
    dp[i][T] = max(dp[i][T-1] + add_value, dp[i-1][T])
print(dp[-1][T])

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
import numpy as np

N, T = [int(i) for i in input().split()]

data = []
for i in range(N):
    data.append([int(i) for i in input().split()])

data.sort(key=itemgetter(0))

dp = [dict() for _ in range(N)]
for i in range(T):
    dp[0][i] = 0

dp[0][T] = data[0][1]
for i in range(1, N):
    time = data[i-1][0]
    value = data[i-1][1]
    add_value = data[i][1]
    for j in range(T):
        if j >= time:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-time] + value)
        else:
            dp[i][j] = dp[i-1][j]
    dp[i][T] = max(dp[i][T-1] + add_value, dp[i-1][T])
print(dp[-1][T])

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
import numpy as np

N, T = [int(i) for i in input().split()]

data = []
for i in range(N):
    data.append([int(i) for i in input().split()])

data.sort(key=itemgetter(0))

#dp = [[0] * (T+1) for i in range(N)]
dp = np.zeros((N, T+1), int)

dp[0][T] = data[0][1]
for i in range(1, N):
    time = data[i-1][0]
    value = data[i-1][1]
    add_value = data[i][1]
    for j in range(T):
        if j >= time:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-time] + value)
        else:
            dp[i][j] = dp[i-1][j]
    dp[i][T] = dp[i][T-1] + add_value

ans = np.max(np.array([row[-1] for row in dp[:]]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
import numpy as np

N, T = [int(i) for i in input().split()]

data = []
for i in range(N):
    data.append([int(i) for i in input().split()])

data.sort(key=itemgetter(0))

#dp = [[0] * (T+1) for i in range(N)]
dp = np.zeros((N, T+1), int)

dp[0][T] = data[0][1]
for i in range(1, N):
    time = data[i-1][0]
    value = data[i-1][1]
    add_value = data[i][1]
    for j in range(T):
        if j >= time:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-time] + value)
        else:
            dp[i][j] = dp[i-1][j]
    dp[i][T] = dp[i][T-1] + add_value

ans = np.max([row[-1] for row in dp[:]])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

N, T = [int(i) for i in input().split()]

data = []
for i in range(N):
    data.append([int(i) for i in input().split()])

data.sort(key=itemgetter(0))

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

dp[0][T] = data[0][1]
for i in range(1, N):
    time = data[i-1][0]
    value = data[i-1][1]
    add_value = data[i][1]
    for j in range(T):
        if j >= time:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-time] + value)
        else:
            dp[i][j] = dp[i-1][j]
    dp[i][T] = dp[i][T-1] + add_value

ans = max([row[-1] for row in dp[:]])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

N, T = [int(i) for i in input().split()]

data = []
for i in range(N):
    data.append([int(i) for i in input().split()])

data.sort(key=itemgetter(0))

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

dp[0][T] = data[0][1]
for i in range(1, N):
    time = data[i-1][0]
    value = data[i-1][1]
    add_value = data[i][1]
    for j in range(T):
        if j >= time:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-time] + value)
        else:
            dp[i][j] = dp[i-1][j]
    dp[i][T] = max(dp[i][T-1] + add_value, dp[i-1][T])

print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
N, T = 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)]

ans = AB[N - 1][1]
for i in range(N - 1, 0, -1):
    for j in range(T + 1):
        if j < AB[i][0]:
            dp[i][j] = dp[i + 1][j]
        else:
            dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - AB[i][0]] + AB[i][1])
    tmp = dp[i][T] + AB[i - 1][1]
    ans = max(ans, tmp)
    
print (ans)
# print (AB)

# for i in dp:
#     print (dp)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, T, *AB = map(int, open(0).read().split())

    dp = [0] * 6010
    for w, v in sorted(zip(*[iter(AB)] * 2)):
        for i in reversed(range(T)):
            if dp[i + w] < dp[i] + v:
                dp[i + w] = dp[i] + v

    print(max(dp))

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, T, *AB = map(int, open(0).read().split())

    dp = [0] * (T + max(AB[::2]))
    for w, v in sorted(zip(*[iter(AB)] * 2)):
        for i in reversed(range(T)):
            if dp[i + w] < dp[i] + v:
                dp[i + w] = dp[i] + v

    print(max(dp))

main()

Submission status: Time Limit Exceeded

Solution code:
N, T, *AB = map(int, open(0).read().split())

dp = [0] * (T + max(AB[::2]))
for w, v in sorted(zip(*[iter(AB)] * 2)):
    for i in reversed(range(T)):
        dp[i + w] = max(dp[i + w], dp[i] + v)

print(max(dp))