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 = [list(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda x: x[0])
#print(AB)

W = T + 1
def dp(AB,W):
    N = len(AB)
    DP = [[0]*W for i in range(N+1)]
    for i in range(N):
        for w in range(W):
            if w != W-1:
                if AB[i][0] <= w:
                    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]
            else:
                DP[i+1][w] = max(DP[i][w-1]+AB[i][1],DP[i][w])
    return DP

DP1 = dp(AB,W)

ans = DP1[N][T]
print(ans)

Submission status: Accepted

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

for i in range(N):
    tvlist[i] = list(map(int,input().split()))
tvlist.sort(key=lambda x: x[0])
for i in range(N):
    tlist[i] = tvlist[i][0]
    vlist[i] = tvlist[i][1]

for i in range(N):
    for t in range(T):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,T=map(int,input().split())
    l=[]
    for i in range(N):
        A,B=map(int,input().split())
        l.append((A,B))
    l.sort()
    dp=[[0]*T for _ in range(N)]
    for i in range(N-1):
        for t in range(T-1):
            dp[i+1][t+1]=max(dp[i+1][t+1],dp[i][t+1])
            if t+1>=l[i][0]:
                dp[i+1][t+1]=max(dp[i+1][t+1],dp[i][t+1-l[i][0]]+l[i][1])
    ans=0
    B_sorted=[x[1] for x in l]
    for i in range(1,N):
        ans=max(ans,dp[i][T-1]+max(B_sorted[i:]))
    print(ans)
        

if __name__ == '__main__':
    main()

Submission status: Accepted

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

G.sort()
dp=[0]*(60000)
for g in G:
    for i in range(T-1,-1,-1):
        dp[i+g[0]]=max(dp[i+g[0]],dp[i]+g[1])
print(max(dp))

Submission status: Accepted

Solution code:
n,t = map(int,input().split())
dpa = [[0 for i in range(t)] for j in range(n+1)]
dpb = [[0 for i in range(t)] for j in range(n+1)]

for i in range(t):
   dpa[0][i] = 0
   dpb[0][i] = 0
abl = []
for _ in range(n):
   a,b = map(int,input().split())
   abl.append((a,b))

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

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

abl = abl[::-1]
ans = 0
for i in range(t):
   for j in range(n):
      ans = max(ans, dpa[j][i]+dpb[n-j-1][t-i-1]+abl[j][1])

print(ans)

Submission status: Accepted

Solution code:
#Aをソートしておく，時間ギリギリにi番目の料理を頼むことで固定．
N,T = map(int,input().split())
AB = [[0,0] for _ in range(N)]

for i in range(N):
    AB[i][0],AB[i][1] = map(int,input().split())

AB.sort()
#ソートした時のbの情報を維持しないと

# T-1分以内にi番目以外の料理をできるだけ完食する
#DP1[i][t]はi番目の料理まででt分後までに完食で得られる幸福度の最大(商品番号は1から振る)


dp1 =[[0]*T for _ in range(N+1)]

for i in range(N):
    for t in range(1,T):
        dp1[i+1][t] = dp1[i][t]
        if t-AB[i][0]>=0:
            dp1[i+1][t]=max(dp1[i][t],dp1[i][t-AB[i][0]]+AB[i][1])
            
ans=0
for i in range(N):
    ans=max(ans,dp1[i][-1]+AB[i][1])
print(ans)

Submission status: Accepted

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

# T-1分以内に完食を目指す，特定の料理のみ使わないで計算したい．
#DP1[i][t]はi番目の料理まででt分後までに完食で得られる幸福度の最大(商品番号は1から振る)
#DP2[i][t]はiからN番目の料理でt分後までに完食で得られる幸福度の最大

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

for i in range(N):
    for t in range(1,T):
        dp1[i+1][t] = dp1[i][t]
        if t-A[i]>=0:
            dp1[i+1][t]=max(dp1[i][t],dp1[i][t-A[i]]+B[i])


