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: Wrong Answer

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

    dp = [-1 for i in range(T)]
    dp[0] = 0
    for a, b in ab:
        for i in range(T-1, -1, -1):
            if i - a >= 0:
                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])

    # print(dp)
    print(max(dp[-1]+tmp, ans))


main()

Submission status: Wrong Answer

Solution code:
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[1])
ans = ab.pop()[1]

dp = [-1 for i in range(T)]
dp[0] = 0
for a, b in ab:
    for i in range(T-1, -1, -1):
        if i - a >= 0:
            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])

# print(dp)
print(dp[-1] + ans)

Submission status: Wrong Answer

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

    Food = namedtuple('Food', 'time_to_eat deliciousness')
    INF = 3000 * 3000 + 1

    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 = [-INF] * T
    dp[0] = 0
    # dp[time]:=timeに食べ終わる注文の仕方で得られる最大満足度

    ma = 0
    for food in foods:
        # これでフィニッシュの場合
        ma = max((
            dp[time] + food.deliciousness
            for time in range(T)
        ), default=ma)

        # まだ食べる場合
        for time in reversed(range(T - food.time_to_eat)):
            dp[time + food.time_to_eat] = max(
                dp[time + food.time_to_eat],
                dp[time] + food.deliciousness
            )

    print(ma)
    return


if __name__ == '__main__':
    solve_145e()

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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]=dp[i][j]+b
for i in range(t):
    ans=max(ans, dp[-1][i])
print(ans)
# print(dp)

Submission status: Wrong Answer

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

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

dp=[[-1]*(t-1) 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-1):
        if dp[i][j]>=0:
            dp[i+1][j]=max(dp[i][j],dp[i+1][j])
            if j+a>=t-1:
                ans=max(ans, dp[i][j]+b)
            else:
                dp[i+1][j+a]=dp[i][j]+b
for i in range(t-1):
    ans=max(ans, dp[-1][i])
print(ans)
# print(dp)

Submission status: Wrong Answer

Solution code:


n, t = map(int, input().split())
t-= 1
time = []
ratio = []
delic = []
v = []
used = [False for i in range(n)]
for i in range(n):
    x, y = map(int, input().split())
    v.append((x, y))
    time.append((x, y, i))
    delic.append((y, x, i))
    ratio.append((y/x, x, y, i))

time.sort()
delic.sort(key=lambda x: x[1])
delic.sort(reverse=True)
ratio.sort(key=lambda x: x[1])
ratio.sort(reverse=True)

res = 0
for r, ti, d, i in ratio:
    if t -  ti >= 0:
        t -= ti
        res += d
        used[i] = True

if t != 0:
    for d, ti, i in delic:
        if t >= ti and not used[i]:
            t -= ti
            res += d
            used[i] = True

for d, ti, i in delic:
    if not used[i]:
        # print(d, ti, i)
        print(res+d)
        break
else:
    print(res)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

DP = [[0] * (T + 4000) for counter1 in range(N+1)]

for i in range(N):
    for j in range(T):
        DP[i+1][j+A[i]] = max(DP[i+1][j+A[i]], DP[i][j] + B[i])
        DP[i+1][j] = max(DP[i][j], DP[i+1][j])
ans = max(DP[N])
print(ans)

Submission status: Wrong Answer

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

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

DP = [[0] * (T + 4000) for counter1 in range(N+1)]

for i in range(N):
    for j in range(T):
        DP[i+1][j+A[i]] = max(DP[i+1][j], DP[i][j] + B[i])
        DP[i+1][j] = max(DP[i][j], DP[i+1][j])
ans = max(DP[N])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]


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


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

ans = 0

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

inf = 10**9
dp = np.full(6000 + 1, -inf, dtype=np.int64)

a, b = AB.pop()
dp[0] = b

for a, b in AB:
    dp[a:] = np.maximum(dp[a:], dp[:-a] + b)

dp = dp[:T]
ans = dp.max()
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

def solve(N: int, T: int, A: "List[int]", B: "List[int]"):
    dp = [0] * (2 * max(T, max(A)))
    for a, b in zip(A, B):
        for t in range(T+a-1, a-1, -1):
            dp[t] = max(dp[t], dp[t-a] + b)
    return(max(dp))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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))
    print(solve(N, T, A, B))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

def solve(N: int, T: int, A: "List[int]", B: "List[int]"):
    dp = [0] * (2 * T)
    for a, b in zip(A, B):
        for t in range(min(T+a-1, 2*T-1), a-1, -1):
            dp[t] = max(dp[t], dp[t-a] + b)
    return(max(dp))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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))
    print(solve(N, T, A, B))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
from fractions import gcd
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
EPS = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

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

ab_list.sort(key = lambda x: x[1])
#(jikan, oishisa)
ans = ab_list[-1][1]
ab_list = ab_list[:-1]

