Problem:


Score : 500 points

Problem Statement

Takahashi is at an all-you-can-eat restaurant.

The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.

The restaurant has the following rules:

You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.

You cannot order the same kind of dish more than once.

Until you finish eating the dish already served, you cannot order a new dish.

After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.

Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.

What is the maximum possible happiness achieved by making optimal choices?

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.

Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

We cannot order three dishes, in whatever order we place them.

Sample Input 4

10 100
15 23
20 18
13 17
24 12
18 29
19 27
23 21
18 20
27 15
22 25

Sample Output 4

145



Sample input:
2 60
10 10
100 100


Sample output:
110

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:


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)]#i番目までの料理でT分までの注文での満足度最大値
    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()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

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

N, T = lr()
AB = [lr() for _ in range(N)]
AB.sort()
# 最後の注文はAiが選ぶ料理で最大で、T-1分後に注文するとして良い
dp_left = [np.zeros(T, np.int32) for _ in range(N+2)]
dp_right = [np.zeros(T, np.int32) for _ in range(N+2)]

for i in range(1, N+1):
    a, b = AB[i-1]
    dp_left[i][a:] = np.maximum(dp_left[i-1][a:], dp_left[i-1][:-a] + b)

for i in range(N, 0, -1):
    c, d = AB[i-1]
    dp_right[i][c:] = np.maximum(dp_right[i+1][c:], dp_right[i+1][:-c] + d)

temp_ans = 0
for i in range(N):
    a, b = AB[i]
    for j in range(T):
        result = dp_left[i][j] + dp_right[i+2][T-1-j] + b
        if result > temp_ans:
            temp_ans = result

