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: Wrong Answer

Solution code:
def solve():
    from collections import namedtuple
    import sys

    input = sys.stdin.readline
    Dish = namedtuple('Dish', 'idx cost pt')

    n, t = map(int, input().split())
    dishes = []
    for idx in range(1, n + 1):
        cost, pt = map(int, input().split())
        dishes.append(Dish(idx=idx, cost=cost, pt=pt))

    dp = [[0] * 2 for _ in range(t)]
    # dp[完食にかかる総時間][最後の皿を選んだか]:= 美味しさの最大値

    for dish in dishes:
        for cost_ in range(t - 1, -1, -1):
            dp[cost_][True] = max(dp[cost_][True], dp[cost_][False] + dish.pt)
            if cost_ >= dish.cost:
                dp[cost_][False] = max(dp[cost_][False], dp[cost_ - dish.cost][False] + dish.pt)

    return max(dp[cost_][True] for cost_ in range(t))


print(solve())

# 最後の皿を選んだかフラグを設ける
# https://sen-comp.hatenablog.com/entry/2019/11/17/003150

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N,T=map(int,input().split())
D=[tuple(map(int,input().split())) for i in range(N)]

D.sort(key=lambda x:x[1],reverse=True)

DP=[[0,0] for i in range(T+1)]

for ind,(x,y) in enumerate(D):
    for time in range(T,x-1,-1):
        if DP[time][0]<DP[time-x][0]+y:

            if DP[time-x][1]==ind:
                DP[time]=[DP[time-x][0]+y,ind+1]
            else:
                DP[time]=[DP[time-x][0]+y,DP[time-x][1]]

        elif DP[time][0]==DP[time-x][0]+y:

            if DP[time-x][1]==ind:
                DP[time][1]=min(ind+1,DP[time][1])
            else:
                DP[time][1]=min(DP[time-x][1],DP[time][1])

    #print(DP)

D.append((0,0))
                
ANS=DP[-1][0]

for x,y in DP[:-1]:
    #if x==0:
    #    continue
    ANS=max(ANS,x+D[y][1])

print(ANS)

