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
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    dp1 = [[0]*(T+1) for _ in range(N+2)] 
    dp2 = [[0]*(T+1) for _ in range(N+2)] 

    
    for i in range(N):
        cost, value = cv[i]

        
        for now in range(min(T+1, cost)):
            dp1[i+1][now] = dp1[i][now]

        for now in range(cost, T+1):
            old = now - cost
            dp1[i+1][now] = max(dp1[i][now], dp1[i][old] + value)

        
        cost, value = cv[(N-i)-1]
        for now in range(min(T+1, cost)):
            dp2[N-i][now] = dp2[N-(i-1)][now]

        for now in range(cost, T+1):
            old = now - cost
            dp2[N-i][now] = max(dp2[N-(i-1)][now], dp2[N-(i-1)][old] + value)
    
    ans = 0
    for i in range(1, N+1):
        i_value = cv[i-1][1]
        i_max = max([dp1[i-1][j] + dp2[i+1][(T-1)-j] + i_value for j in range(T)])
        ans = max(ans, i_max)
    print(ans)


    

if __name__ == "__main__":
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    cv = sorted(cv, key=lambda x : x[0])
    dp = [[0]*(T+1) for _ in range(N+1)]

    

    ans = 0
    for i in range(N):
        cost, value = cv[i]
        for now in range(min(T+1, cost)):
            dp[i+1][now] = dp[i][now]

        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)
        
        ans = max(ans, dp[i][T-1] + value)

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    cv = sorted(cv, key=lambda x : x[0])

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

    values = [v for c, v in cv]
    max_values = [max(values[i:]) for i in range(N)]
    
    for i in range(N):
        cost, value = cv[i]
        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)

        for j in range(1, T+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i+1][j-1])
        
        for j in range(1, T+1):
            dp_ext[i+1][j] = max(dp[i+1][j], dp[i][j-1] + max_values[i], dp_ext[i][j])

    print(dp_ext[N][T])

if __name__ == "__main__":
    main()

Solution (Python):

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

dp1=[[0]*t for _ in range(n+1)] 
dp2=[[0]*t for _ in range(n+1)] 
arev=a[::-1]
brev=b[::-1]

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

for i in range(1,n+1):
    for j in range(t):
        if j-arev[i-1]>=0:
            dp2[i][j]=max(dp2[i-1][j-arev[i-1]]+brev[i-1],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]+b[i-1])
print(ans)

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'

    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase

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

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

def gcj(t,*x):
    print("Case 
    return

n,t=LI()
N=6001
N=31
dp=[[0]*t for i in range(n+1)]
c=[]
for i in range(n):
    a,b=LI()
    c+=[(a,b)]
c.sort(key=lambda x:x[0]*4000-x[1])
for i in range(n):
    a,b=c[i]
    ans=max(ans,dp[i][-1]+b)
    for j in range(t):
        dp[i+1][j]=max(dp[i+1][j],dp[i][j])
        if j+a<t:
            dp[i+1][j+a]=max(dp[i][j]+b,dp[i][j+a])

print(ans)

Solution (Python):
n, t = map(int, input().split())
dish = [list(map(int, input().split())) for _ in range(n)]
dish.sort(key=lambda x: x[0])
dp = [[0 for _ in range(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])
        nj = j + dish[i][0]
        if nj < t:
            dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + dish[i][1])
    now = dp[i][t- 1] + dish[i][1]
    ans = max(ans, now)

print(ans)

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

dp1 = [[0] * t for _ in range(n+2)]
dp2 = [[0] * t for _ in range(n+2)]

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

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

ans = 0
for i in range(1, n+1):
    for j in range(t):
        total = b[i] +dp1[i-1][j] + dp2[i+1][t-1-j]
        ans = max(ans, total)
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 _ in range(n+2)]
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):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = sorted([inpl() for _ in range(n)])
dp = [[0]*t for _ in range(n+1)]

for i in range(n-1):
    w,v = wv[i]
    for j in range(t):
        if j < w:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j] , dp[i][j-w] + v)
res = 0
for i in range(n):
    tmp = dp[i][t-1] + wv[i][1]
    res = max(res, tmp)
print(res)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = [None] * n
for i in range(n):
    a,b = inpl()
    wv[i] = (a,b)
rwv = wv[::-1]
udp = [[0] * t for _ in range(n)]
ddp = [[0] * t for _ in range(n)]

for i in range(n-1):
    w,v = wv[i]
    for j in range(t):
        if j < w:
            udp[i+1][j] = udp[i][j]
        else:
            udp[i+1][j] = max(udp[i][j], udp[i][j-w] + v)