for i in reversed(range(0,N)):
    for t in range(1,T):
        dp2[i][t] = dp2[i+1][t]
        if t-A[i]>=0:
            dp2[i][t]=max(dp2[i+1][t],dp2[i+1][t-A[i]]+B[i])


ans=0
#i番目の料理を時間ギリギリに注文
for i in range(1,N):
    for j in range(0,T):
        ans = max(ans,dp1[i][j]+dp2[i+1][T-1-j]+B[i])
print(ans)

Submission status: Accepted

Solution code:
def ints():
  return [int(x) for x in input().split()]
def ii():
  return int(input())

N, T = ints()
dp = [[0]*T for j in range(N+1)]
F = sorted([ints() for i in range(N)])

for i in range(N):
  a, b = F[i]

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

w = max([F[i][1] + dp[i][T-1] for i in range(1, N)])
print(w)

Submission status: Accepted

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

food.sort()

#dp[i][j]: i番目までの料理をいくつか食べて、時間jまでに注文した美味しさの合計のmax
dp = [[0 for i in range(t+1)] for j in range(n+1)]

for i in range(n):
    for j in range(t+1):
        a, b = food[i]
        if j == t:
            dp[i+1][j] = max(dp[i][j], dp[i+1][j])

        elif j + a >= t:
            dp[i+1][t] = max(dp[i][j] + b, dp[i+1][t])
            dp[i+1][j] = max(dp[i][j], dp[i+1][j])

        else:
            dp[i+1][j+a] = max(dp[i][j] + b, dp[i+1][j+a])
            dp[i+1][j] = max(dp[i][j], dp[i+1][j])

print(dp[n][t])

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
a, b = [0] * n, [0] * n
for i in range(n):
  a[i], b[i] = map(int, input().split())

dp = [[[0] * (t+1) for i in range(n+1)] for j in range(2)]
for i in range(n):
  for d in range(2):
    for l in range(1,t+1):
      if(l < a[i]):
        dp[d][i+1][l] = max(dp[d][i][l], d*(b[i]+dp[0][i][l-1]))
      else:
        dp[d][i+1][l] = max(dp[d][i][l], b[i]+dp[d][i][l-a[i]], d*(b[i]+dp[0][i][l-1]))

print(dp[1][n][t])

Submission status: Accepted

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

AB.sort()
dp = [0] * T
#print("AB:", AB)

answer = 0
for a, b in AB:
  for t in range(T-1, -1, -1):
    if dp[t]:
      if  a + t < T:
        dp[a + t] = max(dp[a + t], dp[t] + b)
        answer = max(answer, dp[a + t])
      else:
        answer = max(answer, dp[t] + b)
  if a < T:
    dp[a] = max(dp[a], b)
  answer = max(answer, b)
  #print("dp:", dp)
print(answer)

Submission status: Accepted

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

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

for i in range(n):
    for j in range(t+1):
        dp[i+1][j] =  dp[i][j]
    for j in range(t):
        next = min(j + ab[i][0], t)
        dp[i+1] [next] = max(dp[i+1][next], dp[i][j] + ab[i][1])
print(max(dp[n]))

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
food = []
for i in range(n):
    food.append(list(map(int, input().split())))
food.sort(key = lambda x:x[0])
b_max = [0 for i in range(n+2)]
for i in range(n):
    b_max[-(i+2)] = max(b_max[-(i+1)], food[-(i+1)][1])
dp = [[0 for i in range(t+1)] for j in range(n+1)]
for i in range(n):
    for j in range(t+1):
        if j + food[i][0] < t:
            dp[i+1][j + food[i][0]] = max(dp[i+1][j + food[i][0]], dp[i][j] + food[i][1])
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
ans = 0
for i in range(n+1):
    ans = max(ans, dp[i][-2] + b_max[i+1])
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n,t=map(int,input().split())
a=[list(map(int,input().split()))for i in range(n)]
b=np.zeros((n+1,t))
c=np.zeros((n+1,t))
for i in range(n):
    b[i+1]=b[i]
    b[i+1][a[i][0]:]=np.maximum(b[i+1][a[i][0]:],b[i][:-a[i][0]]+a[i][1])
    c[-i-2]=c[-i-1]
    c[-i-2][a[-i-1][0]:]=np.maximum(c[-i-2][a[-i-1][0]:],c[-i-1][:-a[-i-1][0]]+a[-i-1][1])