dp = [[0]*3010 for _ in range(3010)]
#dp[n_ryori][total_jikan]
dp[0][0] = 0

for i in range(n-1):
    for j in range(t):
        if j >= ab_list[i][0]:
            dp[i+1][j] = max(dp[i][j-ab_list[i][0]]+ab_list[i][1], dp[i][j])
        else:
            dp[i+1][j] = dp[i][j]
ans += dp[n-1][t-1]
print(ans)

Submission status: Wrong Answer

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

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
l = []
for i in range(n):
    a,b = map(int,input().split())
    l.append([a,b])
l.sort(key=lambda x:x[0])
dp = [[0 for i in range(t)] for i in range(n+1)]
al = []
for i in range(n):
    a,b = l[i]
    for j in range(t-1):
        if dp[i][j] == 0:
            if j == 0:
                if j + a < t:
                    dp[i+1][j+a] = max(dp[i][j+a],b)
                else:
                    al.append(b)
        else:
            if j + a < t:
                dp[i+1][j+a] = max(dp[i][j+a],dp[i][j]+b)
            else:
                al.append(dp[i][j]+b)
            dp[i+1][j] = max(dp[i+1][j],dp[i][j])
if len(al) > 0:
    print(max(max(dp[n]),max(al)))
else:
    print(max(dp[n]))

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
l = []
for i in range(n):
    a,b = map(int,input().split())
    l.append([a,b])
l.sort(key=lambda x:x[1])
dp = [[0 for i in range(t)] for i in range(n)]
for i in range(n-1):
    a,b = l[i]
    for j in range(t-1):
        if dp[i][j] == 0:
            if j == 0:
                if j + a < t:
                    dp[i+1][j+a] = max(dp[i][j+a],b)
        else:
            if j + a < t:
                dp[i+1][j+a] = max(dp[i][j+a],dp[i][j]+b)
            dp[i+1][j] = max(dp[i+1][j],dp[i][j])
print(max(dp[n-1])+l[n-1][1])

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
l = []
for i in range(n):
    a,b = map(int,input().split())
    l.append([a,b])
l.sort(key=lambda x:x[1],reverse=True)
dp = [[0 for i in range(t)] for i in range(n+1)]
for i in range(1,n):
    a,b = l[i]
    for j in range(t-1):
        if dp[i][j] == 0:
            if j == 0:
                if j + a < t:
                    dp[i+1][j+a] = max(dp[i][j+a],b)
        else:
            if j + a < t:
                dp[i+1][j+a] = max(dp[i][j+a],dp[i][j]+b)
            dp[i+1][j] = max(dp[i+1][j],dp[i][j])
print(max(dp[n])+l[0][1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,T=map(int,input().split())

l=[]
for _ in range(N):
    a,b=map(int,input().split())
    l.append([a,b])
    
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))

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
import bisect
import queue
import heapq
from collections import deque
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

改行あり
x = []
y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

N行M列の初期化
dp = [[INF] * M for i in range(N)]

'''

N, T = map(int, input().split())
A = []
D = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    A.append(x1)
    D.append(y1)


best = 0
for i in range(N):
    if D[i] >= D[best]:
        if D[i] == D[best]:
            if A[i] > A[best]:
                best = i
        else:
            best = i

ans = D[best]



del A[best]
del D[best]

dp = [[0] * T for i in range(N-1)] # 満足度の最大値(i番目の料理、j分後まで)

for t in range(T):
    if A[0] <= t:
        dp[0][t] = D[0]

for dish in range(1, N-1):
    for t in range(T):
        if t-A[dish] >= 0:
            dp[dish][t] = max(dp[dish-1][t], dp[dish-1][t-A[dish]] + D[dish])
        else:
            dp[dish][t] = dp[dish-1][t]

print(dp[N-2][T-1] + ans)



"""
ATTENTION: Pythonで間に合うか？？

            <目安>
 文字列結合、再帰関数を含む→Python
 上を含まず時間が不安→PyPy3

"""

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
import bisect
import queue
import heapq
from collections import deque
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

改行あり
x = []
y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

N行M列の初期化
dp = [[INF] * M for i in range(N)]

'''

N, T = map(int, input().split())
A = []
D = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    A.append(x1)
    D.append(y1)


best = 0
for i in range(N):
    if D[i] >= D[best] and A[i] >= A[best]:
        best = i

ans = D[best]



del A[best]
del D[best]

dp = [[0] * T for i in range(N-1)] # 満足度の最大値(i番目の料理、j分後まで)

for t in range(T):
    if A[0] <= t:
        dp[0][t] = D[0]

for dish in range(1, N-1):
    for t in range(T):
        if t-A[dish] >= 0:
            dp[dish][t] = max(dp[dish-1][t], dp[dish-1][t-A[dish]] + D[dish])
        else:
            dp[dish][t] = dp[dish-1][t]

