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:
#145_E
n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]

#dp1[i][j] ... 1~iでj分以内の最大値
#dp2[i][j] ... i~nでj分以内の最大値
dp1 = [[0 for _ in range(t)] for _ in range(n+1)]
dp2 = [[0 for _ in range(t)] for _ in range(n+2)]

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

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

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

print(ans)

Submission status: Accepted

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

d = [0] * (6000)
for c, v in a:
    for i in reversed(range(t)):
        d[i+c] = max(d[i+c], d[i] + v)

print(max(d))

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()

ans = 0
dp = [[0 for _ in range(3005)] for _ in range(3005)]
for i in range(N):
    a, b = AB[i]
    for j in range(T):
        # iを使わないパターン
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        # iを使うパターン
        nj = j + a
        if nj < T:
            dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + b)
    now = dp[i][T - 1] + b
    ans = max(ans, now)

print(ans)

Submission status: Accepted

Solution code:
from copy import deepcopy

# 多次元配列を作成する
def make_multi_list(initial, degree):
    ans = [initial for _ in range(degree[-1])]
    for d in reversed(degree[:-1]):
        ans = [deepcopy(ans) for _ in range(d)]
    return ans


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

ans = 0
dp = make_multi_list(0, [3005, 3005])
for i in range(N):
    a, b = AB[i]
    for j in range(T):
        # iを使わないパターン
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        # iを使うパターン
        nj = j + a
        if nj < T:
            dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + b)
    now = dp[i][T - 1] + b
    ans = max(ans, now)

print(ans)

Submission status: Accepted

Solution code:
import itertools


def read():
    N, T = list(map(int, input().strip().split()))
    A = list()
    B = list()
    for i in range(N):
        a, b = list(map(int, input().strip().split()))
        A.append(a)
        B.append(b)
    return N, T, A, B


def solve(N, T, A, B):
    dp1 = [[0 for j in range(T)] for i in range(N+2)]
    dp2 = [[0 for j in range(T)] for i in range(N+2)]
    for i, t in itertools.product(range(N), range(T)):
        u = t - A[i]
        if u >= 0:
            dp1[i+1][t] = max(dp1[i][u] + B[i], dp1[i][t])
        else:
            dp1[i+1][t] = dp1[i][t]
        u = t - A[N-1-i]
        if u >= 0:
            dp2[N-i][t] = max(dp2[N+1-i][u] + B[N-1-i], dp2[N+1-i][t])
        else:
            dp2[N-i][t] = dp2[N+1-i][t]
    v = 0
    for i, t in itertools.product(range(N), range(T)):
        v = max(v, dp1[i][t] + dp2[i+2][(T-1) - t] + B[i])
    return v


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

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

n, t = [int(x) for x in input().split()]
ab = []
for _ in range(n):
    ab.append([int(x) for x in input().split()])
ab_rev = ab[::-1]

dp1 = [[0] * t for _ in range(n + 1)] # 前から見た
dp2 = [[0] * t for _ in range(n + 1)] # 後ろから見た

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

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

ans = -1

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

print(ans)

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
ab=[[0]*2 for i in range(n+1)]
for i in range(1,n+1):
  ab[i][0],ab[i][1]=map(int,input().split())
dp=[[0]*(t+3001) for i in range(n+1)]
ab.sort(key=lambda x:x[0])
for i in range(1,n+1):
  for j in range(t):
    dp[i][j]=max(dp[i][j],dp[i-1][j])
    dp[i][j+ab[i][0]]=max(dp[i][j+ab[i][0]],dp[i-1][j]+ab[i][1])
ans=0
for i in range(n+1):
  ans=max(ans,max(dp[i]))
print(ans)

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 = np.zeros(t, dtype=np.int64)

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


print(ans)

Submission status: Accepted

Solution code:
def main():
    N, T = map(int, input().split())
    ab = [tuple(map(int, input().split())) for i in range(N)]
    ab.sort(key=lambda x: x[0])
    t = ab[-1][0]
    dp = [-1]*(T+t+1)
    dp[0] = 0
    for a, b in ab:
        for i in range(T-1, -1, -1):
            if dp[i] < 0:
                continue
            if dp[i+a] < dp[i]+b:
                dp[i+a] = dp[i]+b
    ans = max(dp)
    print(ans)
