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

Solution (Python):
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():
    N, T = map(int, input().split())
    AB = sorted([list(map(int, input().split())) for _ in range(N)])
    A = [0] * (N + 1)
    B = [0] * (N + 1)
    for i in range(N):
        A[i + 1], B[i + 1] = AB[i]

    a_max = max(A)
    t_max = T - 1 + a_max
    dp = [[0 for _ in range(t_max + 1)] for _ in range(N + 1)]

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

    ans = 0
    for d in dp:
        ans = max(ans, max(d))

    print(ans)

    
    
    
    
    
    
    


if __name__ == '__main__':
    solve()

Solution (Python):
n,t=map(int,input().split())
F=[list(map(int,input().split())) for i in range(n)]
dp1=[[0]*(t+1) for i in range(n)]
for i in range(1,n):
  for j in range(1,t+1):
    if j-F[i-1][0]>=0:
      dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1],dp1[i-1][j-F[i-1][0]]+F[i-1][1])
    else:
      dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1])
dp2=[[0]*(t+1) for i in range(n)]
for i in range(1,n):
  for j in range(1,t+1):
    if j-F[n-i][0]>=0:
      dp2[i][j]=max(dp2[i-1][j],dp2[i][j-1],dp2[i-1][j-F[n-i][0]]+F[n-i][1])
    else:
      dp2[i][j]=max(dp2[i-1][j],dp2[i][j-1])
ans=0
for i in range(n):
  for j in range(1,t):
    a=dp1[i][j]+dp2[n-i-1][t-j-1]+F[i][1]
    if a>ans:
      ans=a
print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,T,*ab = map(int, read().split())

    AB = []
    for a, b in zip(*[iter(ab)]*2):
        AB.append((a, b))

    AB.sort()
    
    dp = [[0] * (T+1) for _ in range(N+1)]

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

if __name__ == "__main__":
    main()

Solution (Python):







N,T=map(int,input().split())
d=[]
for i in range(N):
    a,b=map(int,input().split())
    d.append((a,b))
dpl=[[0 for i in range(T)] for j in range(N+1)]
for i in range(N):
    weight,value=d[i]
    for j in range(T):
        if j>=weight:
            dpl[i+1][j]=max(dpl[i][j],dpl[i][j-weight]+value)
        else:
            dpl[i+1][j]=dpl[i][j]

            

dpr=[[0 for i in range(T)] for j in range(N+2)]
for i in range(1,N+1):
    weight,value=d[-i]
    for j in range(T):
        if j>=weight:
            dpr[N+1-i][j]=max(dpr[N+2-i][j],dpr[N+2-i][j-weight]+value)
        else:
            dpr[N+1-i][j]=dpr[N+2-i][j]


for i in range(1,N+1):
    for j in range(1,T):
        dpr[i][j]=max(dpr[i][j],dpr[i][j-1])
    
ans=0
for i in range(N):
    sub=d[i][1]
    
    add=0
    for j in range(T):
        add=max(add,dpl[i][j]+dpr[i+2][T-1-j])
    sub+=add
    ans=max(sub,ans)
print(ans)

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

dp=[[0]*(T+1) for _ in range(N+1)]
AB.sort()
for i in range(1,N+1):
  dp[i]=dp[i-1][:]
  time=AB[i-1][0]
  happy=AB[i-1][1]
  for j in range(T):
    dp[i][min(j+time,T)]=max(dp[i][min(j+time,T)],dp[i-1][j]+happy)
print(dp[N][T])

Solution (Python):
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)
dp=[[0,0] for i in range(T)]

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

Solution (Python):


def solve():
    N, T = map(int, input().split())
    X = [list(map(int, input().split())) for _ in range(N)]
    X.sort()
    dp = [[0]*(T+3000) for _ in range(N+1)]
    for n in range(1,N+1):
        for t in range(1,T+X[n-1][0]):
            dp[n][t]=dp[n-1][t]
            if t>=X[n-1][0]:
                dp[n][t]=max(dp[n][t],dp[n-1][t-X[n-1][0]]+X[n-1][1])
    ans = max(dp[N])
    return ans
