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: Accepted

Solution code:
N, T = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort()
DP = [[0] * (T + 1) for _ in range(N + 1)]
for n in range(1, N + 1):
    a, b = AB[n - 1]
    for t in range(1, T + 1):
        if t - a >= 0:
            DP[n][t] = max(DP[n - 1][t - a] + b, DP[n - 1][t])
        else:
            DP[n][t] = DP[n - 1][t]

T = [DP[n][T - 1] + AB[n][1] for n in range(N)] + [DP[N][T]]
print(max(T))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np
 
N,T = map(int,readline().split())
m = map(int,read().split())
AB = sorted(zip(m,m))
 
can_add = [0] * N
for n in range(N-1,0,-1):
    x = can_add[n]
    y = AB[n][1]
    can_add[n-1] = x if x > y else y
dp = np.zeros(T,np.int64)
 
answer = 0
for i,(a,b) in enumerate(AB):
    np.maximum(dp[a:], dp[:-a] + b, out = dp[a:])
    x = dp.max() + can_add[i]
    if answer < x:
        answer = x
print(answer)

Submission status: Accepted

Solution code:
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, T = map(int, readline().split())

AB= list(map(int, read().split()))
A = AB[0::2]
B = AB[1::2]
ls = sorted(zip(A, B))
#ls = list(zip(A, B))
# 食べるのにかかる時間で昇順にソート
#ls.sort()
# 上の並び順において、ls[i:]の美味しさの最大値をdeli[i]とする
deli = [0]*N
deli[- 1] = ls[-1][- 1]
for i in range(N - 2, 0, - 1):
    deli[i] = max(deli[i + 1], ls[i][- 1])

res = 0
dp = np.zeros(T, np.int64)
for i in range(N - 1):
    dp[ls[i][0]:] = np.maximum(dp[ls[i][0]:], dp[:-ls[i][0]] + ls[i][1])
    res = max(res, dp[- 1] + deli[i + 1])
print(res)

Submission status: Accepted

Solution code:
import numpy as np
def main():
    n, t = map(int, input().split())
    dt = [list(map(int, input().split())) for _ in range(n)]
    dt.sort()
    dp = np.zeros(t, dtype=int)
    ans = 0
    for a, b in dt:
        ans = max(ans, dp[-1]+b)
        np.maximum(dp[a:], dp[:-a]+b, out=dp[a:])
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

n, t = map(int, input().split())
dt = [list(map(int, input().split())) for _ in range(n)]
dt.sort()
dp = np.zeros(t, dtype=int)
ans = 0
for a, b in dt:
    ans = max(ans, dp[-1]+b)
    np.maximum(dp[a:], dp[:-a]+b, out=dp[a:])
print(ans)

Submission status: Accepted

Solution code:
import sys

import numpy as np

n, t = map(int, input().split())
knapsack = np.zeros(t, dtype=np.int64)
cuisines = [tuple(map(int, line.split())) for line in sys.stdin]
cuisines.sort()
tmp_ans = 0
for a, b in cuisines:
    tmp_ans = max(tmp_ans, knapsack.max() + b)
    knapsack[a:] = np.maximum(knapsack[a:], knapsack[:-a] + b)

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

Submission status: Accepted

Solution code:
import numpy as np
N,T = map(int, raw_input().split())
L = [map(int, raw_input().split()) for i in range(N)]
d=np.zeros(10000,int)
for a,b in sorted(L):
    t=d[a:T+a]
    np.maximum(t,d[:T]+b,t)
print(max(d))

Submission status: Accepted

Solution code:
N, T = map(int,input().split())
ab_list = []
for _ in range(N):
    a, b = map(int,input().split())
    ab_list.append((a,b))

ab_list.sort(key=lambda x: x[0])
    
dp = [[0 for _ in range(T+1+3000)] for _ in range(N+1)]

for i in range(N):
    a, b = ab_list[i]
    for j in range(T+1):
        if j-a >= 0:
            dp[i+1][j] = max(dp[i][j-a] + b, dp[i][j])
        else:
            dp[i+1][j] = dp[i][j]
    for k in range(T+1,T+1+3000):
        # T-1分までなら最後の一品注文可能
        if k-a >= 0 and k-a <= T-1:
            dp[i+1][k] = max(dp[i][k-a] + b, dp[i][k])
        else: 
            dp[i+1][k] = dp[i][k]
            
