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:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, t = LI()
ab = [LI() for _ in range(n)]
ab.sort(key = itemgetter(1))

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

Submission status: Wrong Answer

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, t = LI()
ab = [LI() for _ in range(n)]
ab.sort(key = itemgetter(1))
dp = [[0] * t for _ in range(n)]
s = 0
for i in range(1, n):
    a, b = ab[i-1]
    s += b
    for j in range(t-1):
        if j < a:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-a] + b)

ans = dp[n-1][t-2] + ab[-1][1]
print(ans)

Submission status: Wrong Answer

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, t = LI()
ab = [LI() for _ in range(n)]
ab.sort(key = itemgetter(1))
dp = [[0] * t for _ in range(n)]
s = 0
for i in range(1, n):
    a, b = ab[i-1]
    s += b
    for j in range(t):
        if j < a:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-a] + b)

ans = dp[n-1][t-1] + ab[-1][1]
print(ans)

Submission status: Wrong Answer

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, t = LI()
ab = [LI() for _ in range(n)]
ab.sort(key = itemgetter(1))
dp = [[0] * t for _ in range(n)]
s = 0
for i in range(1, n):
    a, b = ab[i-1]
    s += b
    for j in range(t)[::-1]:
        if j < a:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-a] + b)

ans = dp[n-1][t-1] + ab[-1][1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, T = mapint()
AB = [list(mapint()) for _ in range(N)]
AB.sort()
maxtime = AB[-1][0]
Bs = [b for a, b in AB]
dp = [0]*(T)

ans = 0
for n in range(N-1):
    a, b = AB[n]
    newdp = [0]*(T-1)
    for i in range(T-1):
        if a<=i and i-a<T:
            newdp[i] = max(dp[i], dp[i-a]+b)
    dp = newdp
    ans = max(ans, max(dp)+Bs[n+1])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, T = mapint()
AB = [list(mapint()) for _ in range(N)]
AB.sort()
maxtime = AB[-1][0]
Bs = [b for a, b in AB]
dp = [0]*(T)

ans = 0
for n in range(N-1):
    a, b = AB[n]
    newdp = [0]*(T-1)
    for i in range(T-1):
        if a<=i and i-a<T:
            newdp[i] = max(dp[i], dp[i-a]+b)
    dp = newdp
    ans = max(ans, max(dp)+max(Bs[n+1:]))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, T = mapint()
AB = [list(mapint()) for _ in range(N)]
AB.sort()
maxtime = AB[-1][0]

dp = [0]*(T+maxtime)

for n in range(N):
    a, b = AB[n]
    newdp = [0]*(T+maxtime)
    for i in range(T+maxtime):
        if a<=i and i-a<T:
            newdp[i] = max(dp[i], dp[i-a]+b)
    dp = newdp

print(max(dp))

Submission status: Wrong Answer

Solution code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort()
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]

    dp1 = [[0]*(T+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(T):
            nt = min(j + A[i], T)
            dp1[i+1][nt] = max(dp1[i][nt], dp1[i][j] + B[i])

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort()
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]

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

    ans = 0
    for i in range(N):
        ans = max(ans, max(dp1[i]) + B[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort()
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, T = mapint()
AB = [list(mapint()) for _ in range(N)]
AB.sort()
maxtime = AB[-1][0]

dp = [0]*(T+maxtime+1)

for n in range(N):
    a, b = AB[n]
    newdp = [0]*(T+maxtime+1)
    for i in range(T+maxtime+1):
        if a<=i and i-a<T:
            newdp[i] = max(dp[i], dp[i-a]+b)
    dp = newdp

print(max(dp))

Submission status: Wrong Answer

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(N, T, AB):
    dp = [[0] * (T + 1) for _ in range(N + 1)]
    for i in range(1, N + 1):
        a, b = AB[i - 1]
        mx = 0
        for t in range(T):
            dp[i][t] = dp[i - 1][t]
            if t >= a:
                dp[i][t] = max(dp[i][t], dp[i - 1][t - a] + b)
            mx = max(mx, dp[i - 1][t])
        dp[i][T] = max(dp[i][T], mx + b)
    print(dp[N][T])

if __name__ == '__main__':
    input = sys.stdin.readline
    N, T = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(N)]
    main(N, T, AB)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, T = mapint()
AB = [list(mapint()) for _ in range(N)]
AB.sort()
maxtime = AB[-1][0]

dp = [0]*(T+maxtime+1)

for n in range(N):
    a, b = AB[n]
    newdp = [0]*(T+maxtime+1)
    for i in range(T+maxtime+1):
        if a<=i and i-a<=T:
            newdp[i] = max(dp[i], dp[i-a]+b)
    dp = newdp

print(max(dp))

Submission status: Wrong Answer

Solution code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort()
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]

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

    ans = dp1[N-1][T-1] + B[-1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

# 01ナップサック問題
N,W = map(int,readline().split())
AB = [list(map(int,readline().split())) for i in range(N)]
AB.sort(key=lambda x: (x[1],x[0]))
dp = [0]*(W+1)

for (w,v) in AB:
    dpnew = dp[::]
    for j in range(W):
        dpnew[min(j+w,W)] = max(dp[min(j+w,W)],dp[j] + v)
    dp = dpnew

print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

# 01ナップサック問題
N,W = map(int,readline().split())
AB = [list(map(int,readline().split())) for i in range(N)]
AB.sort(key=lambda x: x[1])
dp = [0]*(W+1)

for (w,v) in AB:
    dpnew = dp[::]
    for j in range(W):
        dpnew[min(j+w,W)] = max(dp[min(j+w,W)],dp[j] + v)
    dp = dpnew

print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

# 01ナップサック問題
N,W = map(int,readline().split())

dp = [0 for j in range(W+1)]

for i in range(N):
    w,v = map(int,readline().split())
    dpnew = dp[::]
    # dp[i][j] = i番目までの品物から重さj以下でつくれる最大の価値
    for j in range(W):
        # jがwより小さいとき、wは選べない
        dpnew[min(j+w,W)] = max(dp[min(j+w,W)],dp[j] + v)
    dp = dpnew

print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

# 01ナップサック問題
N,W = map(int,readline().split())

dp = [0 for j in range(W+1)]

for i in range(N):
    w,v = map(int,readline().split())
    dpnew = dp[::]
    # dp[i][j] = i番目までの品物から重さj以下でつくれる最大の価値
    for j in range(W):
        # jがwより小さいとき、wは選べない
        dpnew[min(j+w,W)] = max(dp[min(j+w,W)],dp[j] + v)
    dp = dpnew

print(dp[W])

Submission status: Wrong Answer

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

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

    ans = 0
    for le in range(N):
        ri = le + 2
        for j in range(T):
            v = max(dp1[le][j], dp2[ri][T-1-j]) + B[le]
            ans = max(ans, v)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

# 01ナップサック問題
N,W = map(int,readline().split())

dp = [0 for j in range(6001)]

for i in range(N):
    w,v = map(int,readline().split())
    dpnew = dp[::]
    # dp[i][j] = i番目までの品物から重さj以下でつくれる最大の価値
    for j in range(W):
        # jがwより小さいとき、wは選べない
        dpnew[j+w] = max(dp[j+w],dp[j] + v)
    dp = dpnew

print(max(dp))

Submission status: Wrong Answer

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

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

    ans = 0
    for le in range(N):
        ri = le + 2
        v = max(dp1[le][T-1], dp2[ri][T-1]) + B[le]
        ans = max(ans, v)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  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+1)]
  for i in range(1,N+1):
    for t in range(1,T+A[i-1]):
      if t<A[i-1]:
        dp[i][t] = dp[i-1][t]
      else:
        dp[i][t] = max(dp[i-1][t],dp[i-1][t-A[i-1]]+B[i-1])
  ans = max(dp[-1])
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
ab=[list(map(int,input().split()))for _ in range(n)]
ab.sort()
dp=[(t+3001)*[0]for _ in range(n+1)]
dp[0][0]=0
ans=0
for i in range(n):
  for j in range(t):
    dp[i+1][j+ab[i][0]]=max(dp[i][j]+ab[i][1],dp[i][j+ab[i][0]])
    ans=max(ans,dp[i+1][j+ab[i][0]])