print(solve())

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,T = LI()
    A = [LI() for _ in range(N)]
    A.sort()
    dp = [[0] * (T+1) for _ in range(N+1)]

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from operator import itemgetter

    n, t = list(map(int, readline().split()))
    mat = [list(map(int, readline().split())) for _ in range(n)]
    dp1 = [[0] * t for _ in range(n + 2)]
    dp2 = [[0] * t for _ in range(n + 2)]

    for i, vec in enumerate(mat):
        a = vec[0]
        b = vec[1]
        for time in range(t):
            if time + a < t:
                dp1[i + 1][time + a] = max(dp1[i][time + a], dp1[i][time] + b)
            dp1[i + 1][time] = max(dp1[i][time], dp1[i + 1][time])

    for j, vec in enumerate(mat[::-1]):
        i = n + 1 - j
        a = vec[0]
        b = vec[1]
        for time in range(t):
            if time + a < t:
                dp2[i - 1][time + a] = max(dp2[i][time + a], dp2[i][time] + b)
            dp2[i - 1][time] = max(dp2[i][time], dp2[i - 1][time])

    ans = 0
    for i, vec in enumerate(mat, 1):
        val = vec[1]
        for j in range(t):
            temp = dp1[i - 1][j] + dp2[i + 1][t - j - 1] + val
            ans = max(ans, temp)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from operator import itemgetter

    n, t = list(map(int, readline().split()))
    mat = [list(map(int, readline().split())) for _ in range(n)]
    mat.sort(key=itemgetter(0))
    dp = [[0] * t for _ in range(n + 1)]
    ans = 0

    for dish, vec in enumerate(mat):
        a = vec[0]
        b = vec[1]
        for time in range(t):
            dp[dish + 1][time] = max(dp[dish + 1][time], dp[dish][time])
            if time + a >= t:
                ans = max(ans, dp[dish][time] + b)
            else:
                dp[dish + 1][time + a] = max(dp[dish + 1][time + a], dp[dish][time] + b)

    ans = max(max(dp[n]), ans)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
n,t = map(int,readline().split())
f = []
m = 0
for i in range(n):
  p,q = map(int,readline().split())
  m = max(m,p)
  f.append((p,q))
  
f.sort()
dp = [[0]*(t+m) for i in range(n+1)]

for i,c in enumerate(f):
  a,b = c
  for j in range(t+m):
    if j-a >= t:
      break
    if j < a :
      dp[i+1][j] = dp[i][j]
    else:
      dp[i+1][j] = max(dp[i][j],dp[i][j-a]+b)
      
print(max(dp[n]))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
n,t = map(int,readline().split())
f = []
m = 0
for i in range(n):
  p,q = map(int,readline().split())
  m = max(m,p)
  f.append((p,q))
  
f.sort()
dp = [[0]*(t+m) for i in range(n+1)]

for i,c in enumerate(f):
  a,b = c
  for j in range(t+m):
    if j < a or j-a >= t:
      dp[i+1][j] = dp[i][j]
    else:
      dp[i+1][j] = max(dp[i][j],dp[i][j-a]+b)
      
print(max(dp[n]))

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

menu = []
for _ in range(N):
    m = tuple(map(int, input().split()))
    menu.append(m)
menu = sorted(menu, key=lambda m: m[0])


dp = np.zeros((N, T), dtype='int64')
anss = []

for i in range(1, N):
    
    ai, bi = menu[i-1]
    dp[i][:ai] = dp[i-1][:ai]  
    dp[i][ai:] = np.fmax(
        dp[i-1][ai:], 
        dp[i-1][:-ai] + bi 
    )
    
    anss.append(dp[i][-1] + menu[i][1])

print(max(anss))

Solution (Python):



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


"""
最後に食べる料理は時刻Tを超えても食べていてOKなので、時刻Tの時点で、食べるのに必要な時間が長くても得点の高いものを食べればよい
最後に食べるやつ x N-1品からi番目まで選べる時 x T-1分までのうちt分までに食べ終えて得られる最大のおいしさ
でDPをやると、O(N*N*T)で間に合わない

↑は最後に食べるものを軸に考えたDPだが、実際の所、その後ろの「N-1品～T-1までに食べ終えて得られる最大のおいしさ」を軸にしてみると、
この「」の後に食べる候補で全体の美味しさを最大化できるのは、残りの中で最も美味しいものである。
なので、最後に食べるもののループは実際は必要ない
これでO(N*T)程度になるので間に合う
ただし、食べる時間が速い順に並べおく必要がある。（先にT-1までに食べ終えておくものの中で、最後に食べるのより時間がかかるものがあるなら、入れ替えたほうがよいケースが出てくるので）
"""


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

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)