print(temp_ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np


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 = np.zeros((n + 1, t + 1), dtype = int)
for i in range(1, n + 1):
    for j in range(1, t + 1):
        a, b = DISHES[i][0], DISHES[i][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)

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

print(np.max(DP))

Submission status: Time Limit Exceeded

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


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

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

DP = [[0] * (t + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, t + 1):
        a, b = DISHES[i][0], DISHES[i][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):
    happiness_k = DP[k - 1][t - 1] + DISHES[k][1]
    if happiness_k > answer:
        answer = happiness_k

print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

N, T = map(int, input().split())
S = sys.stdin.readlines()
A = []
B = []
for s in S:
    a, b = map(int, s.split())
    A.append(a)
    B.append(b)

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

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]
        if j < A[i - 1]:
            dp2[i][j] = here
        else:
            dp2[i][j] = max(here, dp2[i + 1][j - A[i - 1]] + B[i - 1])

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

print(ans)

Submission status: Time Limit Exceeded

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

from collections import defaultdict
dp0 = defaultdict(lambda: 0)
dp1 = defaultdict(lambda: 0)
dp0[0] = 0
dp1[0] = 0

for _ in range(N):
    a,b = map(int,input().split())
    _dp0 = defaultdict(lambda: 0)
    _dp1 = defaultdict(lambda: 0)
    for k0,v0 in dp0.items():
        _dp0[k0] = max(_dp0[k0], v0)
        if k0+a < T:
            _dp0[k0+a] = max(_dp0[k0+a], v0+b)
        _dp1[k0] = max(_dp1[k0], v0+b)
    for k1,v1 in dp1.items():
        _dp1[k1] = max(_dp1[k1], v1)
        if k1+a < T:
            _dp1[k1+a] = max(_dp1[k1+a], v1+b)
    dp0 = _dp0
    dp1 = _dp1

print(max(dp1.values()))

Submission status: Time Limit Exceeded

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

from collections import defaultdict
dp0 = defaultdict(lambda: 0)
dp1 = defaultdict(lambda: 0)
dp0[0] = 0
dp1[0] = 0

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

print(max(dp1.values()))

Submission status: Time Limit Exceeded

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

dp0 = [{0:0} for i in range(N+1)]
dp1 = [{0:0} for i in range(N+1)]

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

print(max(dp1[-1].values()))

Submission status: Time Limit Exceeded

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

dp = [[[-1,-1] for t in range(T)] for i in range(N+1)]
dp[0][0][0] = 0
maxt = 0
for i,(a,b) in enumerate(AB):
    maxt = min(T-1, maxt + a)
    for t in range(maxt, -1,-1):
        for k in range(2):
            dpitk = dp[i][t][k]
            if dpitk < 0: continue
            if dp[i+1][t][k] < dpitk: dp[i+1][t][k] = dpitk
            if t+a < T:
                if dp[i+1][t+a][k] < dpitk + b: dp[i+1][t+a][k] = dpitk + b
            if k==0:
                if dp[i+1][t][1] < dpitk + b: dp[i+1][t][1] = dpitk + b
print(max(a[1] for a in dp[-1]))

Submission status: Time Limit Exceeded

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

dp = [[[-1,-1] for t in range(T)] for i in range(N+1)]
dp[0][0][0] = 0
maxt = 0
for i,(a,b) in enumerate(AB):
    maxt = min(T-1, maxt + a)
    for t in range(maxt, -1,-1):
        for k in range(2):
            dpitk = dp[i][t][k]
            if dpitk < 0: continue
            if dp[i+1][t][k] < dpitk: dp[i+1][t][k] = dpitk
            if t+a < T:
                if dp[i+1][t+a][k] < dpitk + b: dp[i+1][t+a][k] = dpitk + b
            if k==0:
                if dp[i+1][t][1] < dpitk + b: dp[i+1][t][1] = dpitk + b
print(max(a[1] for a in dp[-1]))

Submission status: Time Limit Exceeded

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

dp = [[[-1,-1] for t in range(T)] for i in range(N+1)]
dp[0][0][0] = 0
for i,(a,b) in enumerate(AB):
    for t in range(T-1,-1,-1):
        for k in range(2):
            dpitk = dp[i][t][k]
            if dpitk < 0: continue
            if dp[i+1][t][k] < dpitk: dp[i+1][t][k] = dpitk
            if t+a < T:
                if dp[i+1][t+a][k] < dpitk + b: dp[i+1][t+a][k] = dpitk + b
            if k==0:
                if dp[i+1][t][1] < dpitk + b: dp[i+1][t][1] = dpitk + b
print(max(a[1] for a in dp[-1]))

Submission status: Time Limit Exceeded

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

dp = [[[-1,-1] for t in range(T)] for i in range(N+1)]
dp[0][0][0] = 0
for i,(a,b) in enumerate(AB):
    for t in range(T-1,-1,-1):
        for k in range(2):
            if dp[i][t][k] < 0: continue
            if dp[i+1][t][k] < dp[i][t][k]: dp[i+1][t][k] = dp[i][t][k]
            if t+a < T:
                if dp[i+1][t+a][k] < dp[i][t][k] + b: dp[i+1][t+a][k] = dp[i][t][k] + b
            if k==0:
                if dp[i+1][t][1] < dp[i][t][0] + b: dp[i+1][t][1] = dp[i][t][0] + b
print(max(a[1] for a in dp[-1]))

Submission status: Time Limit Exceeded

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

dp = [[[0,0] for t in range(T)] for i in range(N+1)]
for i,(a,b) in enumerate(AB):
    for t in range(T-1,-1,-1):
        if dp[i+1][t][0] < dp[i][t][0]: dp[i+1][t][0] = dp[i][t][0]
        if dp[i+1][t][1] < dp[i][t][1]: dp[i+1][t][1] = dp[i][t][1]
        if t+a < T:
            if dp[i+1][t+a][0] < dp[i][t][0] + b: dp[i+1][t+a][0] = dp[i][t][0] + b
            if dp[i+1][t+a][1] < dp[i][t][1] + b: dp[i+1][t+a][1] = dp[i][t][1] + b
        if dp[i+1][t][1] < dp[i][t][0] + b: dp[i+1][t][1] = dp[i][t][0] + b
print(dp[-1][-1][-1])

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ab.sort()

dp = [[0] * t for i in range(n+1)]
ans = 0
for i in range(n):
    for j in range(t):
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        if j - ab[i][0] >= 0:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j - ab[i][0]] + ab[i][1])
    now = dp[i][t-1] + ab[i][1]
    ans = max(ans, now)

