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):
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):
        
        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):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, T = MAP()
L = [LIST() for i in range(N)]

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

L.sort()
for i in range(N):
    for j in range(T):
        if j - L[i][0] >= 0:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j-L[i][0]]+L[i][1])
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])


ans = 0
for k in range(N):
    
    
    
    ans = max(dp[k][T-1] + L[k][1], ans)
print(ans)

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

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, T = MAP()
A = []
B = []
L = [LIST() for i in range(N)]



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





L.sort()
for a, b in L:
    A.append(a)
    B.append(b)

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

ans = 0

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




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

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

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

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

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

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

ab.sort()

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

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

print(ans)

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

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


main()

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


main()

Solution (Python):
def solve_145e():
    from collections import namedtuple
    from operator import attrgetter
    import sys
    input = sys.stdin.readline

    Food = namedtuple('Food', 'time_to_eat deliciousness')

    N, T = map(int, input().split())
    foods = []
    for _ in range(N):
        row = map(int, input().split())
        foods.append(Food(*row))
    foods.sort(key=attrgetter('time_to_eat'))
    

    dp = [-1] * T
    dp[0] = 0
    

    ma = 0  
    ca = 0  
    for food in foods:
        
        ma = max(ma, ca + food.deliciousness)

        
        for time in range(T - 1 - food.time_to_eat, -1, -1):
            if ~dp[time]:
                when_eat = dp[time] + food.deliciousness
                if dp[time + food.time_to_eat] < when_eat:
                    dp[time + food.time_to_eat] = when_eat
                    ca = max(ca, when_eat)

    print(ma)
    return


if __name__ == '__main__':
    solve_145e()

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

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

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

Solution (Python):
def main():
    n,t=map(int,input().split())
    F=[tuple(map(int,input().split())) for _ in range(n)]

    dp1=[[0]*t for _ in range(1+n)]
    dp2=[[0]*t for _ in range(1+n)]
    for i in range(n):
        a1=F[i][0]
        b1=F[i][1]
        a2=F[n-i-1][0]
        b2=F[n-i-1][1]
        for j in range(t):
            if j<a1:
                dp1[i+1][j]=dp1[i][j]
            else:
                dp1[i+1][j]=max(dp1[i][j],dp1[i][j-a1]+b1)
            
            if j<a2:
                dp2[i+1][j]=dp2[i][j]
            else:
                dp2[i+1][j]=max(dp2[i][j],dp2[i][j-a2]+b2)
    
    ans=0
    for i in range(n):
        b=F[i][1]
        for j in range(t):
            ans=max(dp1[i][j]+dp2[n-i-1][t-1-j]+b,ans)
    print(ans)          
  
if __name__=='__main__':
    main()

Solution (Python):



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)

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

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

AB.sort(reverse=True)
dp = [0] * (T + 1)

for a, b in AB:
    for t in range(T):
        x = dp[t+a]+b if t+a < T else b
        dp[t] = max(dp[t], x)

ans = dp[0]
print(ans)

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

Solution (Python):

import sys

def solve(N: int, T: int, A: "List[int]", B: "List[int]"):
    dp = [0] * (T + 1)
    for a, b in sorted(zip(A, B), reverse=True):
        for t in range(T):
            dp[t] = max(dp[t],
                        b + (dp[t+a] if t + a < T else 0))
    return max(dp)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    T = int(next(tokens))  
    A = [int()] * (N)  
    B = [int()] * (N)  
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    print(solve(N, T, A, B))

if __name__ == '__main__':
    main()

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

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

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

Solution (Python):


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

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

l.sort()

dp=[-1]*(6001)
dp[0]=0

for cand in l:
    for i in range(T)[::-1]:
        if dp[i]!=-1:
            dp[i+cand[0]]=max(dp[i+cand[0]],dp[i]+cand[1])
print(max(dp))

Solution (Python):
N,T = map(int,input().split())
data = [0] * N

M = 0
for i in range(N):
    data[i] = list(map(int,input().split()))
    M = max(M,data[i][0])

dp1 = [[0]*T for i in range(N)]

for j in range(T):
    if j < data[0][0]:
        dp1[0][j] = 0
    else:
        dp1[0][j] = data[0][1]

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

dp2 = [[0]*T for i in range(N)]

for j in range(T):
    if j < data[N-1][0]:
        dp2[N-i][j] = 0
    else:
        dp2[N-1][j] = data[N-1][1]

for j in range(T):
    for i in range(N-2,-1,-1):
        if data[i][0] <= j:
            dp2[i][j] = max(dp2[i+1][j],dp2[i+1][j-data[i][0]]+data[i][1])
        else:
            dp2[i][j] = dp2[i+1][j]

m = 0

for j in range(T):
    for i in range(N):
        if i == 0:
            m = max(m,dp2[i+1][T-1-j] + data[i][1])
        elif i == N-1:
            m = max(m,dp1[i-1][j] + data[i][1])
        else:
            m = max(m,dp1[i-1][j] + dp2[i+1][T-1-j] + data[i][1])

print(m)

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

for _ in range(n):
    time, point = map(int, input().split())
    dishes.append([time, point])

dishes.sort()

DP = [0] * t

candidates = []
for time, point in dishes:
    candidates.append(max(DP) + point)

    for i in range(t-1, time-1, -1):
        DP[i] = max(DP[i], DP[i-time]+point)

print(max(candidates))

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

A = [0]
B = [0]
for _ 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(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)

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



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

Solution (Python):


def main():
    N,T=map(int,input().split())
    A=[0 for i in range(0,N)]
    B=[0 for i in range(0,N)]
    for i in range(0,N):
        A[i],B[i]=map(int,input().split())
    C=[[A[i],B[i]] for i in range(0,N)]
    C.sort()
    B=[C[i][1] for i in range(0,N)]
    A=[C[i][0] for i in range(0,N)]
    dp=[[0 for j in range(0,T+1)] for i in range(0,N+1)]
    for i in range(0,N):
        for j in range(1,T+1):
            if j-A[N-1-i]<0:
                dp[i+1][j]=max(dp[i][j],B[N-1-i])
            else:
                dp[i+1][j]=max(dp[i][j],dp[i][j-A[N-1-i]]+B[N-1-i])
    print(dp[N][T])

if __name__ == '__main__':
    main()

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

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)

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

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

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

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

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

Solution (Python):
import sys

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=[-1 for _ in range(T+max_T)]
	dp[0] = 0
	for i in range(N):
		for j in range(T-1, -1, -1):
			if(dp[j]>=0):
				dp[j+dish[i][0]] = max(dp[j+dish[i][0]], dp[j]+dish[i][1])
	print(max(dp))
main()

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

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

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

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

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

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





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

    
    
    now = 0
    if i != N-1:
        now = dp[i + 1][T - 1] + max(FOOD[i + 1:], key=lambda x: x[1])[1]
        
        
        

    ans = max(ans, now)

    

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

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

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