Solution (Python):
N, T = map(int, input().split())
table = [[0] * (N + 1) for _ in range(T + 1)]
dish = sorted((tuple(map(int, input().split())) for _ in range(N)), key=lambda t: t[0])
for t in range(T + 1):
    for n in range(N):
        minutes_to_take, deliciousness = dish[n]
        now = table[t][n]
        if t < T:
            table[min(T, t + minutes_to_take)][n + 1] = max(table[min(T, t + minutes_to_take)][n + 1], now + deliciousness)
        if n < N:
            table[t][n + 1] = max(table[t][n + 1], now)
print(table[T][N])

Solution (Python):
N, T = map(int, input().split())
table = [[0] * (N + 1) for _ in range(T + 1)]
dish = sorted((tuple(map(int, input().split())) for _ in range(N)), key=lambda t: t[0])
for t in range(T + 1):
    for n in range(N):
        minutes_to_take, deliciousness = dish[n]
        now = table[t][n]
        if t < T:
            table[min(T, t + minutes_to_take)][n + 1] = max(table[min(T, t + minutes_to_take)][n + 1], now + deliciousness)
        if n < N:
            table[t][n + 1] = max(table[t][n + 1], now)
print(max(table[t][N] for t in range(T + 1)))

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

Dp = [0 for _ in range(T+1)]

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

for i in range(N):
    for j in range(T-1,-1, -1):
        if j+ab[i][0] <= T-1:
            kari2 = Dp[j] + ab[i][1]
            if  Dp[j+ab[i][0]] < kari2:
                Dp[j+ab[i][0]] = kari2
        else:
            kari2 = Dp[j] +ab[i][1]
            if Dp[T] < kari2:
                Dp[T] = kari2
                
print(max(Dp))

Solution (Python):
N, T = map(int, input().split())
AB= []
for _ in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort(key=lambda x: x[1], reverse=True)
AB.sort()

A = [x[0] for x in AB]
B = [x[1] for x in AB]

dp = [[0] * (T + 3) for _ in range(N + 3)]
dp[0][0] = 0
for i in range(N):
    for j in range(T):
        if A[i] <= j:
            dp[i + 1][j] = max(dp[i][j], dp[i][j - A[i]] + B[i])
        else:
            dp[i + 1][j] = max(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)

Solution (Python):
n,t = map(int,input().split())
dishes = []
for i in range(n):
    a,b = map(int,input().split())
    dishes.append((a,b))
dp1 = [[0]*t for _ in range(n+2)]
dp2 = [[0]*t for _ in range(n+2)]
for i in range(n):
    a,b = dishes[i]
    for j in range(t):
        if j-a >= 0:
            dp1[i+1][j] = max(dp1[i][j],dp1[i][j-a]+b)
        else:
            dp1[i+1][j] = dp1[i][j]
for i in range(2,n+2)[::-1]:
    a,b = dishes[i-2]
    for j in range(t):
        if j-a >= 0:
            dp2[i-1][j] = max(dp2[i][j],dp2[i][j-a]+b)
        else:
            dp2[i-1][j] = dp2[i][j]
ans = 0
for i in range(n):
    a,b = dishes[i]
    for j in range(t):
        ans = max(ans,dp1[i][j]+dp2[i+2][t-1-j]+b)
print(ans)

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

Solution (Python):
n,t = map(int,input().split())
orders = []
for i in range(n):
    a,b = map(int,input().split())
    orders.append([a,b])

orders.sort(key=lambda x:(x[0],-x[1]))


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

for i,od in enumerate(orders):
    for j in range(t+1):
        dp[i+1][j] = dp[i][j]
    for j in range(t):
        nt = j+od[0]
        if nt > t-1:
            nt = t
        if dp[i+1][nt] < dp[i][j]+od[1]:
            dp[i+1][nt] = dp[i][j]+od[1]




print(max(dp[-1]))

Solution (Python):
n, t = map(int, input().split())
a, b, ab = [], [], []
for _ in range(n):
    ab.append(tuple(map(int, input().split())))
ab.sort(key=lambda x:x[0])
for i in ab:
    a.append(i[0])
    b.append(i[1])