q=0
for i in range(n):
    q=max(q,max(b[i]+c[i+1][::-1])+a[i][1])
print(int(q))

Submission status: Accepted

Solution code:
n,t = map(int, input().split())
ls = [list(map(int, input().split())) for i in range(n)]
t-=1
dp1 = [[0 for _ in range(t+1)] for _ in range(n+1)]
for i in range(n+1):
    for j in range(t+1):
        if i>0 and j>=ls[i-1][0]:
                dp1[i][j] = max(dp1[i-1][j-ls[i-1][0]] + ls[i-1][1] ,dp1[i-1][j])

        elif i>0:
            dp1[i][j] = dp1[i-1][j]

dp2 = [[0 for _ in range(t+1)] for _ in range(n+1)]
for i in range(n, -1, -1):
    for j in range(t+1):
        if i<n and j>=ls[i][0]:
                dp2[i][j] = max(dp2[i+1][j-ls[i][0]] + ls[i][1] ,dp2[i+1][j])

        elif i<n:
            dp2[i][j] = dp2[i+1][j]

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


print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,T=map(int,input().split())
    AB=[tuple(map(int,input().split())) for _ in range(n)]
    AB.sort()

    dp=[0]*T
    ans=0
    for a,b in AB:
        ndp=dp[:]
        for t in range(T):
            if(t+a<T):
                ndp[t+a]=max(ndp[t+a],dp[t]+b)
            else:
                ans=max(ans,dp[t]+b)
        dp=ndp
    print(ans)
resolve()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
AB=[]
for i in range(N):
    AB.append(list(map(int,input().split())))
AB.sort(key=lambda x: x[0])

max_=0
for i in AB:
    if max_<i[0]:
        max_=i[0]

dp=[0]*(max_+M)
max_=0
for i in AB:
    for j in range(M)[::-1]:
        dp[j+i[0]] = max(dp[j+i[0]],dp[j]+i[1])
        
print(max(dp))

Submission status: Accepted

Solution code:
import numpy as np
def solve(n, t, a, b):
    dp = np.zeros(t, dtype=int)
    candidates = []
    for w, v in sorted(zip(a, b)):
        candidates.append(dp[-1] + v)
        if w > t:
            continue
        dp[w:] = np.maximum(dp[:-w] + v, dp[w:])
    return np.max(candidates)

n, t = map(int, input().split())
a = [0] * n
b = [0] * n
for i in range(n):
    a[i], b[i] = map(int, input().split())
print(solve(n, t, a, b))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, t = LI()
ab = sorted(LIR(n))
dp = [0] * t
ans = 0
for a, b in ab:
    ans = max(ans, dp[-1] + b)
    for i in range(a, t)[::-1]:
        dp[i] = max(dp[i], dp[i - a] + b)


print(ans)

Submission status: Accepted

Solution code:
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
    mv = 0
    for a, b in ab:
        mv = max(mv, dp[-1] + b)
        for i in range(t - 1 - a, -1, -1):
            dp[i+a] = max(dp[i+a], dp[i] + b)
    print(mv)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N,T=map(int,input().split())
import numpy as np
dp=np.zeros(T,dtype=int)
maxval=0

meals=[None]*N
for i in range(N):
  a,b=map(int,input().split())
  meals[i]=a,b

meals=sorted(meals,key=lambda x:x[0])

for i in range(N):
  a,b=meals[i]
  if maxval<dp[-1]+b:
    maxval=dp[-1]+b
  np.maximum(dp[:-a]+b,dp[a:],out=dp[a:])
  
print(maxval)

Submission status: Accepted

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

n, t = map(int, input().split())
food_l = []
for _ in range(n):
    a, b = map(int, input().split())
    food_l.append((a, b))