res = udp[n-1][t-1] + wv[n-1][1]

for i in range(n-1):
    w,v = rwv[i]
    for j in range(t):
        if j < w:
            ddp[i+1][j] = ddp[i][j]
        else:
            ddp[i+1][j] = max(ddp[i][j], ddp[i][j-w] + v)
    res = max(res,ddp[n-1][t-1] + wv[0][1])

for i in range(1,n-1):
    u = i; d = n-i-1
    mx = 0
    for j in range(t):
        tmp = udp[u][j] + ddp[d][t-1-j]
        mx = max(mx, tmp)
    res = max(res, mx + wv[i][1])
print(res)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = [inpl() for _ in range(n)]
rwv = wv[::-1]
udp = [[0] * t for _ in range(n)]
ddp = [[0] * t for _ in range(n)]

for i in range(n-1):
    w,v = wv[i]
    for j in range(t):
        if j < w:
            udp[i+1][j] = udp[i][j]
        else:
            udp[i+1][j] = max(udp[i][j], udp[i][j-w] + v)
res = udp[n-1][t-1] + wv[n-1][1]

for i in range(n-1):
    w,v = rwv[i]
    for j in range(t):
        if j < w:
            ddp[i+1][j] = ddp[i][j]
        else:
            ddp[i+1][j] = max(ddp[i][j], ddp[i][j-w] + v)
    res = max(res,ddp[n-1][t-1] + wv[0][1])

for i in range(1,n-1):
    u = i; d = n-i-1
    mx = 0
    for j in range(t):
        tmp = udp[u][j] + ddp[d][t-1-j]
        mx = max(mx, tmp)
    res = max(res, mx + wv[i][1])
print(res)

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

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

dp = [[0] * 3100 for _ in range(3100)]
ans = 0

for i in range(1, N + 1):
    for j in range(1, T):

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


        if j - dishes[i-1][0] >= 0: 
            dp[i][j] = max(dp[i][j], dp[i-1][j-dishes[i-1][0]] + dishes[i-1][1]) 

print(ans)

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



dpA=[[0]*t for i in range(n+1)]

dpB=[[0]*t for i in range(n+1)]

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

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

ans=0

for i in range(n):
    for j in range(t):
        ans=max(dpA[i][j]+dpB[i+1][t-j-1]+a[i][1],ans)
print(ans)

Solution (Python):
import sys
from collections import deque
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,T = IL()
ab = [IL() for i in range(N)]
ab.sort()

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

Solution (Python):

N,W = list(map(int,input().split()))
vw = [list(map(int,input().split())) for i in range(N)]
vw.sort(key = lambda x:x[0])
value = [i[1] for i in vw]
weight = [i[0] for i in vw]



inf=float("inf")
dp = [[-inf for _ in range(W+1)] for _ in range(N+1)]

dp[0] = [0]*(W+1)
def nap():
    for i in range(N):
        w = 0
        while w <= W:
            if weight[i] <= w:
                dp[i+1][w]=max(dp[i][w-weight[i]]+value[i],dp[i][w])
            else:
                dp[i+1][w]=dp[i][w]
            w+=1
nap()

ret = 0
for i in range(N):
    ret = max(ret,dp[i][W-1]+value[i])
print(ret)