print(ans)

Submission status: Wrong Answer

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]*(t+3001) for _ in range(n+1)]

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

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

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

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
#dp

#dp[T] T分での最大幸福 幸福度B、時間A

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

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



dp = [0]*T
counter = 0
for i in range(N-1):
    A,B = li[i]
    counter = max(counter,max(dp)+B)
    if A>T-1:
        continue
    for t in range(T-A-1,-1,-1):
        dp[t+A]=max(dp[t]+B,dp[t+A])

print(max(max(dp)+li[N-1][1],counter))

Submission status: Wrong Answer

Solution code:
#dp

#dp[T] T分での最大幸福 幸福度B、時間A

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

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

li = sorted(li,key=lambda x: x[0])
print(li)
dp = [0]*T
counter = 0
for i in range(N-1):
    A,B = li[i]
    counter = max(counter,max(dp))
    if A>T-1:
        continue
    for t in range(T-A-1,-1,-1):
        dp[t+A]=max(dp[t]+B,dp[t+A])

print(max(dp)+li[N-1][1],counter)

Submission status: Wrong Answer

Solution code:
#dp

#dp[T] T分での最大幸福 幸福度B、時間A

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

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

li = sorted(li,key=lambda x: x[0])
dp = [0]*T
for i in range(N-1):
    A,B = li[i]
    
    if A>T-1:
        continue
    for t in range(T-A-1,-1,-1):
        dp[t+A]=max(dp[t]+B,dp[t+A])