print(ans)

Submission status: Time Limit Exceeded

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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, T: int, A: "List[int]", B: "List[int]"):

    # DP1
    # 1からi番目の料理でj分以内に完食できる美味しさの合計の最大値
    DP1 = [[0 for _ in range(T+1)] for __ in range(N+1)]
    for i in range(1, N+1):
        for j in range(1, T+1):
            if j-A[i-1] >= 0:
                DP1[i][j] = max(DP1[i][j], DP1[i-1][j-A[i-1]]+B[i-1])
            DP1[i][j] = max(DP1[i][j], DP1[i-1][j], DP1[i][j-1])

    # DP2
    # i番目からN番目の料理でj分以内に完食できる美味しさの合計の最大値
    DP2 = [[0 for _ in range(T+1)] for __ in range(N+2)]
    for i in range(N, 0, -1):
        for j in range(1, T+1):
            if j-A[i-1] >= 0:
                DP2[i][j] = max(DP2[i][j], DP2[i+1][j-A[i-1]]+B[i-1])
            DP2[i][j] = max(DP2[i][j], DP2[i+1][j], DP2[i][j-1])

    # i番目以外の料理でT-1分以内に完食できる美味しさの合計の最大値
    ans = 0
    for i in range(1, N+1):
        bns = 0
        for j in range(T):
            bns = max(bns, DP1[i-1][j] + DP2[i+1][T-j-1])
        ans = max(ans, bns+B[i-1])
    print(ans)

    return


def main():

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

m, d = 0, [0] * t
for a, b in sorted(ab):
  m = max(m, b+d[t-1])
  for l in range(t-1,a-1,-1):
    if(b+d[l-a] > d[l]):
      d[l] = b+d[l-a]
print(m)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 8)

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

dp = [[[-1] * (t+1) for i in range(n)] for j in range(2)]
def dfs(i, l, d):
  if(i == n or l <= 0):
    return 0
  elif(dp[d][i][l] != -1):
    return dp[d][i][l]
  r = 0
  if(l > 0 and d > 0):
    r = max(r, b[i] + dfs(i+1, l - 1, d - 1))
  if(l >= a[i]):
    r = max(r, b[i] + dfs(i+1, l - a[i], d))
  r = max(r, dfs(i+1, l, d))
  dp[d][i][l] = r
  return dp[d][i][l]

print(dfs(0, t, 1))

Submission status: Time Limit Exceeded

Solution code:
#典型dp
n,t=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
ab.sort(key=lambda x:x[0])
m=ab[-1][0]
dp=[0]*(t+m)

for i in range(n):
    dp_sub=[0]*(t+m)
    for j in range(t):
        if j==0:
            #print(ab[i][1])
            dp_sub[ab[i][0]]=ab[i][1]
        else:
            if dp[j]!=0:
                #print(ab[i][0])
                #print()
                dp_sub[j+ab[i][0]]=dp[j]+ab[i][1]
    for j in range(t+m):
        dp[j]=max(dp[j],dp_sub[j])
print(max(dp))
#print(len(dp))

Submission status: Time Limit Exceeded

Solution code:
#典型dp
n,t=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
ab.sort(key=lambda x:x[0],reverse=True)
m=ab[0][0]
ab.sort(key=lambda x:x[0])
dp=[0]*(t+m)

for i in range(n):
    dp_sub=[0]*(t+m)
    for j in range(t):
        if j==0:
            #print(ab[i][1])
            dp_sub[ab[i][0]]=ab[i][1]
        else:
            if dp[j]!=0:
                #print(ab[i][0])
                #print()
                dp_sub[j+ab[i][0]]=dp[j]+ab[i][1]
    for j in range(t+m):
        dp[j]=max(dp[j],dp_sub[j])
print(max(dp))
#print(len(dp))

Submission status: Time Limit Exceeded

Solution code:
n, t = map(int, input().split())
ab = sorted([ list(map(int, input().split())) for _ in range(n)])
A, B = zip(*ab)
dp = [[0]*(t+1) for _ in range(n+1)]
for i in range(n):
  for j in range(t+1):
    if A[i] >= j:
      dp[i+1][j] = dp[i][j]
    else:
      dp[i+1][j] = max(dp[i][j-A[i]] + B[i], dp[i][j])