dp = [[0 for _ in range(min([t, a[0]]))] + [b[0] for _ in range(t-a[0])]]
for i in range(1, n-1):
    dp.append([])
    for j in range(min([t, a[i]])):
        dp[i].append(dp[i-1][j])
    for j in range(a[i], t):
        dp[i].append(max([dp[i-1][j], dp[i-1][j-a[i]]+b[i]]))

ans = []
for i in range(1, n):
    ans.append(dp[i-1][t-1] + b[i])
print(max(ans))

Solution (Python):
n, t = map(int, input().split())
a, b, ab = [], [], []
for _ in range(n):
    ab.append(tuple(map(int, input().split())))
ab.sort(key=lambda x:x[0])
for i in ab:
    a.append(i[0])
    b.append(i[1])



dp = [[0 for _ in range(min([t, a[0]]))] + [b[0] for _ in range(t-a[0])]]
for i in range(1, n):
    dp.append([])
    for j in range(min([t, a[i]])):
        dp[i].append(dp[i-1][j])
    for j in range(a[i], t):
        dp[i].append(max([dp[i-1][j], dp[i-1][j-a[i]]+b[i]]))

ans = []
for i in range(1, n):
    ans.append(dp[i-1][t-1] + b[i])
print(max(ans))

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

print(ans)

Solution (Python):
import numpy as np
 
N, T = map(int, input().split())
data = []
for i in range(N):
  a, b = map(int, input().split())
  data.append((a,b))
data.sort()
data = np.array(data)

dp = np.zeros(T)
ans = 0
for a, b in data:
    ans = max(ans,dp[-1]+b)
    if a< T:
        newtable = dp[:T-a] + b
        dp[a:] = np.maximum(dp[a:], newtable)

print(int(ans))

Solution (Python):


import sys





















def main():
    mod = 1000000007                
    inf = float('inf')              
    inf = 2 ** 64 - 1               
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, t = mi()
    eating_cost = [None]    
    energy = [None]    
    for _ in range(n):
        a, b = mi()
        eating_cost.append(a)
        energy.append(b)
    
    
    dp_cost_unitl_i = [[0] * (n + 2) for _ in range(t)]
    for time_cost in range(1, t):
        for i in range(1, n + 1):
            tmp = energy[i] + dp_cost_unitl_i[time_cost - eating_cost[i]][i - 1] if time_cost - eating_cost[i] >= 0 else 0
            dp_cost_unitl_i[time_cost][i] = max(dp_cost_unitl_i[time_cost][i - 1], tmp)
    
    dp_cost_from_i = [[0] * (n + 2) for _ in range(t)]
    for time_cost in range(1, t):
        for i in range(n, 0, -1):
            tmp = energy[i] + dp_cost_from_i[time_cost - eating_cost[i]][i + 1] if time_cost - eating_cost[i] >= 0 else 0
            dp_cost_from_i[time_cost][i] = max(dp_cost_from_i[time_cost][i + 1], tmp)
    
    maximum_energy = 0
    for i in range(1, n + 1):
        
        for until_i_time in range(t):
            a = dp_cost_unitl_i[until_i_time][i - 1]
            b = dp_cost_from_i[t - 1 - until_i_time][i + 1]
            maximum_energy = max(maximum_energy, energy[i] + a + b)
    
    print(maximum_energy)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
n,t=map(int,input().split())
info=[tuple(reversed(list(map(int,input().split())))) for i in range(n)]


dp=[(-10**9,-10**9)]*t
dp[0]=(0,0)

for _ in reversed(sorted(info)):
    good,time=_
    for i in range(t-1,-1,-1):
        g0,m0=dp[i]
        dp[i]=(g0,max(m0,good))
        if i>=time:
            g1,m1=dp[i-time]
            if g0+m0<g1+good+m1:
                dp[i]=(g1+good,m1)
ans=0
for a,b in dp:
    ans=max(ans,a+b)

print(ans)

Solution (Python):
n,t = map(int,input().split())
L = []
for i in range(n):
    a,b = map(int,input().split())
    L.append([a,b])
dp1 = [[0 for i in range(t)] for i in range(n+1)]
dp2 = [[0 for i in range(t)] for i in range(n+1)]
for i in range(n):
    a = L[i][0]
    b = L[i][1]
    for j in range(t):
        if j-a >= 0:
            dp1[i+1][j] = max(dp1[i][j], dp1[i][j-a]+b, dp1[i+1][j])
        else:
            dp1[i+1][j] = max(dp1[i][j],dp1[i+1][j])