Submission status: Wrong Answer

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[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: Wrong Answer

Solution code:
import sys
n, t = [int(i) for i in sys.stdin.readline().split()]
ls = []
for k in range(n):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    ls.append((a, b))
memo_ls = [[0 for i in range(t)] for j in range(n)]
another_memo_ls = [[-1 for i in range(t)] for j in range(n)]
a_ls, b_ls = zip(*ls)

for j in range(n):
    a, b = ls[j]
    for i in range(t):
        if j == 0:
            if a <= i:
                memo_ls[j][i] = b
                another_memo_ls[j][i] = j
            continue
        if a <= i:
            if memo_ls[j-1][i-a] + b > memo_ls[j-1][i]:
                memo_ls[j][i] = memo_ls[j-1][i-a] + b
                another_memo_ls[j][i] = j
            else:
                memo_ls[j][i] = memo_ls[j-1][i]
                another_memo_ls[j][i] = -1
        else:
            memo_ls[j][i] = memo_ls[j - 1][i]
            another_memo_ls[j][i] = -1
cur = another_memo_ls[-1][-1]
path = set()
j = n-1
i = t-1
while i >= 0 and j >= 0:
    if cur == -1:
        j -= 1
        cur = another_memo_ls[j][i]
    else:
        path.add(j)
        i -= a_ls[cur]
        j -= 1
        cur = another_memo_ls[j][i]
print(memo_ls[-1][-1] + max([b for i, (a, b) in enumerate(ls) if i not in path] + [0]))

Submission status: Wrong Answer

Solution code:
N,T = input().split()
N,T = int(N),int(T)
import numpy as np


A,B = [],[]
for i in range(N):
    a,b = input().split()
    a,b = int(a),int(b)
    A.append(a)
    B.append(b)
    
ind = np.argsort(np.array(B))

data = np.zeros((N-1,T-1))

for i in range(N-1):
    for t in range(T-1):
        if(i==0):
            num = ind[0]
            if(t>=A[num]):
                data[i][t] = B[num]
        else:
            if(t<A[num]):
                num = ind[i]
                data[i][t] = data[i-1][t]
            else:
                num = ind[i]
                p1 = data[i-1][t]
                p2 = data[i-1][t-A[num]]+B[num]

                max_p = max([p1,p2])

                data[i][t] = max_p
            
print(int(data[N-2][T-2])+B[ind[N-1]])

Submission status: Wrong Answer

Solution code:
'''
やっと判例発見した．
3 3
1 1
100 10
1 100
'''
n,t = map(int,input().split())
now = [0 for _ in range(t+1)]
for _ in range(n):
  w,v = map(int,input().split())
  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: Wrong Answer

Solution code:
'''
やっと判例発見した．
3 3
1 1
100 10
1 100
'''
n,t = map(int,input().split())
now = [0 for _ in range(t+1)]
for _ in range(n):
  w,v = map(int,input().split())
  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: Wrong Answer

Solution code:
def calc(N, T, AB_list):
    AB_list.sort(key=lambda ab: ab[1])
    dp = [0 if i < AB_list[0][0] else AB_list[0][1] for i in range(T)]
    for (A, B) in AB_list[1:N - 1]:
        new_dp = [max(dp[i], 0 if i < A else dp[i - A] + B) for i in range(T)]
        dp = new_dp
    return dp[T - 1] + AB_list[N - 1][1]


(N, T) = tuple([int(s) for s in input().split()])
AB_list = [tuple([int(s) for s in input().split()]) for _ in range(N)]
print(calc(N, T, AB_list))

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
now = [0 for _ in range(t+1)]
for _ in range(n):
  w,v = map(int,input().split())
  for f in range(t)[::-1]:
    if f + w < t:
      now[f+w] = max(now[f]+v,now[f+w])
    else:
      now[t] = max(now[f]+v,now[t])
  #print(len(now),now)
print(now[t])

Submission status: Wrong Answer

Solution code:
def calc(N, T, AB_list):
    AB_list.sort(key=lambda ab: ab[1])
    dp = [0 if i < AB_list[0][0] else AB_list[0][1] for i in range(T)]
    for (A, B) in AB_list[1:N - 1]:
        new_dp = []
        for i in range(T):
            if i - A < 0:
                new_dp += [dp[i]]
            else:
                new_dp += [max(dp[i], dp[i - A] + B)]
        dp = new_dp
    return dp[T - 1] + AB_list[N - 1][1]


(N, T) = tuple([int(s) for s in input().split()])
AB_list = [tuple([int(s) for s in input().split()]) for _ in range(N)]
print(calc(N, T, AB_list))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,t=map(int,input().split())
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append((a,b))
dp=[[0]*t for i in range(n+1)]
check=[[0]*t for i in range(n+1)]
for i in range(n):
    for j in range(t):
        if j>=l[i][0]:
            if dp[i+1][j]<dp[i][j]:
                dp[i+1][j]=dp[i][j]
            if dp[i+1][j-l[i][0]]<dp[i][j]+l[i][1]:
                dp[i+1][j-l[i][0]]=dp[i][j]+l[i][1]
                check[i+1][j-l[i][0]]=j
        else:
            if dp[i+1][j]<dp[i][j]:
                dp[i+1][j]=dp[i][j]
M=0
for j in range(t):
    M=max(M,dp[n][j])
m0=0
for j in range(t):
    if dp[n][j]==M:
        x=j
        g=[]
        for i in range(n,0,-1):
            if check[i][x]:
                x=check[i][x]
            else:
                g.append(i-1)
        m=0
        for i in g:
            m=max(m,l[i][1])
        m0=max(m0,m)
print(M+m0)

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy
import itertools
input = sys.stdin.readline

N, T = map(int, input().split())
dish = []
for _ in range(N):
	A, B = map(int, input().split())
	dish.append((A, B))
dish.sort() # sort by time

"""
作戦：最適解が求まっている時、それを食べる順序を（ラストオーダー以外は）変更しても問題なし。
そこで注文順は（ラストオーダー以外）食べる時間が昇順であるとして良い。
具体的には、ラストオーダーkを決める ⇒ 0,...,k-1の中で満足度を最大にする
ここまで落とし込めばあとはナップサック問題。
"""

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

dp = [[0] * T for _ in range(N)]
a_0, b_0 = dish[0]
if a_0 < T:
	for j in range(a_0, T):
		dp[0][j] = b_0

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

#print(dp)


ans = b_0
for k in range(1, N):
	ans = max(ans, dp[k-1][T-1] + dish[k][1])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy
import itertools
input = sys.stdin.readline

N, T = map(int, input().split())
dish = []
for _ in range(N):
	A, B = map(int, input().split())
	dish.append((A, B))
dish.sort() # sort by time

"""
作戦：最適解が求まっている時、それを食べる順序を（ラストオーダー以外は）変更しても問題なし。
そこで注文順は（ラストオーダー以外）食べる時間が昇順であるとして良い。
具体的には、ラストオーダーkを決める ⇒ 0,...,k-1の中で満足度を最大にする
ここまで落とし込めばあとはナップサック問題。
"""

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

dp = [[0] * T for _ in range(N)]
a_0, b_0 = dish[0]
if a_0 < T:
	for j in range(a_0, T):
		dp[0][j] = b_0

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

#print(dp)


ans = 0
for k in range(1, N):
	ans = max(ans, dp[k-1][T-1] + dish[k][1])

print(ans)

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
now = [0 for _ in range(t+1)]
for _ in range(n):
  w,v = map(int,input().split())
  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[t-1]+v,last[t]))
  #print(now)
print(now[t])

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
now = [0 for _ in range(t+1)]
for _ in range(n):
  w,v = map(int,input().split())
  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[t-1]+v,last[t]))
  print(now)
print(now[t])