main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N, T = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(N)]
    ab.sort(key=lambda x: x[0])
    t = ab[-1][0]
    dp = [-1 for i in range(T+t + 1)]
    dp[0] = 0
    for a, b in ab:
        for i in range(T+t, 0, -1):
            if i >= a:
                if dp[i-a] >= 0 and i - a < T:
                    dp[i] = max(dp[i], dp[i-a] + b)

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

    # print(dp)
    print(ans)


main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N, T = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(N)]
    ab.sort(key=lambda x: (x[0], x[1]))
    dp = [-1 for i in range(T*2 + 1)]
    dp[0] = 0
    for a, b in ab:
        for i in range(T*2, 0, -1):
            if i >= a:
                if dp[i-a] >= 0 and i - a < T:
                    dp[i] = max(dp[i], dp[i-a] + b)

    for i in range(1, T*2+1):
        dp[i] = max(dp[i], dp[i-1])
    ans = dp[-1]

    ab.sort(key=lambda x: x[1])
    tmp = ab.pop()
    dp = [-1 for i in range(T)]
    dp[0] = 0
    for a, b in ab:
        for i in range(T-1, 0, -1):
            if i >= a:
                if dp[i-a] >= 0:
                    dp[i] = max(dp[i], dp[i-a] + b)

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

    # print(dp)
    print(ans)


main()

Submission status: Accepted

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,t=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
ab.sort()
dp=[[-1]*t for i in range(n+1)]
dp[0][0]=0
ans=0
for i in range(n):
    a,b=ab[i]
    for j in range(t):
        dp[i+1][j]=max(dp[i][j],dp[i+1][j])
        if dp[i][j]>=0:
            if j+a>=t:
                ans=max(ans, dp[i][j]+b)
            else:
                dp[i+1][j+a]=max(dp[i+1][j+a], dp[i][j]+b)
for i in range(t):
    ans=max(ans, dp[-1][i])
print(ans)
# print(dp)

Submission status: Accepted

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

n,t = map(int,readline().split())

dp = [0]*(t+1)

AB = [list(map(int,readline().split())) for i in range(n)]
AB.sort(reverse=True)
for i in range(n):
    a,b = AB[i]
    for j in range(t):
        if j+a > t:
            dp[j] = max(dp[j],b)
        else:
            dp[j] = max(dp[j],dp[j+a]+b)

print(dp[0])

Submission status: Accepted

Solution code:
def main():
    import numpy as np
    n,t=map(int,input().split())
    F=[tuple(map(int,input().split())) for _ in range(n)]
    F.sort(key=lambda x:x[0])

    dp=np.zeros([n+1,t],dtype=np.int64)
    for i in range(n):
        a1=F[i][0]
        b1=F[i][1]
        dp[i+1][:a1]=dp[i][:a1]
        dp[i+1][a1:]=np.maximum(dp[i][a1:],dp[i][:-a1]+b1)
    
    ans=0
    for i in range(n):
        ans=max(ans,dp[i][-1]+F[i][1])
    print(ans)
         
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
# https://codeforces.com/blog/entry/71482
# https://atcoder.jp/contests/abc145/submissions/8473167

import numpy
n, t = map(int, input().split())
time = []
for i in range(n):
    x, y = map(int, input().split())
    time.append((x, y))


dp = numpy.zeros(t, dtype=int)
time.sort()
ans = 0
for a, b in time:
    ans = max(ans, dp[-1]+b)
    dp[a:] = numpy.maximum(dp[a:], dp[:-a]+b)

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter
import numpy as np

def main():
    N,T = map(int,input().split())
    food = []
    for _ in range(N):
        a,b = map(int,input().split())
        food.append((a,b))
        
    dp = np.zeros(T,dtype=int)
    food.sort(key = itemgetter(0))
    
    ans = 0
    for a,b in food:
        ans = max(ans,dp[-1]+b)
        dp[a:] = np.maximum(dp[a:],dp[:-a]+b)

    print(ans)
    
if __name__ == "__main__":
    main()

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)]


def knapsack(ab):
    dp = np.zeros(T, dtype=np.int64)
    res = [dp]
    for a, b in ab:
        newDP = res[-1].copy()
        newDP[a:] = np.maximum(newDP[a:], newDP[:-a] + b)
        res.append(newDP)
    return res


dp1 = knapsack(AB)
dp2 = knapsack(AB[::-1])
dp2 = dp2[::-1]

ans = 0