print(dp[N-2][T-1] + ans)



"""
ATTENTION: Pythonで間に合うか？？

            <目安>
 文字列結合、再帰関数を含む→Python
 上を含まず時間が不安→PyPy3

"""

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
import bisect
import queue
import heapq
from collections import deque
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

改行あり
x = []
y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

N行M列の初期化
dp = [[INF] * M for i in range(N)]

'''

N, T = map(int, input().split())
A = []
D = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    A.append(x1)
    D.append(y1)


best = 0
for i in range(N):
    if D[i] > D[best] and A[i] >= A[best]:
        best = i

ans = D[best]



del A[best]
del D[best]

dp = [[0] * T for i in range(N-1)] # 満足度の最大値(i番目の料理、j分後まで)

for t in range(T):
    if A[0] <= t:
        dp[0][t] = D[0]

for dish in range(1, N-1):
    for t in range(T):
        if t-A[dish] >= 0:
            dp[dish][t] = max(dp[dish-1][t], dp[dish-1][t-A[dish]] + D[dish])
        else:
            dp[dish][t] = dp[dish-1][t]

print(dp[N-2][T-1] + ans)



"""
ATTENTION: Pythonで間に合うか？？

            <目安>
 文字列結合、再帰関数を含む→Python
 上を含まず時間が不安→PyPy3

"""

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
import bisect
import queue
import heapq
from collections import deque
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

改行あり
x = []
y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

N行M列の初期化
dp = [[INF] * M for i in range(N)]

'''

N, T = map(int, input().split())
A = []
D = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    A.append(x1)
    D.append(y1)


best = -1
for i in range(N):
    if D[i] > D[best] and A[i] >= A[best]:
        best = i

ans = D[best]

del A[best]
del D[best]

dp = [[0] * T for i in range(N-1)] # 満足度の最大値(i番目の料理、j分後まで)

for t in range(T):
    if A[0] <= t:
        dp[0][t] = D[0]

for dish in range(1, N-1):
    for t in range(T):
        if t-A[dish] >= 0:
            dp[dish][t] = max(dp[dish-1][t], dp[dish-1][t-A[dish]] + D[dish])
        else:
            dp[dish][t] = dp[dish-1][t]

print(dp[N-2][T-1] + ans)



"""
ATTENTION: Pythonで間に合うか？？

            <目安>
 文字列結合、再帰関数を含む→Python
 上を含まず時間が不安→PyPy3

"""

Submission status: Wrong Answer

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

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

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,T=map(int, input().split())
A=[tuple(map(int, input().split())) for i in range(N)]
o=[0]*(T+1)
l=[[]for i in range(T+1)]
for i,t in enumerate(A):
    for j in range(T,-1,-1):
        if t[0]+j<=T and o[t[0]+j]<o[j]+t[1]:
            o[t[0]+j]=o[j]+t[1]
            l[t[0]+j]=l[j]+[i]
e=0
l2=l[-2]
for i,a in enumerate(A):
    if i not in l2:
        if a[1]>e:
            e=a[1]
print(max(o[-2]+e,o[-1]))

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,T=map(int, input().split())
A=[tuple(map(int, input().split())) for i in range(N)]
o=[0]*(T+1)
l=[[]for i in range(T+1)]
for i in range(N):
    for j in range(T,-1,-1):
        if A[i][0]+j<=T and o[A[i][0]+j]<o[j]+A[i][1]:
            o[A[i][0]+j]=o[j]+A[i][1]
            l[A[i][0]+j]=l[j]+[i]
e=0
for i in range(N):
    if i not in l[-2]:
        if A[i][1]>e:
            e=A[i][1]
print(max(o[-2]+e,o[-1]))

Submission status: Wrong Answer

Solution code:
def solve():
    N,T = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(N)]
    
    dp_table = [[0 for _ in range(T+1)] for _ in range(N+1)]
    for i in range(N):
        for j in range(T+1):
            if j < AB[i][0]:
                dp_table[i+1][j] = dp_table[i][j]
            else:
                dp_table[i+1][j] = max(dp_table[i][j-AB[i][0]]+AB[i][1], dp_table[i][j])
    
    T_ = T-1
    AB_ = []
    for i in range(0,N)[::-1]:
        if T_ - AB[i][0] >= 0 and dp_table[i][T_] == max(dp_table[i][T_-AB[i][0]]+AB[i][1], dp_table[i][T_]):
            AB_.append(AB[i][1])
        elif T_ - AB[i][0] >= 0 and dp_table[i][T_-AB[i][0]]+AB[i][1] == max(dp_table[i][T_-AB[i][0]]+AB[i][1], dp_table[i][T_]):
            T_ -= AB[i][0]
        elif T_ - AB[i][0] < 0: 
            AB_.append(AB[i][1])
    # print(dp_table)
    # print(AB_)
    ret = max(dp_table[N][T], dp_table[N][T-1]+max(AB_)) if AB_ else dp_table[N][T]
    print(ret)
    