maxB = [B[-1]]*n
for i in range(n-2, 0, -1):
  maxB[i] = max(B[i], maxB[i+1])
ans = 0
for i in range(n-1):
  ans = max(ans, dp[i+1][t] + maxB[i+1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def solve(n, t, a, b):
    prefix = np.zeros((n+1, t+1), dtype=int)
    suffix = np.zeros((n+1, t+1), dtype=int)
    for i in range(n):
        prefix[i+1] = prefix[i]
        for j in range(t):
            nj = j+a[i]
            if nj <= t:
                prefix[i+1, nj] = max(prefix[i+1, nj], prefix[i, j] + b[i])
    for i in range(n-1, -1, -1):
        suffix[i] = suffix[i+1]
        for j in range(t):
            nj = j+a[i]
            if nj <= t:
                suffix[i, nj] = max(suffix[i, nj], suffix[i+1, j] + b[i])
    for i in range(n+1):
        for j in range(t):
            prefix[i, j+1] = max(prefix[i, j+1], prefix[i, j])
            suffix[i, j+1] = max(suffix[i, j+1], suffix[i, j])
    best = 0
    for i in range(n):
        for j in range(t):
            cur = prefix[i,j] + b[i] + suffix[i+1,t-1-j]
            best = max(best, cur)
    return best

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

Submission status: Time Limit Exceeded

Solution code:
# E - All-you-can-eat
import numpy as np
N, T = map(int, input().split())
dishes = [0] * N
for i in range(N):
    A, B = map(int, input().split())
    dishes[i] = (A, B)

dishes.sort()
dp = np.zeros([N + 1, T])  # [[0] * T for _ in range(N + 1)]
ans = 0

for i in range(N):
    for j in range(T):
        if dp[i + 1][j] < dp[i][j]:
            dp[i + 1][j] = dp[i][j]
        nj = j + dishes[i][0]
        if nj < T:
            if dp[i + 1][nj] < dp[i][j] + dishes[i][1]:
                dp[i + 1][nj] = dp[i][j] + dishes[i][1]
    now = dp[i][T - 1] + dishes[i][1]
    ans = max(ans, now)
    
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
# E - All-you-can-eat   
N, T = map(int, input().split())
dishes = [0] * N
for i in range(N):
    A, B = map(int, input().split())
    dishes[i] = (A, B)

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

for i in range(N):
    for j in range(T):
        if dp[i + 1][j] < dp[i][j]:
            dp[i + 1][j] = dp[i][j]
        nj = j + dishes[i][0]
        if nj < T:
            if dp[i + 1][nj] < dp[i][j] + dishes[i][1]:
                dp[i + 1][nj] = dp[i][j] + dishes[i][1]
    now = dp[i][T - 1] + dishes[i][1]
    ans = max(ans, now)
    
print(ans)

Submission status: Time Limit Exceeded

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

AB.sort()

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]
n, p = map(int, input().split())
snacks = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: x[0])
dp = [0 for _ in range(p)]
ans = snacks[0][1]

for i in range(1, n):
	x, y = snacks[i-1]
	z = snacks[i][1]
	for j in range(p-1, 0, -1):
		if j >= x:
			dp[j] = max(dp[j], dp[j-x]+y)
		ans = max(ans, dp[j]+z)

print(ans)

Submission status: Time Limit Exceeded

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

l.sort()
#print(l)

ans = 0
dp = [[0]*(t+1) for _ in range(n+1)]
for i in range(n):
  for j in range(t):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    nj = j+l[i][0]
    if nj < t:
      dp[i+1][nj] = max(dp[i+1][nj], dp[i][j]+l[i][1])
  now = dp[i][t-1] + l[i][1]
  ans = max(ans, now)