for i in range(N):
    _, b = AB[i]
    l = dp1[i]
    r = dp2[i + 1][::-1]
    tmp = (l + r).max() + b
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N, T = map(int, input().split())
    C = []
    max_A = 0
    for _ in range(N):
        A, B = map(int, input().split())
        max_A = max(max_A, A)
        C.append((A,B))

    C.sort()

    dp = [[0] * (T + max_A) for _ in range(N+5)]
    for i in range(N):
        for t in range(T):
            dp[i+1][t+C[i][0]] = max(dp[i+1][t+C[i][0]], dp[i][t] + C[i][1])
            dp[i+1][t] = max(dp[i+1][t], dp[i][t])        

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

    print(ans)


if __name__ == "__main__":
    main()

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(key=lambda x:x[0])
dp = [[0 for i in range(t)] for i in range(n+1)]
al = []
for i in range(n):
    a,b = l[i]
    for j in range(t):
        if dp[i][j] == 0:
            if j == 0:
                if j + a < t:
                    dp[i+1][j+a] = max(dp[i][j+a],b)
                else:
                    al.append(b)
        else:
            if j + a < t:
                dp[i+1][j+a] = max(dp[i][j+a],dp[i][j]+b)
            else:
                al.append(dp[i][j]+b)
            dp[i+1][j] = max(dp[i+1][j],dp[i][j])
if len(al) > 0:
    print(max(max(dp[n]),max(al)))
else:
    print(max(dp[n]))

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
arr=sorted(arr,key=lambda x:x[1])
arr=sorted(arr,key=lambda x:x[0])
dp=[[0]*3001 for _ in range(n+1)]
maxs=[0]
for i in range(n-2,-1,-1):
  maxs.append(max(maxs[-1],arr[i+1][1]))
maxs=maxs[::-1]
for i in range(n):
  a,b=arr[i]
  for j in range(t):
    if j-a<0:
      continue
    dp[i+1][j]=max(dp[i][j],dp[i][j-a]+b)
  dp[i+1][a]=max(dp[i][a],b)
ans=0
for i in range(n):
  tmp=max(dp[i+1][:t])+maxs[i]
  ans=max(ans,tmp)
print(ans)

Submission status: Accepted

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

# dp1[i][j] 1 ~ i の料理で j 分以内に完食できる美味しさの合計の最大値
# dp2[i][j] i ~ N の料理で j 分以内に〃
# max(DP1[i − 1][j] + DP2[i + 1][T − 1 − j]) i番目以外で T - 1 以内

dp1 = [[0] * T for _ in range(N+2)]
dp2 = [[0] * T for _ in range(N+2)]
for i in range(1, N+1):
  for j in range(T):
    dp1[i][j] = dp1[i-1][j]
    if j - A[i] >= 0:
      dp1[i][j] = max(dp1[i][j], dp1[i-1][j - A[i]] + B[i])

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

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

print(ans)

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda ab: ab[0])

# dp[i][j] := i番目の料理までから選んだときの、j分後における満足度の最大値
# dp[i][j] := max(dp[i - 1][j - A] + B, dp[i - 1][j])
dp = [[0] * (T + 1) for _ in range(N + 1)]
ans = 0
for i in range(1, N + 1):
    a, b = AB[i - 1]
    for j in range(T + 1):
        if j == T:
            a = 1
        if j - a < 0:
            dp[i][j] = dp[i - 1][j]
        else:
            dp[i][j] = max(dp[i - 1][j - a] + b, dp[i - 1][j])

print(dp[N][T])

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,t = inm()
ab = []
for i in range(n):
    aa,bb = inm()
    ab.append((aa,-bb))
ab.sort()
d = [ [-1]*(t+1) for i in range(n+1) ]
for i in range(n+1):
    d[i][0] = 0
#ddprint(d)
for i in range(n):
    for j in range(t+1):
        d[i+1][j] = max(d[i+1][j], d[i][j])
        if j<t and d[i][j]>=0:
            s = min(t,j+ab[i][0])
            d[i+1][s] = max(d[i+1][s], d[i][j]-ab[i][1])
if DBG:
    for i in range(n):
        printn(i)
        ddprint(d[i])
mx = 0
for j in range(t,-1,-1):
    if d[n][j]>=0:
        mx = max(mx,d[n][j])
print(mx)

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
ls = []
for i in range(N):
  a, b = map(int, input().split())
  ls += [(a,b)]
ls.sort(key=lambda x:x[0])
t = [[0]*T for i in range(N+1)]
for i in range(1,N+1):
  a, b = ls[i-1]
  for j in range(T):
    if j-a>=0:
      t[i][j] = max(t[i-1][j-a]+b,t[i-1][j])