Solution (Python):
N, T = map(int, input().split())
dishes = sorted([tuple(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
"""
dp[i][j]: 0~i番目までの料理でj分以内に完食できる美味しさの合計の最大値
"""
dp = [[0] * 3100 for _ in range(3100)]
ans = 0
for i in range(1, N+1):
    for j in range(1, T):
        dp[i][j] = dp[i-1][j]
        ans = max(ans, dp[i][j] + dishes[i-1][1])
        if j - dishes[i-1][0] >= 0:
            dp[i][j] = max(dp[i][j], dp[i-1][j-dishes[i-1][0]] + dishes[i-1][1])

print(ans)

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

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

DP = [[[0] * 2 for _ in range(T)] for _ in range(2)]

a, b = AB[0]
if a < T:
  DP[0][a][0] = b
DP[0][0][1] = b

t = 1
for i in range(1, N):
  a, b = AB[i]
  if a < T:
    DP[t][a][0] = b
  DP[t][0][1] = b
  for j in range(T):
    DP[t][j][0] = max(DP[t][j][0], DP[1 - t][j][0])
    DP[t][j][1] = max(DP[t][j][1], DP[1 - t][j][1])
    if j + a < T: 
      DP[t][j + a][0] = max(DP[t][j + a][0], DP[1 - t][j][0] + b)
      DP[t][j + a][1] = max(DP[t][j + a][1], DP[1 - t][j][1] + b)
    DP[t][j][1] = max(DP[t][j][1], DP[1 - t][j][1])
    DP[t][j][1] = max(DP[t][j][1], DP[1 - t][j][0] + b)
  t = 1 - t
ans = 0
for i in range(T):
  ans = max(ans, DP[1 - t][i][0])
  ans = max(ans, DP[1 - t][i][1])

print(ans)

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

N, T = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort()
dp0 = [0] * T 
dp1 = [0] * T 
for A, B in AB:
    for j in range(T):
        dp1[j] = max(dp1[j], dp0[j] + B)
    for j in range(A, T)[::-1]:
        dp0[j] = max(dp0[j], dp0[j-A] + B)
print(dp1[-1])

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

    AB.sort()

    
    
    
    dp = [[0] * 3100 for _ in range(3100)]

    dp[0][0] = 0

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

            if j - AB[i - 1][0] >= 0:
                dp[i][j] = max(dp[i][j], dp[i - 1][j - AB[i - 1][0]] + AB[i - 1][1])

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = [inpl() for _ in range(n)]
wv.sort(key = lambda x:x[1],reverse = True)
res = 0
tmp = min(n,5)
for i in range(tmp):
    dp = [[0] * (t+5) for _ in range(n+5)]
    for j in range(n):
        w,v = wv[j]
        for k in range(t):
            if k < w or i == j:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-w] + v)
    
    res = max(dp[n][t-1] + wv[i][1], res)
print(res)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = [inpl() for _ in range(n)]
wv.sort(key = lambda x:x[1],reverse = True)
res = 0
tmp = min(n,4)
for i in range(tmp):
    dp = [[0] * (t+5) for _ in range(n+5)]
    for j in range(n):
        w,v = wv[j]
        for k in range(t):
            if k < w or i == j:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-w] + v)
    
    res = max(dp[n][t-1] + wv[i][1], res)
print(res)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = [inpl() for _ in range(n)]
wv.sort(key = lambda x:x[1],reverse = True)
res = 0
for i in range(min(n,3)):
    dp = [[0] * (t+5) for _ in range(n+5)]
    for j in range(n):
        w,v = wv[j]
        for k in range(t):
            if k < w or i == j:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-w] + v)
    
    res = max(dp[n][t-1] + wv[i][1], res)
print(res)

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

n, t = map(int, input().split())
m = map(int, read().split())
AB = sorted(zip(m, m))
A, B = zip(*AB)
dp = [[0]*t for _ in range(n+1)]
for i, a in enumerate(A[:-1]):
  for j in range(t):
    if j < a:
      dp[i+1][j] = dp[i][j]
    else:
      dp[i+1][j] = max(dp[i][j-a]+B[i], dp[i][j])
ans = 0
maxB = [B[-1]]*n
for i in range(n-2, 0, -1):
  maxB[i] = max(B[i], maxB[i+1])
for i in range(n-1):
  ans = max(ans, dp[i+1][-1] + maxB[i+1])
print(ans)

Solution (Python):
def main():
    from collections import namedtuple
    from operator import attrgetter

    Dish = namedtuple('Dish', 'time deliciousness')

    N, T = map(int, input().split())
    dishes = []
    for _ in range(N):
        a, b = map(int, input().split())
        d = Dish(time=a, deliciousness=b)
        dishes.append(d)

    dishes.sort(key=attrgetter('time'))

    dp = [0] * T
    ma = 0
    ret = 0
    for d in dishes:
        ret = max(ret, ma + d.deliciousness)
        for t in range(T - 1, d.time - 1, -1):
            dp[t] = max(dp[t], dp[t - d.time] + d.deliciousness)
            ma = max(ma, dp[t])
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
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])
    maxA = 0
    for a, b in AB:
        maxA = max(maxA, a)

    dp = [-1]*(T+maxA)
    dp[0] = 0

    for a, b in AB:
        for i in range(T+maxA-1, -1, -1):
            if i >= T:
                if i-a < T and i-a >= 0 and dp[i-a] >= 0:
                    dp[i] = max(dp[i], dp[i-a] + b)

            elif i - a >= 0 and dp[i-a] >= 0:
                dp[i] = max(dp[i], dp[i-a] + b)

    print(max(dp))


main()

Solution (Python):
N,T = map(int, input().split())
data = [list(map(int, input().split())) for i in range(N)]
dp1 = [[0]*(N+1) for _ in range(T+1)]
dp2 = [[0]*(N+1) for _ in range(T+1)]