for i in range(n):
    a = L[n-i-1][0]
    b = L[n-i-1][1]
    if i == 0:
        for j in range(a, t):
            dp2[n-i-1][j] = b
    else:
        for j in range(t):
            if j-a >= 0:
                dp2[n-i-1][j] = max(dp2[n-i][j], dp2[n-i][j-a]+b,dp2[n-i-1][j])
            else:
                dp2[n-i-1][j] = max(dp2[n-i-1][j], dp2[n-i][j])
ans = 0
for i in range(1,n+1):
    for j in range(t):
        ans = max(ans, dp1[i-1][j]+dp2[i][t-1-j]+L[i-1][1])
print(ans)

Solution (Python):
n,t = map(int,input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
dp = [[0 for i in range(t+1)]for j in range(n+1)]
for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])
ma = 0
for i in range(n):
    ma = max(ma,dp[i][t-1]+ab[i][1])
print(ma)

Solution (Python):
n,t = map(int,input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
dp = [[0 for i in range(t+1)]for j in range(n+1)]
for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])
ma = 0
for i in range(n):
    ma = max(ma,dp[i][t-1]+ab[i][1])
print(ma)

Solution (Python):
n,t = map(int,input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])

ma = 0
for i,(ti,vi) in enumerate(ab):
    ma = max(ma,dp[i][t-1]+vi)
print(ma)

Solution (Python):
n,t = map(int,input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])

ma = 0
for i,(ti,vi) in enumerate(ab):
    ma = max(ma,dp[i][t-1]+vi)
print(ma)

Solution (Python):
n,t = map(int,input().split())
ab = sorted([list(map(int, input().split())) for i in range(n)])
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])

ans = 0
for i, (ti, vi) in enumerate(ab):
    ans = max(ans, dp[i][t - 1] + vi)
print(ans)

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

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


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)

Solution (Python):
n,t = map(int, input().split())

x = [list(map(int, input().split())) for i in range(n)]
x.sort()


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


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

Solution (Python):
N,T = map(int,input().split())
item=sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])

dp=[0]*(T+1)

for a,b in item:
    for t in reversed(range(T)):
        dist=min(T,t+a)
        dp[dist] = max(dp[t]+b,dp[dist])
print(max(dp))

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

item=[list(map(int,input().split())) for _ in range(N)]
item = sorted(item,key=lambda x:x[0])

dp=[0]*(T+1)

for a,b in item:
    for t in reversed(range(T)):
        dist=min(T,t+a)
        dp[dist] = max(dp[t]+b,dp[dist])
print(max(dp))

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

item=[list(map(int,input().split())) for _ in range(N)]
item = sorted(item,key=lambda x:x[0])

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

for i,(a,b) in  enumerate(item,1):
    for t in reversed(range(T)):
        dist=min(T,t+a)
        dp[i][dist] = max(dp[i-1][t]+b,dp[i][dist])
        dp[i][t]=max(dp[i-1][t],dp[i-1][t])
ans=0
for l in dp:
    ans=max(ans,max(l))
print(ans)

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

item=[list(map(int,input().split())) for _ in range(N)]
item = sorted(item,key=lambda x:x[0])

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

for i,(a,b) in  enumerate(item,1):
    for t in reversed(range(T)):
        dist=t+a
        dp[i][dist] = max(dp[i-1][t]+b,dp[i][dist])
        dp[i][t]=max(dp[i-1][t],dp[i-1][t])
ans=0
for l in dp:
    ans=max(ans,max(l))
print(ans)

Solution (Python):
from itertools import accumulate

n,t = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
dp1 = [[-(10 ** 18)] * t for i in range(n)]
dp2 = [[-(10 ** 18)] * t for i in range(n)]
dp1[0][0] = 0
dp2[0][0] = 0
for i,(a,b) in enumerate(ab[:-1]):
  for j in range(t):
    dp1[i+1][j] = max(dp1[i+1][j],dp1[i][j])
    if j < t-a:
      dp1[i+1][j+a] = max(dp1[i+1][j+a],dp1[i][j]+b)