last = [0]
for i in range(N-1,-1,-1):
  last += [max(last[-1], ls[i][-1])]
ans = 0
for i in range(1,N+1):
  j = N-i
  ans = max(ans, last[j]+t[i][-1])
print(ans)

Submission status: Accepted

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

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

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)

last = [0]*N
last[-1] = ls[-1][1]
for i in range(N-2, -1, -1):
  last[i] = max(last[i+1], ls[i][1])

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, T = lr()
AB = [lr() for _ in range(N)]
AB.sort()
# 最後の注文はAiが選ぶ料理で最大で、T-1分後に注文するとして良い
dp = np.zeros(T, 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())
A = [[0,0] for _ in range(N+1)]
for i in range(1,N+1):
    a,b = map(int,input().split())
    A[i][0] = a
    A[i][1] = b

A.sort()

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

Submission status: Accepted

Solution code:
n,t = map(int,input().split())
dish = []
for _ in range(n):
  a,b = map(int,input().split())
  dish.append([a,b])
dish.sort()
now = [0 for _ in range(t+1)]
for i in range(n):
  a,b = dish[i]
  for d in range(t)[::-1]:
    if d + a < t:
      now[d+a] = max(now[d+a],now[d]+b)
    else:
      now[-1] = max(now[-1],now[d]+b)
  #print(now)
print(now[-1])

Submission status: Accepted

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N,T = MI()
	max_T = -1
	dish = []
	for i in range(N):
		A,B = MI()
		dish.append([A,B])
		dish.sort(key=lambda x : x[0])
		max_T = max(max_T, A)

	dp=np.array([-1 for _ in range(T+max_T)])
	dp[0] = 0
	upper = 1
	for i in range(N):
		ch = (dp>=0)
		ch[T:] = False
		ch2 = np.roll(ch, dish[i][0])
		ch2[:dish[i][0]]=False
		dp[ch2] = np.maximum(dp[ch2] , dp[ch] + dish[i][1])
	print(max(dp))
main()

Submission status: Accepted

Solution code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]
    dp1 = [[0]*(T+1) for i in range(N+2)]
    for i in range(1, N+1):
        for j in range(T):
            dp1[i][j] = max(dp1[i][j], dp1[i-1][j])
            if j + A[i-1] < T:
                dp1[i][j + A[i-1]] = \
                    max(dp1[i][j + A[i-1]], dp1[i-1][j] + B[i-1])

    dp2 = [[0]*(T+1) for i in range(N+2)]
    for i in range(1, N+1)[::-1]:
        for j in range(T):
            dp2[i][j] = max(dp2[i][j], dp2[i+1][j])
            if j + A[i-1] < T:
                dp2[i][j + A[i-1]] = \
                    max(dp2[i][j + A[i-1]], dp2[i+1][j] + B[i-1])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, T = map(int, input().split())

A, B = zip(*[tuple(map(int, input().split())) for _ in range(N)])
dp1 = [[0]*(T) for _ in range(N+1)]
dp2 = [[0]*(T) for _ in range(N+2)]

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

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

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

print(ans)

Submission status: Accepted

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


n, t = map(int, input().split())

DISHES = [(0, 0)]
for _ in range(n):
    a, b = map(int, input().split())
    DISHES.append((a, b))
DISHES.sort()

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

answer = 0
for k in range(1, n + 1):
    answer = max(answer, DP[k - 1][t - 1] + DISHES[k][1])

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10 ** 7)

# ・最適解をとってみる
# ・最適解に仮定できる構造を探す
# ・探索空間が小さくなったら、既知の手法に当てはまる（dpなど）

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

FOOD.sort()
dp = np.array([np.zeros(T + 1, dtype=np.int64) for _ in range(N + 1)])
ans = 0
for i in range(N):
    a, b = FOOD[i]
    dp[i + 1][a:] = np.maximum(dp[i][:-a] + b, dp[i][a:])

    # if len(dp[i][-a+1:T]) > 0:
    # dp[i + 1][T] = max(dp[i + 1][T], max(dp[i][-a+1:T]) + b)
    now = 0
    if i != N-1:
        now = dp[i + 1][T - 1] + max(FOOD[i + 1:], key=lambda x: x[1])[1]
        #print(FOOD[i + 1:])
        #print(max(FOOD[i + 1:], key=lambda x: x[1])[1])
        #print(now)

    ans = max(ans, now)

    # print(dp[i+1])