print(max(dp[-1]))

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
items = [tuple(map(int, input().split())) for _ in range(n)]
items.sort()
ma = items[-1][0]
dp = [0] * (t+ma)

for a, b in items:
    for i in range(t+ma-1, a-1, -1):
        if i-a < t:
            dp[i] = max(dp[i], dp[i-a] + b)

print(max(dp))

Submission status: Accepted

Solution code:
import numpy as np


def solve():


    N, T = map(int, input().split())

    A = []
    B = []

    for _ in range(N):
        A_temp, B_temp = map(int, input().split())
        A.append(A_temp)
        B.append(B_temp)

    A = np.array(A, np.int)
    B = np.array(B, np.int)

    idx = np.argsort(A)
    A = A[idx]
    B = B[idx]

    dp = np.zeros((N, T), np.int)

    for i in range(N - 1):
        dp[i + 1, :A[i]] = dp[i, :A[i]]
        dp[i + 1, A[i]:] = np.maximum(dp[i, A[i]:], dp[i, :-A[i]] + B[i])

    print(np.max(dp[:, -1] + B))


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

#import sys
#input = sys.stdin.readline
#val = list(input()) #abc
#val=np.array(val)
#index=np.argsort(val[:,0])
#A=val[index,0].tolist()
#B=val[index,1].tolist()
val.sort()
A=[v[0] for v in val]
B=[v[1] for v in val]

#temp=np.zeros((T+1,N))
#Ts=T*np.ones(N-1)
#jun=np.arange(1,N)
#for itemp in range(T):
#    i=itemp
#    if i>=A[0]:
#        temp[i,0]=B[0]
#    if i>0:
#        temp[i,:]=np.amax([temp[i,:],temp[i-1,:]],0)
#    for jtemp in range(N-1):
#        j=jtemp+1
#        temp[i,j]=max(temp[i,j],temp[i,j-1])
#    jnext=np.amin([Ts,i+A[1:]],0)
#    jnext=[int(s) for s in jnext]
#    temp[jnext,jun]=np.amax([temp[i,:-1]+B[1:],temp[jnext,jun]],0)

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

for itemp in range(T):
    i=itemp
    if i>=A[0]:
        temp[i][0]=B[0]
    for jtemp in range(N-1):
        j=jtemp+1
        jbfo=temp[i][j-1]
        ijnow=temp[i][j]
        
        if i>0:
            temp[i][j]=max(ijnow,temp[i-1][j])
            
        temp[i][j]=max(ijnow,jbfo)
        if A[j]<T-i:
            temp[i+A[j]][j]=max(jbfo+B[j],temp[i+A[j]][j])
        else:
            temp[T][j]=max(jbfo+B[j],temp[T][j])
print(int(max(temp[-1][:])))

Submission status: Accepted

Solution code:
def get():
    return map(int,input().split())

N,W=get()
x=[[0,0] for _ in range(N)]
for i in range(N):
    x[i][0],x[i][1]=get()
x.sort()
w=[0]*N
v=[0]*N
for i in range(N):
    w[i], v[i] = x[i]

if W>=sum(w):
    print(sum(v))