for i,(a,b) in enumerate(ab[n-1:0:-1]):
  for j in range(t):
    dp2[i+1][j] = max(dp2[i+1][j],dp2[i][j])
    if j < t-a:
      dp2[i+1][j+a] = max(dp2[i+1][j+a],dp2[i][j]+b)
ans = 0
for key,(i,j,(a,b)) in enumerate(zip(dp1,dp2[::-1],ab)):
  nowi,nowj = 0,0
  I,J = [],[]
  for x,y in zip(i,j):
    nowi = max(nowi,x)
    nowj = max(nowj,y)
    I.append(nowi)
    J.append(nowj)
  J.reverse()
  tmp = 0
  for x,y in zip(I,J):
    tmp = max(tmp,x+y)
  tmp += b
  ans = max(ans,tmp)
print(ans)

Solution (Python):
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 for _ in range(N-1)]

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

ans = 0
tmp = 0

for i in reversed(range(N-1)):
	tmp = max(tmp, AB[i+1][1])
	ans = max(ans, DP[i][T-1] + tmp)

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline   

d={i:0 for i in range(3001)}
db={i:0 for i in range(3001)}

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

x=[tuple(map(int,input().split()))for i in n*[0]]
x.sort()
dp=[]
dp2=[]
for time,value in x:
    for k,v in d.copy().items():
        if k+time<t:
            if d[k+time]<v+value:
                d[k+time]=v+value
        else:
            break
    l=[0]
    for i in range(1,t+1):
        if d[i]>l[-1]:
            l.append(d[i])
        else:
            l.append(l[-1])

    dp.append(l)    
        
for time,value in x[::-1]:
    for k,v in db.copy().items():
        if k+time<t:
            if db[k+time]<v+value:
                db[k+time]=v+value
        else:
            break

    l=[0]
    for i in range(1,t+1):
        if db[i]>l[-1]:
            l.append(db[i])
        else:
            l.append(l[-1])
        
    dp2.append(l)
dp=[[0]*t,[0]*t]+dp
dp2=([[0]*t,[0]*t]+dp2)[::-1]

ans=0

for i in range(len(dp)):
    for j in range(t):
        if i==0 or i==len(dp)-1:
            tmp=dp[i][j]+dp2[i][t-j-1]
            if ans<tmp:
                ans=tmp
        else:
            tmp=dp[i][j]+dp2[i][t-j-1]+x[i-1][1]
            if tmp>ans:
                ans=tmp
print(ans)

Solution (Python):
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 i in range(T - 1, -1, -1):
            if dp[i] == -1:
                continue
            if dp[i + a] < dp[i] + b:
                dp[i + a] = dp[i] + b
    print(max(dp))


main()

Solution (Python):
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
    c = 0
    for a, b in sorted(AB):
        for j in range(c, -1, -1):
            if dp[j] == -1:
                continue
            t = dp[j] + b
            if dp[j + a] < t:
                dp[j + a] = t
        c = min(c + a, T - 1)
    print(max(dp))


main()

Solution (Python):
import numpy as np

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



dp = np.zeros(T)
ans = 0
for a, b in data:
  ans = max(ans, dp[-1]+b)
  if a < T:
    newtable = dp[:T-a] + b
    dp[a:] = np.maximum(dp[a:], newtable)
print(int(ans))

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,t = mi()
data = []
for i in range(n):
    data.append(li())

data.sort()

dp = [[0]*t for i in range(n+1)]
for i in range(n):
    for k in range(t):
        dp[i+1][k] = max(dp[i+1][k],dp[i][k])
        if k + data[i][0] <= t-1:
            dp[i+1][k+data[i][0]] = max(dp[i+1][k+data[i][0]],dp[i][k] + data[i][1])
ans = 0
for i in range(n):
    ans = max(ans,dp[i][t-1] + data[i][1])
print(ans)

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

dp = [0]*(t+1)

for a, b in ab:
    tmp = dp[:]
    for i in range(t):
        if dp[i]+b > tmp[min(i+a, t)]:
            tmp[min(i+a, t)] = dp[i]+b
    dp = tmp
print(dp[-1])

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

dp = [-1]*(t+1)
dp[0] = 0

for a, b in ab:
    tmp = dp[:]
    for i in range(t):
        if dp[i] >= 0 and dp[i]+b > tmp[min(i+a, t)]:
            tmp[min(i+a, t)] = dp[i]+b
    dp = tmp
print(max(dp))