print(max(dp)+li[N-1][1])

Submission status: Wrong Answer

Solution code:
#dp

#dp[T] T分での最大幸福 幸福度B、時間A

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

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

li = sorted(li,key=lambda x: x[0])
dp = [0]*T
for i in range(N-1):
    A,B = li[i]
    print(A,B)
    if A>T-1:
        continue
    for t in range(T-A-1,-1,-1):
        dp[t+A]=max(dp[t]+B,dp[t+A])

print(max(dp)+li[N-1][1])

Submission status: Wrong Answer

Solution code:
#dp

#dp[T] T分での最大幸福

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

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

li = sorted(li,key=lambda x: x[1])
dp = [0]*T
for i in range(N-1):
    A,B = li[i]
    if A>T-1:
        continue
    for t in range(T-A-1,-1,-1):
        dp[t+A]=max(dp[t]+B,dp[t+A])

print(max(dp)+li[N-1][1])

Submission status: Wrong Answer

Solution code:
#dp

#dp[T] T分での最大幸福

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

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

if N==1:
    print(li[0][1])
    exit()

li = sorted(li,key=lambda x: x[1])
dp = [0]*T
for i in range(N-1):
    A,B = li[i]
    if A>T-1:
        continue
    for t in range(T-A-1,0,-1):
        dp[t+A]=max(dp[t]+B,dp[t+A])

print(max(dp)+li[N-1][1])

Submission status: Wrong Answer

Solution code:
#dp

#dp[T] T分での最大幸福

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

li = [list(map(int,input().split())) for _ in  range(N)]
li = sorted(li,key=lambda x: x[1])
dp = [0]*T
for i in range(N-1):
    A,B = li[i]
    for t in range(T-A-1,0,-1):
        dp[t+A]=max(dp[t]+B,dp[t+A])