for t in range(T):
    for n in range(N):
        if t+1-data[n][0]>=0:
            dp1[t+1][n+1] = max(dp1[t+1][n],dp1[t+1-data[n][0]][n]+data[n][1])
            dp2[t+1][n+1] = max(dp2[t+1][n],dp2[t+1-data[n][0]][n]+data[n][1],
                                dp1[t][n]+data[n][1])
        else:
            dp1[t+1][n+1] = dp1[t+1][n]
            dp2[t+1][n+1] = max(dp2[t+1][n],dp1[t][n]+data[n][1])

print(dp2[T][N])

Solution (Python):
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

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 SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    n,t=MI()
    ab=LLI(n)
    ab.sort()
    dp=[-1]*(t+1)
    dp[0]=0
    for a,b in ab:
        for i in range(t-1,-1,-1):
            pre=dp[i]
            if pre==-1:continue
            ni=min(t,i+a)
            dp[ni]=max(dp[ni],pre+b)
    print(max(dp))

main()

Solution (Python):
import sys
input = sys.stdin.readline
N, T = map(int, input().split())
a = []
for _ in range(N):
  w, v = map(int, input().split())
  a.append((w, v))
a.sort()
dp = [[0] * (T + 1) for _ in range(N + 1)]
for i in range(N):
  w, v = a[i]
  for j in range(T):
    if j + w >= T:
      dp[i + 1][T] = max(dp[i + 1][T], dp[i][j] + v)
    else:
      dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)
    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
  dp[i + 1][T] = max(dp[i + 1][T], dp[i][T])
print(max(dp[-1]))

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

ab.sort()

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

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

print(dp[-1][-1])

Solution (Python):
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, T = iip()

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


    for i, ab in enumerate(AB, 1):
        a, b = ab
        for j in range(T):
            dp1[i][j] = dp1[i-1][j]
        for j in reversed(list(range(T))):
            if j+a >= T:
                continue
            dp1[i][j + a] = max(dp1[i][j + a], dp1[i][j] + b)


    for i, ab in enumerate(reversed(AB), 1):
        a, b = ab
        for j in range(T):
            dp2[i][j] = dp2[i-1][j]
        for j in reversed(list(range(T))):
            if j + a >= T:
                continue
            dp2[i][j + a] = max(dp2[i][j + a], dp2[i][j] + b)

    

    mm = 0
    for i in range(N):
        for j in range(T):
            ii1 = i
            ii2 = N-i-1
            ii3 = i

            ij1 = j
            ij2 = T-j-1

            a = dp1[ii1][ij1]
            b = dp2[ii2][ij2]
            c = AB[ii3][1]
            
            
            mm = max(a+b+c, mm)


            


    
    

    print(mm)




def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import numpy as np

n, t = map(int, readline().split())
ab = sorted([tuple(map(int, readline().split())) for i in range(n)])
dp = np.zeros(t, dtype=int)
ans = 0
for a, b in ab:
    ans = max(ans, max(dp) + b)
    if a < t:
        dp[a:] = np.maximum(dp[a:], dp[:-a] + b)
print(ans)

Solution (Python):
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[-1] + b)
    np.maximum(dp[a:], dp[:-a] + b, out=dp[a:])


print(ans)

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

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

dp = np.zeros(T, np.int64)
answer = 0
for a, b in AB:
    answer = max(answer, dp.max() + b)
    prev = dp
    dp[a:] = np.maximum(prev[a:], prev[:-a] + b)

print(answer)

Solution (Python):
ans=0
n,t=map(int,input().split())
food=[]
for i in range(n):
    f=list(map(int,input().split()))
    food.append(f)
food=sorted(food)
dp=[0,0]+[-1]*(t-1)

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

Solution (Python):
def main():
  N,T=map(int,input().split())
  L=[[int(x) for x in input().split()] for _ in range(N)]
  L.sort(key=lambda x:x[0])
  dp=[0]+[-1]*(T+L[-1][0])

  for a,b in L:
    for j in range(T-1,-1,-1):
      if dp[j]>=0 and dp[j+a]<dp[j]+b:
        dp[j+a]=dp[j]+b

  print(max(dp))

if __name__ == '__main__':
  main()

Solution (Python):

n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]



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)

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

Solution (Python):
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):
        
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        
        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)

Solution (Python):
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 = dp = [[0 for _ in range(3005)] for _ in range(3005)]
for i in range(N):
    a, b = AB[i]
    for j in range(T):
        
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        
        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)