food_r = food_l[::-1]

dp_l = [ [0]*t for _ in range(n+1) ]
dp_r = [ [0]*t for _ in range(n+1) ]
for i in range(1, n+1):
    a_l, b_l = food_l[i-1]
    a_r, b_r = food_r[i-1]
    for j in range(t):
        dp_l[i][j] = max(dp_l[i][j], dp_l[i-1][j])
        if j + a_l < t:
            dp_l[i][j+a_l] = max(dp_l[i][j+a_l], dp_l[i-1][j]+b_l)
        
        dp_r[i][j] = max(dp_r[i][j], dp_r[i-1][j])
        if j + a_r < t:
            dp_r[i][j+a_r] = max(dp_r[i][j+a_r], dp_r[i-1][j]+b_r)

ans = 0
for i in range(n):
    res = max(food_l[i][1]+dp_l[i][j]+dp_r[n-i-1][t-j-1] for j in range(t))
    ans = max(res, ans)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

AB.sort()

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

ans = 0
for a, b in AB:
  ans = max(ans, dp[-1] + b)
  dp[a:] = np.maximum(dp[a:], dp[:-a] + b)

print(ans)

Submission status: Accepted

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

AB.sort()

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

ans = 0
for i, ab in enumerate(AB):
  ans = max(ans, dp[i][T-1] + ab[1])
  for t in range(T):
    if t < ab[0]:
      dp[i+1][t] = dp[i][t]
    else:
      dp[i+1][t] = max(dp[i][t-ab[0]] + ab[1], dp[i][t])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

dp1 = [[0]*T for _ in range(1+n)]
dp2 = [[0]*T for _ in range(1+n)]

for i in range(n):
    for j in range(T):
        dp1[i+1][j] = dp1[i][j]
        if j - food[i][0] >= 0:
            dp1[i+1][j] = max(dp1[i+1][j], dp1[i][j-food[i][0]] + food[i][1])

for i in range(n-1, -1, -1):
    for j in range(T):
        dp2[i][j] = dp2[i+1][j]
        if j - food[i][0] >= 0:
            dp2[i][j] = max(dp2[i][j], dp2[i+1][j-food[i][0]] + food[i][1])

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

Submission status: Accepted

Solution code:
def solve():
    import numpy as np
    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()
    max_Ai = AB[-1][0]
    
    dp = [[0] * T for i in range(N + 1)]
    dp = np.zeros(max_Ai + T, dtype=int)
    
    
    for A_i, B_i in AB:
        dp[A_i:A_i+T] = np.maximum(dp[A_i:A_i+T], dp[:T] + B_i)
    
    print(max(dp))

solve()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
def p_e():
    n, t = map(int, input().split())
    ab = [tuple(map(int, input().split())) for _ in range(n)]
    ab.sort()
    ma = ab[-1][0]
    dp = [0] * (t + ma)
    for w, v in ab:
        next = [0] * (t + ma)
        for j in range(1, t + ma):
            if 0 <= j - w < t:
                next[j] = max(dp[j - w] + v, dp[j])
            else:
                next[j] = dp[j]
        dp = next
    print(max(dp))
    
p_e()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,t=map(int,input().split())
    AB=[tuple(map(int,input().split())) for _ in range(n)]
    AB.sort()

    ans=0
    dp=[0]*t
    for a,b in AB:
        ans=max(ans,dp[-1]+b)
        ndp=dp[:]
        for i in range(t):
            if(i+a<t):
                ndp[i+a]=max(ndp[i+a],dp[i]+b)
        dp=ndp

    print(ans)
resolve()

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
p = []
for i in range(N):
    A, B = map(int, input().split())
    p.append([A, B])
p.sort()

dp = [[0]*3005 for _ in range(3005)]
ans = 0
for i in range(N):
    for j in range(T):
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        nextj = j + p[i][0]
        if nextj < T:
            dp[i+1][nextj] = max(dp[i+1][nextj], dp[i][j] + p[i][1])
    now = dp[i][T-1] + p[i][1]
    ans = max(ans, now)

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(key=lambda x:(x[0], -x[1]))