print(max(dp)+li[N-1][1])

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, t = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(n)]

    dp = [[0] * (t + 1) for _ in range(n + 1)]
    prev = [[0] * (t + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        a, b = AB[i - 1]
        for j in range(1, t + 1):
            if j - a >= 0:
                if dp[i - 1][j] < dp[i - 1][j - a] + b:
                    dp[i][j] = dp[i - 1][j - a] + b
                    prev[i][j] = j - a
                else:
                    dp[i][j] = dp[i - 1][j]
                    prev[i][j] = j
            else:
                dp[i][j] = dp[i - 1][j]
                prev[i][j] = j
    cur_t = t
    used = set()
    total = 0
    for i in reversed(range(n)):
        if prev[i + 1][cur_t] == cur_t - AB[i][0]:
            total += AB[i][0]
            used.add(i)
        cur_t = prev[i + 1][cur_t]

    ma = 0
    if total < t:
        for i in range(n):
            if i not in used:
                a, b = AB[i]
                if ma < b:
                    ma = b

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


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
def main():
    N, T = list(map(int, input().split(' ')))
    foods = [list(map(int, input().split(' '))) for _ in range(N)]
    # 食べ物は食べる時間の昇順に並べ替え、短い方を優先して食べるようにしておく。
    # そうしないと下記のようなケースでNGになる。
    # 3 3
    # 2 2
    # 1 1
    # 1 1
    foods.sort(key=lambda food: food[0])
    # dp[n][t]: n個目までの食べ物のうち、時刻tまでに食べきる場合に得られるおいしさの合計の最大値
    # used[n][t]: 上記最大値を実現する場合にn個目の食べ物を食べるか否か
    dp = [[0 for _ in range(T + 1)] for _ in range(N + 1)]
    used = [[0 for _ in range(T + 1)] for _ in range(N + 1)]
    for i, food in enumerate(foods):
        n = i + 1
        A, B = food
        for t in range(T + 1):
            val = dp[n - 1][t]
            if t - A >= 0 and dp[n - 1][t - A] + B > val:
                val = dp[n - 1][t - A] + B
                used[n][t] = 1
            dp[n][t] = val
    ans = dp[N][T - 1]  # 注文できるギリギリの時刻までに食べきる場合のおいしさの合計の最大値
    # 最後に一番高いおいしさの1品を追加
    foods_used = [0 for _ in range(N)]
    t = T - 1
    for n in range(N, 0, -1):
        i = n - 1
        if used[n][t] == 1:
            foods_used[i] = 1
            t -= foods[i][0]
    last_one_val = 0
    for flag, food in zip(foods_used, foods):
        if flag == 1:
            continue
        last_one_val = max(last_one_val, food[1])
    ans += last_one_val
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, T = list(map(int, input().split(' ')))
    foods = [list(map(int, input().split(' '))) for _ in range(N)]
    # dp[n][t]: n個目までの食べ物のうち、時刻tまでに食べきる場合に得られるおいしさの合計の最大値
    # used[n][t]: 上記最大値を実現する場合にn個目の食べ物を食べるか否か
    dp = [[0 for _ in range(T + 1)] for _ in range(N + 1)]
    used = [[0 for _ in range(T + 1)] for _ in range(N + 1)]
    for i, food in enumerate(foods):
        n = i + 1
        A, B = food
        for t in range(T + 1):
            val = dp[n - 1][t]
            if t - A >= 0 and dp[n - 1][t - A] + B > val:
                val = dp[n - 1][t - A] + B
                used[n][t] = 1
            dp[n][t] = val
    ans = dp[N][T - 1]  # 注文できるギリギリの時刻までに食べきる場合のおいしさの合計の最大値
    # 最後に一番高いおいしさの1品を追加
    foods_used = [0 for _ in range(N)]
    t = T - 1
    for n in range(N, 0, -1):
        i = n - 1
        if used[n][t] == 1:
            foods_used[i] = 1
            t -= foods[i][0]
    last_one_val = 0
    for flag, food in zip(foods_used, foods):
        if flag == 1:
            continue
        last_one_val = max(last_one_val, food[1])
    ans += last_one_val
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *AB = map(int, read().split())
    A = AB[::2]
    B = AB[1::2]

    dp = [[0] * T for _ in range(N + 1)]
    for i in range(N):
        for t in range(T):
            if 0 <= t - A[i]:
                dp[i + 1][t] = dp[i][t - A[i]] + B[i]
            if dp[i + 1][t] < dp[i][t]:
                dp[i + 1][t] = dp[i][t]

    C = [False] * N

    t = T - 1
    for i in range(N - 1, -1, -1):
        if t - A[i] > 0 and dp[i + 1][t] == dp[i][t - A[i]] + B[i]:
            C[i] = True
            t -= A[i]

    ans = dp[N][T - 1] + max((B[i] for i in range(N) if not C[i]), default=0)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *AB = map(int, read().split())
    A = AB[::2]
    B = AB[1::2]

    dp = [[0] * (2 * T + 1) for _ in range(N + 1)]
    for i in range(N):
        for t in range(2 * T):
            if 0 <= t - A[i] < T:
                dp[i + 1][t] = dp[i][t - A[i]] + B[i]
            if dp[i + 1][t] < dp[i][t]:
                dp[i + 1][t] = dp[i][t]

    ans = 0
    for i in range(N + 1):
        for t in range(2 * T + 1):
            if ans < dp[i][t]:
                ans = dp[i][t]

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, T = LI()
    a, b = [], []
    for i in range(N):
        _a, _b = LI()
        a.append(_a)
        b.append(_b)
    dp = [[0 for _ in range(T + 3001)] for _ in range(N+1)]
    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):
        for j in range(T + 3001):
            ans = max(dp[i][j], ans)
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, T = map(int, readline().split())
    dishes = []
    for _ in range(N):
        A, B = map(int, readline().split())
        dishes.append([A, B])

    dp1 = [[0]*T for _ in range(N)] # i and less than i
    dp2 = [[0]*T for _ in range(N+1)] # more than i

    for i, dish in enumerate(dishes[:-1]):
        a, b = dish
        i = i + 1
        for t in range(T):
            if a > t:
                dp1[i][t] = dp1[i-1][t]
            else:
                if dp1[i-1][t-a] + b > dp1[i-1][t]:
                    dp1[i][t] = dp1[i-1][t-a] + b
                else:
                    dp1[i][t] = dp1[i-1][t]


    for i, dish in enumerate(reversed(dishes[1:])):
        a, b = dish
        i = N - i - 1
        for t in range(T):
            if a > t:
                dp2[i][t] = dp2[i-1][t]
            else:
                if dp2[i+1][t-a] + b > dp2[i+1][t]:
                    dp2[i][t] = dp2[i+1][t-a] + b
                else:
                    dp2[i][t] = dp2[i+1][t]

    ans = 0
    for last in range(1, N+1):
        for j in range(T):
            a = dishes[last-1][1]
            a += dp1[last-1][T-1-j] + dp2[last][j]
            ans = a if ans < a else ans
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, T = map(int, readline().split())
    dishes = []
    for _ in range(N):
        A, B = map(int, readline().split())
        dishes.append([A, B])

    dp1 = [[0]*T for _ in range(N)] # i and less than i
    dp2 = [[0]*T for _ in range(N+1)] # more than i

    for i, dish in enumerate(dishes[:-1]):
        a, b = dish
        i = i + 1
        for t in range(T):
            if a > t:
                dp1[i][t] = dp1[i-1][t]
            else:
                if dp1[i-1][t-a] + b > dp1[i-1][t]:
                    dp1[i][t] = dp1[i-1][t-a] + b
                else:
                    dp1[i][t] = dp1[i-1][t]


    for i, dish in enumerate(reversed(dishes[1:])):
        a, b = dish
        i = N - i - 1
        for t in range(T):
            if a > t:
                dp2[i][t] = dp2[i-1][t]
            else:
                if dp2[i+1][t-a] + b > dp2[i][t]:
                    dp2[i][t] = dp2[i+1][t-a] + b

    ans = 0
    for last in range(1, N+1):
        for j in range(T):
            a = dishes[last-1][1]
            a += dp1[last-1][T-1-j] + dp2[last][j]
            ans = a if ans < a else ans
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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+1) for _ in range(n+1)]

for j in range(t+1):
    for i in range(n):
        if j < t:
            x = min(j+a[i],t)
            dp[i+1][x] = max(dp[i+1][x],dp[i][j]+b[i])
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
# for i in range(n+1): print(dp[i])
print(dp[n][t])

Submission status: Wrong Answer

Solution code:
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+1) for _ in range(n+1)]

for i in range(n):
    for j in range(t+1):
        if j < t:
            x = min(j+a[i],t)
            dp[i+1][x] = max(dp[i+1][x],dp[i][j]+b[i])
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
# for i in range(n+1): print(dp[i])
print(dp[n][t])