else:
    dp=[0]*(W+1)
    for i in range(N):
        for j in range(W+w[i]-1,w[i]-1,-1):
            if j >= W:
                dp[W]=max(dp[W],dp[j-w[i]]+v[i])
            else:
                dp[j]=max(dp[j],dp[j-w[i]]+v[i])
    print(dp[W])

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
w = []
v = []
for i in range(n):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w, v = zip(*sorted(zip(w, v)))
dp = [[0] * (t + 1) for i in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
    dp[i + 1][t] = dp[i + 1][t] = max(dp[i][t - 1] + v[i], dp[i][t])
print(dp[n][t])

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter as ITEM
input = sys.stdin.readline

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

vA.sort(key=ITEM(0))
#print(vA)

# dp[i..N][t..T+1] := max(sum(B))
# dp[i][min(t+A,T+1)] = max(dp[i-1][min(t+A,T+1)], dp[i-1][t]+B)
# if t<=T-1: you can eat
# dp[0][0] = 0

dp = [[0,]*(T+2)]
dp_apnd = dp.append

for A,B in vA:
  pDP = dp[-1]
  #nDP = [0,]*(T+2)
  nDP = dp[-1][:]
  #print(id(pDP),id(nDP))###
  for t in range(T):
    nDP[min(t+A,T+1)] = max(pDP[min(t+A,T+1)], pDP[t]+B)
  dp_apnd(nDP)
  #print(dp[-1][-4:])###

print(max(dp[-1]))

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
ab=[list(map(int,input().split()))for _ in range(n)]
ab.sort()
dp=[6007*[0]for _ in range(n)]
for i in range(n):
  a,b=ab[i]
  for j in range(6007):
    if i==0:
      if j>=a and j<t:dp[i][j]=b
    elif j>=a and j-a<t:dp[i][j]=max(dp[i-1][j-a]+b,dp[i-1][j])
    else:dp[i][j]=dp[i-1][j]
print(max(dp[-1]))

Submission status: Accepted

Solution code:
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, t = list(map(int, input().rstrip('\n').split()))
    ab = [list(map(int, input().rstrip('\n').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)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import numpy as np

N, T = map(int, input().split())

A = []
B = []

for _ in range(N):
    A_temp, B_temp = map(int,input().split())
    A.append(A_temp)
    B.append(B_temp)

A = np.array(A, np.int)
B = np.array(B, np.int)

idx = np.argsort(A)
A = A[idx]
B = B[idx]

dp = np.zeros((N, T), np.int)

for i in range(N - 1):
    dp[i+1, :A[i]] = dp[i, :A[i]]
    dp[i+1, A[i]:] = np.maximum(dp[i, A[i]:], dp[i, :-A[i]] + B[i])

print(np.max(dp[:, -1] + B))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,t=map(int,input().split())
dp=[[0 for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
foodmax=[0]*n
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
foodmax[-1]=food[-1][1]
for i in range(n-1)[::-1]:
  foodmax[i]=max(foodmax[i+1],food[i][1])
foodmax+=[0]
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j]=max(dp[i][j],dp[i][j-food[i][0]]+food[i][1])
      
ans=0
for i in range(n):
  if ans<dp[i+1][t-1]+foodmax[i+1]:
    ans=dp[i+1][t-1]+foodmax[i+1]
print(ans)

Submission status: Accepted

Solution code:
#ABC145 E
N,T = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N)]
ab.sort()

dp = [[0]*T for _ in range(N+1)]
# i番目より前まで注文してT分以内(max T-1)で完食する
def chmax(a,b):
    if a < b:
        return b
    else:
        return a 
    
for i in range(N):
    for t in range(T): #T-1分がmax
        if t+ab[i][0] <= T-1:
        	dp[i+1][t+ab[i][0]] =chmax(dp[i+1][t+ab[i][0]],dp[i][t]+ab[i][1])
        
        dp[i+1][t] = chmax(dp[i+1][t],dp[i][t])

#一番遅いものをラスト(T-1分)に注文
#i番目をラストに注文 = dp[i][t-1] + b[i]
ans = 0
for i in range(N):
    ans = chmax(ans,dp[i][T-1]+ab[i][1])
    
print(ans)

Submission status: Accepted

Solution code:
#ABC145 E
n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort()

a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
    
dp = [[0 for j in range(t)] for _ in range(n+1)]
# i番目より前まで注文してT分以内(max T-1)で完食する
    
for i in range(n):
    for j in range(t): 
        if j+ a[i] <= t-1:
            if dp[i+1][j + a[i]] < dp[i][j] + b[i]:
                dp[i+1][j + a[i]] = dp[i][j] + b[i]
        if dp[i+1][j] < dp[i][j]:
            dp[i+1][j] = dp[i][j]

#一番遅いものをラスト(T-1分)に注文
#i番目をラストに注文 = dp[i][t-1] + b[i]
ans = 0
for i in range(n):
    ans = max(ans,dp[i][t-1]+b[i])
    
print(ans)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
 
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
 
a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
 
dp = [[0 for _ in range(t)] for _ in range(n+1)]
 
for i in range(n):
    for j in range(t):
        if j + a[i] <= t-1:
            if dp[i+1][j + a[i]] < dp[i][j] + b[i]:
                dp[i+1][j + a[i]] = dp[i][j] + b[i]
        if dp[i+1][j] < dp[i][j]:
            dp[i+1][j] = dp[i][j]
 
ans = 0
for i in range(n):
    ans = max(ans,dp[i][t-1]+b[i])
    
print(ans)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
 
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
 
a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
 
dp = [[0 for _ in range(t)] for _ in range(n+1)]
 
for i in range(n):
    for j in range(t):
        if j + a[i] <= t-1:
            if dp[i+1][j + a[i]] < dp[i][j] + b[i]:
                dp[i+1][j + a[i]] = dp[i][j] + b[i]
        if dp[i+1][j] < dp[i][j]:
            dp[i+1][j] = dp[i][j]
 
ans = 0
 
for i in range(n):
    ans = max(ans, dp[i][t-1] + b[i])
 
print(ans)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
 
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
 
a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
 
dp = [[0 for j in range(t)] for i in range(n+1)]
 
for i in range(n):
    for j in range(t):
        if j + a[i] <= t-1:
            if dp[i+1][j + a[i]] < dp[i][j] + b[i]:
                dp[i+1][j + a[i]] = dp[i][j] + b[i]
        if dp[i+1][j] < dp[i][j]:
            dp[i+1][j] = dp[i][j]
 
ans = 0
 
for i in range(n):
    ans = max(ans, dp[i][t-1] + b[i])
 
print(ans)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
 
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
 
a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
 
dp = [[0 for j in range(t)] for i in range(n+1)]
 
for i in range(n):
    for j in range(t):
        if j + a[i] <= t-1:
            if dp[i+1][j + a[i]] < dp[i][j] + b[i]:
                dp[i+1][j + a[i]] = dp[i][j] + b[i]
        if dp[i+1][j] < dp[i][j]:
            dp[i+1][j] = dp[i][j]
 
ans = 0
 
for i in range(n):
    ans = max(ans, dp[i][t-1] + b[i])
 
print(ans)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
 
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
 
a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
 
dp = [[0 for j in range(t)] for i in range(n+1)]
 
for i in range(n):
    for j in range(t):
        if j + a[i] <= t-1:
            if dp[i+1][j + a[i]] < dp[i][j] + b[i]:
                dp[i+1][j + a[i]] = dp[i][j] + b[i]
        if dp[i+1][j] < dp[i][j]:
            dp[i+1][j] = dp[i][j]
 
ans = 0
 
for i in range(n):
    ans = max(ans, dp[i][t-1] + b[i])
 
print(ans)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
 
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
 
a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
 
dp = [[0 for j in range(t+1)] for i in range(n+1)]
 
for i in range(n):
    for j in range(t):
        if j + a[i] <= t-1:
            if dp[i+1][j + a[i]] < dp[i][j] + b[i]:
                dp[i+1][j + a[i]] = dp[i][j] + b[i]
        if dp[i+1][j] < dp[i][j]:
            dp[i+1][j] = dp[i][j]
 
ans = 0
 
for i in range(n):
    ans = max(ans, dp[i][t-1] + b[i])
 
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,t=map(int,input().split())
dp=[[0 for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
foodmax=[0]*n
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
foodmax[-1]=food[-1][1]
for i in range(n-1)[::-1]:
  foodmax[i]=max(foodmax[i+1],food[i][1])
foodmax+=[0]
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j]=max(dp[i][j],dp[i][j-food[i][0]]+food[i][1])
ans=0
for i in range(n):
  if ans<dp[i+1][t-1]+foodmax[i+1]:
    ans=dp[i+1][t-1]+foodmax[i+1]
print(ans)

Submission status: Accepted

Solution code:
from numpy import*
(n,t),*l=loadtxt(open(0)).tolist()
d=zeros(1e4,int)
for a,b in sorted(l):p=d[a:a+t];fmax(p,d[:t]+b,p)
print(max(d))

Submission status: Accepted

Solution code:
inf = float('inf')

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

AB = sorted(AB, key=lambda x:x[0])
A = [a for a, b in AB]
B = [b for a, b in AB]

M = T + max(A)
dp = [[-inf] * (M) for _ in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for t in range(M):
        if t < T:
            dp[i+1][t+A[i]] = max(dp[i+1][t+A[i]], dp[i][t] + B[i])
        dp[i+1][t] = max(dp[i+1][t], dp[i][t])

ans = max(dp[N])
print(ans)

Submission status: Accepted

Solution code:
from numpy import*
(N,T),*L=loadtxt(open(0)).tolist()
d=zeros(1e4,int)
for w,v in sorted(L):t=d[w:T+w];maximum(t,d[:T]+v,t)
print(max(d))

Submission status: Accepted

Solution code:
from numpy import*
(N,T),*L=loadtxt(open(0)).tolist()
d=zeros(1e4,int)
for w,v in sorted(L):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Accepted

Solution code:
from numpy import*
(N,T),*L=loadtxt(open(0),int).tolist()
d=zeros(1e4,int)
for w,v in sorted(L):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Accepted

Solution code:
from numpy import*
N,T,*L=int_(open(0).read().split())
d=int_([0]*6010)
for w,v in sorted(zip(*[iter(L)]*2)):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Accepted

Solution code:
from numpy import*
N,T,*L=int_(open(0).read().split())
d=zeros(1e4,int)
for w,v in sorted(zip(*[iter(L)]*2)):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Accepted

Solution code:
from numpy import*
N,T,*L=int_(open(0).read().split())
d=zeros(6010,int)
for w,v in sorted(zip(*[iter(L)]*2)):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Accepted

Solution code:
words = lambda t : list(map(t, input().split()))
n,t = words(int)
xys = []
for i in range(n):
    x,y = words(int)
    xys.append((x,y))

xys.sort()
dp = [[0]*(t+1) for i in range(n)]

def show(dp):
  for row in dp:
      print(row)

for i in range(n):
    for j in range(1,t+1):
        dp[i][j] = dp[i-1][j]
        if xys[i][0] <= j:
            dp[i][j] = max(dp[i][j], xys[i][1] + dp[i-1][j-xys[i][0]])

#show(dp)
ans = xys[0][1]
for i in range(1,n):
    ans = max(ans, dp[i-1][t-1]+xys[i][1])

print(ans)

Submission status: Accepted

Solution code:

def resolve():
    (N,T)=(int(i) for i in input().split())

    dishes=[[int(i) for i in input().split()] for _ in range(N)]
    dishes.sort()
    dpt=[[-1 for i in range(T+1)] for _ in range(N+1)]

    dpt[0][0]=0

    for i in range(N):
        for j in range(T):
            dpt[i+1][j]=max(dpt[i+1][j],dpt[i][j])
            dpt[i+1][min(T,j+dishes[i][0])]=max(dpt[i+1][min(T,j+dishes[i][0])],dpt[i][j]+dishes[i][1])
        dpt[i+1][T]=max(dpt[i+1][T],dpt[i][T])




    print(max(dpt[-1]))
#    for i in dpt:
#        print(i)

if __name__ == "__main__":
#    unittest.main()
    resolve()

Submission status: Accepted

Solution code:
from numpy import*
N,T,*L=int64(open(0).read().split())
d=zeros(1e4,int)
for w,v in sorted(zip(*[iter(L)]*2)):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Accepted

Solution code:
from numpy import*
N,T,*L=int64(open(0).read().split())
d=zeros(6010,int)
for w,v in sorted(zip(*[iter(L)]*2)):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
dp = [[0]*T for i in range(N+1)]

ls = []
for i in range(N):
  a, b = map(int, input().split())
  ls += [(a,b)]
ls.sort(key=lambda x:x[0])

for i in range(N):
  a, b = ls[i]
  for j in range(T-1, a-1, -1):
    dp[i+1][j] = max(dp[i][j], dp[i][j-a]+b)
    
pre = [0]*N
pre[-1] = ls[-1][1]
for i in range(N-2, -1, -1):
  pre[i] = max(pre[i+1], ls[i][1])
    
ans = 0
for i in range(N-1):
  ans = max(ans, max(dp[i+1])+pre[i+1])
ans = max(ans, max(dp[-1]))
print(ans)

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
D = [0 for i in range(N)]
maxA = 0
for i in range(N):
    A, B = map(int, input().split())
    D[i] = [A,B]
G = sorted(D)
maxT = T + G[N-1][0]
dp = [[0 for j in range(maxT)] for i in range(N)]
gans = G[0][1]
dp[0][G[0][0]] = G[0][1]
for i in range(1,N):
    for j in range(maxT):
        nowans = dp[i-1][j]
        if j >= G[i][0] and j <= T-1+G[i][0]:
            nowans = max(nowans, dp[i-1][j-G[i][0]] + G[i][1])
        dp[i][j] = nowans
        gans = max(gans, nowans)
#print(dp)
print(gans)

Submission status: Accepted

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


N, T = map(int, input().split())
AB = []
for _ in range(N):
    a, b = map(int, input().split())
    AB.append([a,b])

# Aを昇順にソート
AB.sort(key=lambda x:x[0])

# 残りのうち(j+1番目以降)で最も美味しいもの
bmax_res = [0]*N
bmax = -1
for i in range(N-1, 0, -1):
    a, b = AB[i]
    bmax = max(bmax, b)
    bmax_res[i-1] = bmax

# DPで解く
# DP[i][j] = i番目までの料理でj分以内に完食できる美味しさの合計の最大値
DP = [[0] * (T+1) for _ in range(N+1)]

res = 0
# update
for i in range(1,N+1):
    a, b = AB[i-1]
    for j in range(1,T+1):
        if a <= j:
            DP[i][j] = max(DP[i-1][j-a] + b, DP[i-1][j])
        else:
            DP[i][j] = max(DP[i-1][j], DP[i][j])
    res = max(res, DP[i][T-1]+bmax_res[i-1])
print(res)

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3005)
ans=chk=0
w=[]
for i in range(n):
    a,b=map(int,input().split())
    w.append((a,b))
w.sort()

for i in w:
    for j in range(t-1,-1,-1):
        dp[j+i[0]]=max(dp[j+i[0]],dp[j]+i[1])
        ans=max(ans,dp[j+i[0]])
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n, t = map(int, input().split())
lst = np.full((t), -float("inf"))
lst[0] = 0
ans = 0
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
for a, b in ab:
    ans = max(ans, max(lst) + b)
    if a < t:
        lst[a:] = np.maximum(lst[a:], lst[:-a] + b)
ans = int(ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, T = map(int, readline().split())
m = map(int, read().split())
AB = sorted(zip(m, m))

dp = np.zeros(T, np.int64)

ans = 0
table = np.zeros(T, int)
for a, b in AB:
    ans = max(ans, max(table) + b)
    np.maximum(table[:-a] + b, table[a:], out=table[a:])
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

N, T = list(map(int,input().split()))
AB = [list(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda k: k[0])

resbmax = [0]*N
bmax = 0
for i in range(N-1,0,-1):
	bi = AB[i][1]
	bmax = max(bmax,bi)
	resbmax[i-1] = bmax

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

ans = 0
i = 0
for ai, bi in AB:
	for j in range(T):
		if j>=ai:
			dp[i+1][j] = max(dp[i][j-ai]+bi, dp[i][j])
		else:
			dp[i+1][j] = dp[i][j]

	ans = max(ans, dp[i+1][-1]+resbmax[i])
	i += 1

print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8
import bisect
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = True



def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    n, t = LMIIS()
    ab = XLMIIS(n)
    ab.sort()

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

    ans = 0
    for i in range(n):
        ans = max(ans, dp1[i][t-1] + ab[i][1])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
foods = [list(map(int, input().split())) for _ in range(n)]
dp = [[0 for _ in range(t)] for _ in range(n)]
ans = 0

foods.sort(key=lambda x: x[0])

for food in range(n-1):
    for time in range(t):
        if time - foods[food][0] >= 0:
            dp[food+1][time] = max(dp[food][time], dp[food][time-foods[food][0]] + foods[food][1])
        else:
            dp[food+1][time] = dp[food][time]

for i in range(n):
    ans = max(ans, dp[i][t-1] + foods[i][1])

#ans = dp[-1][t-2] + foods[-1][1]
print(ans)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
x.sort()
a_max = x[-1][0]

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(ai):
        dp[j] = dp_pre[j]
    for j in range(ai, t + ai):
        dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi)
    for j in range(t + ai, t + a_max):
        dp[j] = dp_pre[j]
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

_n, _t = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(_n)]
AB.sort()

ans = 0
table = np.zeros(_t, int)
for a, b in AB:
    ans = max(ans, max(table) + b)
    np.maximum(table[:-a] + b, table[a:], out=table[a:])


print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

_n, _t = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(_n)]
AB.sort()


def dp(t, ablist):
    ans = 0
    table = np.zeros(t, int)
    for a, b in ablist:
        ans = max(ans, max(table) + b)
        np.maximum(table[:-a] + b, table[a:], out=table[a:])

    return ans


print(dp(_t, AB))

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
dp = [[0 for _ in range(T)] for _ in range(N+1)]
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort()
for i in range(1, N+1):
    A, B = AB[i-1]
    for j in range(T):
        dp[i][j] = max(dp[i][j], dp[i-1][j])
        if j >= A:
            dp[i][j] = max(dp[i][j], dp[i-1][j-A] + B)
AB.append((0, 0))
ans = [x[T-1] + AB[i][1] for i, x in enumerate(dp)]
print(max(ans))

Submission status: Accepted

Solution code:
n,t = map(int,input().split())
c = [tuple(map(int,input().split())) for _ in range(n)]
c.sort()
dp = [-1]*t
dp[0] = 0
ans = 0
for r in range(n):
    a,b = c[r]
    cur = max(dp) + b
    if ans < cur:
        ans = cur
    for i in range(t-a-1,-1,-1):
        if dp[i] >= 0 and dp[i+a] < dp[i] + b:
                dp[i+a] = dp[i] + b
cur = max(dp)
if cur > ans:
    ans = cur
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
D = []
for i in range(N):
    t, v = map(int, input().split())
    D.append((t, v))

D.sort(key=lambda x:x[0])

DP1 = [[0 for j in range(M)] for i in range(N + 2)]
DP2 = [[0 for j in range(M)] for i in range(N + 2)]

for i in range(1, N + 1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP1[i][j] = max(DP1[i - 1][j], DP1[i - 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP1[i][j] = DP1[i - 1][j]

for i in range(N, 0, -1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP2[i][j] = max(DP2[i + 1][j], DP2[i + 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP2[i][j] = DP2[i + 1][j]

mv = 0
for i in range(N):
    for j in range(M):
        mv = max(DP1[i][j] + DP2[i + 2][M - 1 - j] + D[i][1], mv)

print(mv)

Submission status: Accepted

Solution code:
import numpy as np

N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
# dp は0からT-1まで
dp = np.zeros(T, dtype=np.int32)
temp_ans = 0
for a, b in AB:
    temp_ans = max(temp_ans, dp.max() + b)
    dp[a:] = np.maximum(dp[a:], dp[:-a]+b)
print(temp_ans)

Submission status: Accepted

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

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

ab.sort()

ans = 0
i = 0
for a, b in ab:
    i += 1
    ans = max(b, ans)
    for j in range(t):
        dp[i][j] = dp[i-1][j]
    for j in range(t):
        if dp[i-1][j] > 0:
            if j + a >= t:
                ans = max(dp[i-1][j] + b, ans)
            else:
                dp[i][j+a] = max(dp[i][j+a], dp[i-1][j] + b)
                ans = max(dp[i][j+a], ans)
    if a < t:
        dp[i][a] = max(dp[i][a], b)
print(ans)

Submission status: Accepted

Solution code:
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
AB.sort(key = lambda x:x[0])
dp = [0]*(T+1)
for A,B in AB:  #それぞれの食事について
    for i in range(T-1,-1,-1):
        if A+i>=T:  #時間オーバー
            dp[T] = max(dp[T], dp[i]+B)
        else:   #時間内
            dp[i+A] = max(dp[i+A],dp[i]+B)
#これが成り立つのはAB.sortしているから
print(max(dp))

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
# 美味しさのMAX
Tmax = 6000
dp = [[-1] * (Tmax) for i in range(N+1)]
dp[0][0] = 0

q = []
for i in range(N):
    q.append(list(map(int, input().split())))
q.sort()

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

Submission status: Accepted

Solution code:
def main():
	_, t, *L = map(int, open(0).read().split())
	d = [0] * t
	cand = 0
	for w, v in sorted((x, y) for x, y in zip(*[iter(L)] * 2)):
		if cand < d[-1] + v:
			cand = d[-1] + v
		for i in range(t - 1, w - 1, -1):
			if d[i - w] + v > d[i]:
				d[i] = d[i - w] + v
	print(max(cand, max(d)))

if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:


def main():
    n, t = map(int, input().split())
    f = [list(map(int, input().split())) for _ in range(n)]
    dp1 = [[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 j in range(t - 1, -1, -1):
            if j - f[i][0] >= 0:
                dp1[i+1][j] = max(dp1[i][j - f[i][0]] + f[i][1],dp1[i][j])
            else:
                dp1[i+1][j] = dp1[i][j]
    for i in range(n):
        for j in range(t - 1, -1, -1):
            if j - f[n-1-i][0] >= 0:
                dp2[i+1][j] = max(dp2[i][j - f[n-1-i][0]] + f[n-1-i][1],dp2[i][j])
            else:
                dp2[i+1][j] = dp2[i][j]

    res = 0
    for i in range(n):
        for j in range(t):
            res = max(res,dp1[i][j]+f[i][1]+dp2[n-1-i][t-1-j])

    return res




print(main())