dp = np.array([0]*t)
ans = 0

for a, b in ab:
    ans = max(ans, dp[-1] + b)
    dp[a:] = np.maximum(dp[:-a]+b, dp[a:])
print(max(ans, dp[-1]))

Submission status: Accepted

Solution code:
import numpy as np

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
ab.sort()
dp = np.array([0]*t)
ans = 0
for a,b in ab:
    ans = max(ans,dp[-1]+b)
    dp[a:] = np.maximum(dp[:-a]+b,dp[a:])
# print(dp,ans)
print(max(ans,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(key=lambda x: x[0])

dp = [[0] * (n+1) for i in range(t)]
ans = 0
for time in range(1, t):
    for i in range(n):
        if ab[i][0] <= time:
            if dp[time][i] < (ab[i][1] + dp[time - ab[i][0]][i]):
                dp[time][i+1] = (ab[i][1] + dp[ time - ab[i][0] ][i])
            else:
                dp[time][i+1] = dp[time][i]
        else:
            dp[time][i+1] = dp[time][i]
        if i < n-1:
            ans = max(ans, dp[time][i+1] + ab[i+1][1])

print(ans)

Submission status: Accepted

Solution code:
n,t = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([a,b])
dp = [0] * (t)
ab.sort()
m = 0
for i,j in ab:
  pdp,dp = dp,[0] * (t)
  for k in range(t):
    dp[k] = pdp[k]
  for k in range(t):
    if k+i >= t:
      m = max(m,pdp[k]+j)
    else:
      dp[k+i] = max(dp[k+i],pdp[k]+j)
if m == 0:
  print(max(dp))
else:
  print(m)

Submission status: Accepted

Solution code:
# コード整理

import sys
from operator import itemgetter

input = sys.stdin.readline

#################
# 定数
#################
TIME = 0
VAL = 1

##################
# 入力処理
##################
N, T = [int(a) for a in input().split()]
time_value = [(-1, -1)] + [tuple(int(a) for a in input().split()) for _ in range(N)]

# Sort the array in the increasing order of value
time_value.sort(key = itemgetter(VAL))

###########################
# 動的計画法（Knapsack problem）のテーブル埋め
# 時間の最大値は T - 1
##########################
dp = [[-1] * T for _ in range(N + 1)] # (N+1)行 x T列 2次元リスト

for t in range(0, T):
    dp[0][t] = 0

for n in range(1, N + 1):
    dp[n][0] = 0
    for t in range(1, T):
        if time_value[n][TIME] > t:
            dp[n][t] = dp[n - 1][t]
        else:
            dp[n][t] = max(dp[n - 1][t],
                             time_value[n][VAL] + dp[n - 1][t - time_value[n][TIME]])

##########################
# DPテーブルを用いて問題を解く
##########################

# t = T - 1に食べ始める料理が N, N-1, N-2, ..., 2, 1それぞれの場合で
# 美味しさの最大値を出し、更にその最大値を取る

# 最後に食べる料理がNの場合
val_acum = time_value[N][VAL]
t = T - 1  # N以外の料理を食べるのに使える時間
max_val = val_acum + dp[N - 1][t]

for n in range(N - 1, 0, -1):  # 最後に食べる料理が n = N-1, N-2, ..., 2, 1の場合
    val_acum += time_value[n][VAL] # 料理 N, N-1, ..., nの美味しさ合計
    t -= time_value[n + 1][TIME] # その他の料理 1, 2, ..., n-1を食べるのに使える時間
    if t < 0:
        break
    else:
        max_val = max(max_val, val_acum + dp[n - 1][t])

print(max_val)

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter

input = sys.stdin.readline

#################
# 定数
#################
TIME = 0
VAL = 1

##################
# 入力処理
##################
N, T = [int(a) for a in input().split()]
time_value = [(-1, -1)] + [None] * (N)
for i in range(1, N + 1):
    time_value[i] = tuple(int(a) for a in input().split())

# Sort the array in the increasing order of value
time_value.sort(key = itemgetter(VAL))

###########################
# 動的計画法（Knapsack problem）のテーブル埋め
# 時間の最大値は T - 1
##########################
dp = [[-1] * T for _ in range(N + 1)] # (N+1)行 x T列 2次元リスト

for t in range(0, T):
    dp[0][t] = 0

for n in range(1, N + 1):
    dp[n][0] = 0
    for t in range(1, T):
        if time_value[n][TIME] > t:
            dp[n][t] = dp[n - 1][t]
        else:
            dp[n][t] = max(dp[n - 1][t],
                             time_value[n][VAL] + dp[n - 1][t - time_value[n][TIME]])

##########################
# DPテーブルを用いて問題を解く
##########################

# t = T - 1に食べ始める料理が N, N-1, N-2, ..., 2, 1それぞれの場合で
# 美味しさの最大値を出し、更にその最大値を取る

# 最後に食べる料理がNの場合
val_acum = time_value[N][VAL]
t = T - 1  # N以外の料理を食べるのに使える時間
max_val = val_acum + dp[N - 1][t]

for n in range(N - 1, 0, -1):  # 最後に食べる料理が n = N-1, N-2, ..., 2, 1の場合
    val_acum += time_value[n][VAL] # 料理 N, N-1, ..., nの美味しさ合計
    t -= time_value[n + 1][TIME] # その他の料理 1, 2, ..., n-1を食べるのに使える時間
    if t < 0:
        break
    else:
        max_val = max(max_val, val_acum + dp[n - 1][t])

print(max_val)

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
dp=[[-1 for j in range(2)] for i in range(t+1)]
dp[0][0]=0
d=[]
for i in range(n):
    d.append(list(map(int,input().split())))
d.sort()
for i in range(n):
    for j in range(t,-1,-1):
        for k in range(1,-1,-1):
            if k==0:
                if dp[j][k]>=0 and j+d[i][0]<t:
                    dp[j+d[i][0]][k]=max(dp[j+d[i][0]][k],dp[j][k]+d[i][1])
            if k==1:
                if dp[j][0]>=0:
                    dp[j][k]=max(dp[j][k],dp[j][0]+d[i][1])
ans=0
for i in range(t+1):
    for j in range(2):
        ans=max(ans,dp[i][j])
print(ans)

Submission status: Accepted

Solution code:
#input
N, T = map(int, input().split())
A = []
B = []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

#output
import numpy as np
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)
#dp[品物][時間]

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
from operator import itemgetter

sys.setrecursionlimit(5000)
input = sys.stdin.readline


TIME = 0
VAL = 1

N, T = [int(a) for a in input().split()]
time_value = [None] * (N + 1)
time_value[0] = (-1, -1)
for i in range(1, N + 1):
    time_value[i] = tuple(int(a) for a in input().split())

# Sort the array in the increasing order of value
time_value.sort(key = itemgetter(VAL))


# T by (N + 1) table for knapsack problem
# maximum possible time is T-1
dp_table = [[-1] * T for _ in range(N + 1)]

# Fill in dp_table i.e. solve knapsack problem
for t in range(0, T):
    dp_table[0][t] = 0

for n in range(1, N + 1):
    dp_table[n][0] = 0
    for t in range(1, T):
        if time_value[n][TIME] > t:
            dp_table[n][t] = dp_table[n - 1][t]
        else:
            dp_table[n][t] = max(dp_table[n - 1][t],
                             time_value[n][VAL] + dp_table[n - 1][t - time_value[n][TIME]])


val_acum = time_value[N][VAL]
t = T - 1
max_val = val_acum + dp_table[N - 1][t]

for n in range(N - 1, 0, -1):  # food to eat at the end
    val_acum += time_value[n][VAL] # value[N] + value[N-1] + ... + value[n]
    t -= time_value[n + 1][TIME] # T - 1 - (time[N] + time[N-1] + ... + time[n + 1])
    if t < 0: break
    tmp = val_acum + dp_table[n - 1][t]
    max_val = max(max_val, tmp)

print(max_val)

Submission status: Accepted

Solution code:
import numpy as np


def solve():
    N, T = map(int, input().split())
    ab_l = [[0, 0] for _ in range(N)]
    for i in range(N):
        ab_l[i][0], ab_l[i][1] = map(int, input().split())
    ab_l.sort()
    dp = np.zeros(T, dtype=int)
    ans = 0

    for a, b in ab_l:
        ans = max(ans, dp[-1] + b)
        dp[a:] = np.maximum(dp[a:], dp[:-a]+b)

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import math
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N,T=LI()
lis=[tuple(LI()) for i in range(N)]
lis.sort()
RES=[[0]*(T+1) for i in range(N+1)]
for i in range(1, N+1):
    A, B = lis[i-1]
    for t in range(T):
        if (t-A >= 0):
            RES[i][t] = max(RES[i-1][t], RES[i-1][t-A]+B)
        else:
            RES[i][t] = RES[i-1][t]
    RES[i][T] = max(RES[i-1][T], RES[i-1][T-1]+B)

print(RES[i][T])

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
items = sorted([list(map(int, input().split())) for i in range(N)])

# dp1[i][j] := 商品0 ~ i が対象
dp = [[0] * (W + 1) for i in range(N + 1)]

# dpテーブルを埋める
for i in range(N):
    wi, vi = items[i]
    for j in range(W + 1):
        if j + wi <= W:
            dp[i + 1][j + wi] = max(dp[i + 1][j + wi], dp[i][j] + vi)
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j])