#print(dp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    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)]
    ans = 0
    
    for i in range(N):
        for j in range(T):
            dp[i+1][j] = max(dp[i+1][j], dp[i][j])
            x = j + AB[i][0]
            if x < T: dp[i+1][x] = max(dp[i+1][x], dp[i][j]+AB[i][1])
        now = dp[i][T-1] + AB[i][1]
        ans = max(ans, now)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N,T = map(int, input().split())
X = [[]*2]*N
for i in range(N):
  X[i] = list(map(int, input().split()))

ANS = 0
for x in itertools.permutations(X):
  ans = 0
  t = T
  for i in x:
    t -= i[0]
    ans += i[1]
    if ans > ANS: ANS = ans
    if t <= 0.5:
      break
      
print(ANS)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N,T = map(int, input().split())
X = [[]*2]*N
for i in range(N):
  X[i] = list(map(int, input().split()))
X.sort()
X.reverse()

ANS = 0
for x in itertools.permutations(X):
  ans = 0
  t = T
  for i in x:
    t -= i[0]
    ans += i[1]
    if t <= 0.5:
      break
  if ans > ANS: ANS = ans
      
print(ANS)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N,T = map(int, input().split())
X = [[]*2]*N
for i in range(N):
  X[i] = list(map(int, input().split()))
X.sort()

I = list(range(N))

ANS = 0
for i in itertools.permutations(I):
  ans = 0
  t = T
  for x in i:
    t -= X[x][0]
    ans += X[x][1]
    if ans > ANS: ANS = ans
    if t <= 0.5:
      break
      
print(ANS)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N,T = map(int, input().split())
X = [[]*2]*N
for i in range(N):
  X[i] = list(map(int, input().split()))

ANS = 0
for x in itertools.permutations(X):
  ans = 0
  t = T
  for i in x:
    t -= i[0]
    ans += i[1]
    if ans > ANS: ANS = ans
    if t <= 0.5:
      break
      
print(ANS)

Submission status: Time Limit Exceeded

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

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

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

ans = 0

for i in range(n):
    for j in range(t):
        if j < dishes[i][0] :
            dp[i+1][j] = dp[i][j]
        else:
            if dp[i][j] > dp[i][j-dishes[i][0]]+dishes[i][1]:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = dp[i][j-dishes[i][0]]+dishes[i][1]
    ans_i = dp[i][t-1] + max( dishes[i:], key=lambda x: x[1])[1]
    ans = max(ans, ans_i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, t = map(int, input().split())
f = [[0, 0] for i in range(t + 1)]
for i in range(n):
  w, v = map(int, input().split())
  for j in range(t, 0, -1):
    f[j][1] = max(f[j][1], f[j - 1][0] + v)
    if j >= w:
      f[j][0] = max(f[j][0], f[j - w][0] + v)
      f[j][1] = max(f[j][1], f[j - w][1] + v)
print(f[t][1])

Submission status: Time Limit Exceeded

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

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

ans = 0

for i in range(n):
    for j in range(t):
        #for k in range(2):
        #dp[i+1][j] = max(dp[i][j],dp[i-1][j])
        dp[i+1][j] = dp[i][j]
        #print(j,ab[i][0])
        if j >= ab[i][0]:
        #if dp[i][j] < dp[i][j-ab[i][0]] + ab[i][1]:
        #    dp[i+1][j] = dp[i][j-ab[i][0]] + ab[i][1]
            dp[i+1][j] = max(dp[i][j],dp[i][j-ab[i][0]] + ab[i][1])
        #print(dp)
    #print(h,dp)

#print(ab)
#print(dp)


max_num = [0]*n

for i in reversed(range(n)):
    #print(i)
    if i == 0:
        break
    if max_num[i] < ab[i][1]:
        max_num[i-1] = ab[i][1]
    else:
        max_num[i-1] = max_num[i]

#print(max_num)
#print(ab[2272:2336])
#exit()

ans = 0
for i in range(n-1):
    #print(i)
    if ans < dp[i+1][t-1]+max_num[i]:
        ans = dp[i+1][t-1]+max_num[i]
        #print(i,ans,max_num[i],max_num[i+1],max_num[i-1])
    #ans = max(ans,)
    #print(max(ab[i:][1]))
    #print()
        
print(ans)
#print())