print(ans)

Submission status: Accepted

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

    dp = [-1] * (T + 3000)
    dp[0] = 0
    for a, b in sorted(AB):
        for j in range(T - 1, -1, -1):
            if dp[j] == -1:
                continue
            if dp[j] + b > dp[j + a]:
                dp[j + a] = dp[j] + b
    print(max(dp))


main()

Submission status: Accepted

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


dp1 = [[0] * T for _ in range(N + 1)]
for i in range(N):
    for j in range(T):
        here = dp1[i][j]
        a, b = A[i], B[i]
        if j < a:
            dp1[i + 1][j] = here
        else:
            dp1[i + 1][j] = max(here, dp1[i][j - a] + b)

dp2 = [[0] * T for _ in range(N + 2)]
for i in range(N, 0, -1):
    for j in range(T):
        here = dp2[i + 1][j]
        a, b = A[i - 1], B[i - 1]
        if j < a:
            dp2[i][j] = here
        else:
            dp2[i][j] = max(here, dp2[i + 1][j - a] + b)

ans = 0
for i in range(1, N + 1):
    for j in range(T):
        n1 = dp1[i - 1][j]
        n2 = dp2[i + 1][T - 1 - j]
        last = B[i - 1]
        ans = max(ans, n1 + n2 + last)

print(ans)

Submission status: Accepted

Solution code:
n,t = map(int,input().split())
a = [list(map(int,input().split())) for i in range(n)]
a.sort()
DP = [[0 for i in range(t+3001)] for j in range(n+1)]
for i in range(1,n+1):
  for j in range(1,t+3001):
    if j-a[i-1][0] >= 0:
      DP[i][j] = max(DP[i-1][j],DP[i-1][j-a[i-1][0]]+a[i-1][1])
    else:
      DP[i][j] = DP[i-1][j]
ans = 0
for i in range(n):
  ans = max(ans,DP[i][t-1]+a[i][1])
print(ans)

Submission status: Accepted

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

for i in range(N):
    A, B = D[i]
    for j in range(T):
        here = dp[i][j]
        if j < A:
            dp[i + 1][j] = here
        else:
            dp[i + 1][j] = max(here, dp[i][j - A] + B)

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

print(ans)

Submission status: Accepted

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

for i in range(N):
    A, B = D[i]
    for j in range(T):
        here = dp[i][j]
        if j < A:
            dp[i + 1][j] = here
        else:
            dp[i + 1][j] = max(here, dp[i][j - A] + B)

maxB = [0] * (N + 1)
t = 0
for i in range(N - 1, -1, -1):
    t = max(t, D[i][1])
    maxB[i] = t

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

print(ans)

Submission status: Accepted

Solution code:
N,T = list(map(int,input().split()))

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

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

INF = float("inf")

DP = [[-INF]*(T+3001) for i in range(N+1)]

DP[0][0] = 0

for i in range(N):
    for j in range(T):
        DP[i+1][j] = max(DP[i+1][j],DP[i][j])
        DP[i+1][j+A[i][0]] = max(DP[i+1][j+A[i][0]],DP[i][j+A[i][0]],DP[i][j]+A[i][1])
score = 0
for i in range(N):
    for j in range(T+A[i][0]):
        score = DP[i+1][j] if DP[i+1][j]>score else score

print(score)

Submission status: Accepted

Solution code:
N,T = list(map(int,input().split()))

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

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

INF = float("inf")

DP = [[-INF]*(T+3001) for i in range(N+1)]

DP[0][0] = 0

for i in range(N):
    for j in range(T):
        DP[i+1][j] = max(DP[i+1][j],DP[i][j])
        DP[i+1][j+A[i][0]] = max(DP[i+1][j+A[i][0]],DP[i][j+A[i][0]],DP[i][j]+A[i][1])
score = 0
for i in range(N):
    for j in range(T+A[i][0]):
        score = max(DP[i+1][j],score)

print(score)

Submission status: Accepted

Solution code:



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

# 0 ~ T-1分までに食べ終わる時、i番目の品まで選べる場合の最大満足度
dp = [[0 for _ in range(T)] for _ in range(N+1)]

for i in range(N):
    for j in range(T):
        # 時間までに食べきれなくて選べない場合
        if AB[i][0] > j:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j], dp[i][j - AB[i][0]] + AB[i][1])