ans = 0
for i, (wi, vi) in enumerate(items):
    ans = max(ans, dp[i][W - 1] + vi)

print(ans)

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
items = sorted([list(map(int, input().split())) for i in range(N)])

# dp1[i][j] := 商品0 ~ i が対象
dp = [[0] * (W + 1) for i in range(N + 1)]

# dpテーブルを埋める
for i in range(N):
    wi, vi = items[i]
    for j in range(W + 1):
        if j + wi <= W:
            dp[i + 1][j + wi] = max(dp[i + 1][j + wi], dp[i][j] + vi)
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j])


ans = 0
for i, (wi, vi) in enumerate(items):
    for j in range(W):
        ans = max(ans, dp[i][j] + vi)

print(ans)

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
items = [list(map(int, input().split())) for i in range(N)]

# dp1[i][j] := 商品0 ~ i が対象
# dp2[i][j] := 商品i ~ N が対象(順番はそのまま)
# out-of-index 対策に余分に取っておく
dp1 = [[0] * (W + 1) for i in range(N + 2)]
dp2 = [[0] * (W + 1) for i in range(N + 2)]

# dp1を埋める
for i in range(N):
    wi, vi = items[i]
    for j in range(W + 1):
        if j + wi <= W:
            dp1[i + 1][j + wi] = max(dp1[i + 1][j + wi], dp1[i][j] + vi)
        dp1[i + 1][j] = max(dp1[i][j], dp1[i + 1][j])

# dp2を埋める
for i in range(N + 1, 1, -1):
    wi, vi = items[i - 2]
    for j in range(W + 1):
        if j + wi <= W:
            dp2[i - 1][j + wi] = max(dp2[i - 1][j + wi], dp2[i][j] + vi)
        dp2[i - 1][j] = max(dp2[i][j], dp2[i - 1][j])


# 2つのdpテーブルを使って、最後に使う商品を全探索
ans = 0
# ここからは商品を1-indexedとする
for i, (wi, vi) in enumerate(items, start=1):
    for j in range(W):
        ans = max(ans, dp1[i - 1][j] + dp2[i + 1][W - j - 1] + vi)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n,t = map(int, input().split())
DP = np.full((t), -float("inf"))
DP[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(DP)+b)
    if a<t:
        DP[a:] = np.maximum(DP[a:], DP[:-a]+b)

ans = int(ans)
print(ans)

Submission status: Accepted

Solution code:
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)


if __name__ == '__main__':
    solve()