solve()

Submission status: Wrong Answer

Solution code:
def solve():
    N,T = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(N)]
    
    dp_table = [[[0,[]] for _ in range(T+1)] for _ in range(N+1)]
    for i in range(N):
        for j in range(T+1):
            if j < AB[i][0]:
                dp_table[i+1][j][0] = dp_table[i][j][0]
                dp_table[i+1][j][1] = dp_table[i][j][1]
            else:
                if dp_table[i][j][0] < dp_table[i][j-AB[i][0]][0]+AB[i][1]:
                    dp_table[i+1][j][0] = dp_table[i][j-AB[i][0]][0]+AB[i][1]
                    dp_table[i+1][j][1] = dp_table[i][j-AB[i][0]][1]+[i]
                else:
                    dp_table[i+1][j][0] = dp_table[i][j][0]
                    dp_table[i+1][j][1] = dp_table[i][j][1]
                    
    # print(dp_table)
    AB_ = [AB[i][1] for i in range(N) if i not in dp_table[N][T-1][1]]
    # print(AB_)
    ret = max(dp_table[N][T][0], dp_table[N][T-1][0]+max(AB_)) if AB_ else dp_table[N][T][0]
    print(ret)
    
solve()

Submission status: Wrong Answer

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())
    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: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

n,t = inm()
a = []
b = []
for i in range(n):
    aa,bb = inm()
    a.append(aa)
    b.append(bb)
d = [ [-1]*(t+1) for i in range(n+1) ]
for i in range(n+1):
    d[i][0] = 0
#ddprint(d)
for i in range(n):
    for j in range(t):
        if d[i][j]>=0:
            s = min(t,j+a[i])
            d[i+1][s] = max(d[i+1][s], d[i][j]+b[i])
            d[i+1][j] = max(d[i+1][j], d[i][j])
if DBG:
    for i in range(n):
        ddprint(d[i])
for j in range(t,-1,-1):
    if d[n][j]>=0:
        print(d[n][j])
        exit()

Submission status: Wrong Answer

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

dishes = sorted([list(map(int, input().split()))  for _ in range(n)])

max_ai = dishes[-1][0]

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

for i, (ai, bi) in enumerate(dishes):
  for j in range(t):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    dp[i+1][j + ai] = max(dp[i+1][j + ai], dp[i][j] + bi)
      
      
print(max(dp[-1]))

Submission status: Wrong Answer

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

dishes = sorted([list(map(int, input().split()))  for _ in range(n)], reverse=True, key=lambda x: (x[1], x[0]))

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

for i, (ai, bi) in enumerate(dishes[1:]):
  for j in range(t):
    dp[i+1][j] = dp[i][j]
    if ai <= j:
      dp[i+1][j] = max(dp[i+1][j], dp[i][j-ai] + bi)
      
      
print(max(dp[-1]) + dishes[0][1])

Submission status: Wrong Answer

Solution code:

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

dishes = sorted([list(map(int, input().split()))  for _ in range(n)], reverse=True, key=lambda x: x[1])

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

for i, (ai, bi) in enumerate(dishes[1:]):
  for j in range(t):
    dp[i+1][j] = dp[i][j]
    if ai <= j:
      dp[i+1][j] = max(dp[i+1][j], dp[i][j-ai] + bi)
      
      
print(max(dp[-1]) + dishes[0][1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
ls = []
for i in range(N):
  a, b = map(int, input().split())
  ls += [(b,a)]
ls.sort()
t = [[0]*T for i in range(N+1)]
for i in range(1,N+1):
  b, a = 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])
ans = t[N-1][-1]+ls[-1][0]
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
ls = []
for i in range(N):
  a, b = map(int, input().split())
  ls += [(a,b)]
ls.sort(key=lambda x:x[1])
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])
ans = max(max(t[N-1])+ls[-1][1], max(t[N]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
ls = []
for i in range(N):
  a, b = map(int, input().split())
  ls += [(a,b)]
ls.sort(key=lambda x:x[1])
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])
ans = max(t[N-1])+ls[-1][1]
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
ls = []
for i in range(N):
  a, b = map(int, input().split())
  ls += [(a,b)]
ls.sort(key=lambda x:x[1])
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])
ans = t[N-1][-1]+ls[-1][1]
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
ls = []
for i in range(N):
  a, b = map(int, input().split())
  ls += [(a,b)]
ls.sort(key=lambda x:x[1])
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])
ans = 0
for i in range(1,N+1):
  a, b = ls[i-1]
  m = t[i-1][-1] + b
  if ans<m:
    ans = m
print(ans)