# T分目のタイミングで、残りの中から最も満足度の高い物を食べる
ans = 0
for i in range(N):
    tmp = 0
    if i < N-1:
        for j in range(i+1,N):
            tmp = max(tmp, AB[j][1]) 
    ans = max(ans, dp[i+1][-1] + tmp)

print(ans)

Submission status: Accepted

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

dp0 = {0:0}
dp1 = {0:0}

for i,(a,b) in enumerate(AB):
    _dp0 = {0:0}
    _dp1 = {0:0}
    for k0,v0 in dp0.items():
        if k0 in _dp0:
            _dp0[k0] = max(_dp0[k0], v0)
        else:
            _dp0[k0] = v0
        if k0+a < T:
            if k0+a in _dp0:
                _dp0[k0+a] = max(_dp0[k0+a], v0+b)
            else:
                _dp0[k0+a] = v0+b
        if k0 in _dp1:
            _dp1[k0] = max(_dp1[k0], v0+b)
        else:
            _dp1[k0] = v0+b
    for k1,v1 in dp1.items():
        if k1 in _dp1:
            _dp1[k1] = max(_dp1[k1], v1)
        else:
            _dp1[k1] = v1
        if k1+a < T:
            if k1+a in _dp1:
                _dp1[k1+a] = max(_dp1[k1+a], v1+b)
            else:
                _dp1[k1+a] = v1+b
    dp0 = _dp0
    dp1 = _dp1

print(max(dp1.values()))

Submission status: Accepted

Solution code:
import numpy as np
n,t = map(int,input().split())
l = [list(map(int,input().split())) for i in range(n)]
l.sort()
dp = np.array([0]*t)
ans = 0
for a,b in l:
    ans = max(ans,dp[-1]+b)
    dp[a:] = np.maximum(dp[a:],dp[:-a]+b)
    
    
print(max(ans,dp[-1]))

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
N,T = [int(x) for x in input().split()]
AB = [[int(x) for x in input().split()] for _ in range(N)]
AB = sorted(AB)
#print(AB)
j = 0
ans=0
dp = [[0]*(T+1) for _ in range(N+1)]
for i in range(N):
  for t in range(T+1):
    dp[i+1][t]=max(dp[i+1][t],dp[i][t])
    if t<=T-1:
      dp[i+1][min(T,t+AB[i][0])]=max(dp[i+1][min(T,t+AB[i][0])],dp[i][t]+AB[i][1])
print(dp[N][T])

Submission status: Accepted

Solution code:
N,T = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
A.insert(0,[-1,-1])
dp = [[0 for _ in range(T+1)] for _ in range(N)]
for i in range(1,N):
    for j in range(1,T+1):
        dp[i][j] = dp[i-1][j]
        if j>A[i][0]:
            dp[i][j] = max(dp[i][j],dp[i-1][j-A[i][0]]+A[i][1])
dmax = 0
for i in range(1,N+1):
    dmax = max(dmax,dp[i-1][T]+A[i][1])
print(dmax)

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l.sort()
dp=[[0]*(t+1) for i in range(n+1)]
for i in range(1,n+1):
  for j in range(t+1):
    if j!=t:
      if j-l[i-1][0]>=0:
        dp[i][j]=max(dp[i-1][j],dp[i-1][j-l[i-1][0]]+l[i-1][1])
    else:
      dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+l[i-1][1])
    dp[i][j]=max(dp[i-1][j],dp[i][j])
print(max(dp[n]))

Submission status: Accepted

Solution code:
import numpy as np
N,T = map(int,input().split())
AB = []
for _ in range(N):
    AB.append(list(map(int,input().split())))
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:
N,T = map(int,input().split())
food = [list(map(int,input().split())) for i in range(N)]

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

for i in range(1,N+1):
    time,deliciousness = food[i-1]
    for t in range(1,T+1):
        if time <= t:
            dp1[i][t] = max(dp1[i-1][t],dp1[i-1][t-time]+deliciousness)
        else:
            dp1[i][t] = dp1[i-1][t]
            
dp2 = [[0 for t in range(T+1)] for i in range(N+1)]

for i in range(N):
    time,deliciousness = food[N-i-1]
    for t in range(1,T+1):
        if time <= t:
            dp2[N-i-1][t] = max(dp2[N-i][t],dp2[N-i][t-time]+deliciousness)
        else:
            dp2[N-i-1][t] = dp2[N-i][t]

ans = 0

for t in range(T):
    for i in range(1,N+1):
        ans = max(ans,dp1[i-1][t]+dp2[i][T-1-t]+food[i-1][1])
        
print(ans)