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:
#E
N,T=map(int,input().split())
A=[0 for i in range(N+1)]
B=[0 for i in range(N+1)]
for i in range(1,N+1):
    A[i],B[i]=map(int,input().split())
    
dp=[[0 for i in range(6001)] for j in range(N+1)]

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

ans=max(dp[N])
print(ans)

Submission status: Wrong Answer

Solution code:
#E
N,T=map(int,input().split())
A=[0 for i in range(N+1)]
B=[0 for i in range(N+1)]
for i in range(1,N+1):
    A[i],B[i]=map(int,input().split())
    
dp=[[0]+[False]*N for t in range(6001)]

for i in range(T):
    for j in range(1,N+1):
        if dp[i][j]:
            continue

        if dp[i+A[j]][0]<dp[i][0]+B[j]:
            flag=dp[i].copy()
            flag[j]=True
            dp[i+A[j]]=[dp[i][0]+B[j]]+flag[1:]

ans=0
for i in range(6001):
    ans=max(ans,dp[i][0])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7


def main():
    N, T = map(int, input().split())
    A = [None] * N
    B = [None] * N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    ans = max(B)
    index = B.index(ans)
    del B[index]
    del A[index]
    # print(A, B)

    dp = [0] * 3001
    for i in range(N - 1):
        for j in range(3000, -1, -1):
            if j + A[i] <= 3000:
                dp[j + A[i]] = max(dp[j + A[i]], dp[j] + B[i])
    print(ans+dp[T-1])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def getval():
    n,t = map(int,input().split())
    f = [list(map(int,input().split())) for i in range(n)]
    f.sort(reverse=True)
    return n,t,f

def main(n,t,f):
    dp = [0 for i in range(1+t+3000)]
    prev = [-1 for i in range(1+t+3000)]
    ans = 0
    for i in range(n):
        cura = f[i][0]
        curb = f[i][1]
        tempdp = [0 for i in range(1+t+3000)]
        tempprev = [-1 for i in range(1+t+3000)]
        for j in range(1,1+t+3000):
            newdp = dp[j]
            newprev = prev[j]
            update = tempdp[j-1]
            if update>newdp:
                newdp = update
                newprev = tempprev[j-1]
            if j>=cura:
                update = dp[j-cura] + curb
                if update>newdp:
                    newdp = update
                    if newprev==-1:
                        newprev = i
                    else:
                        newprev = prev[j-cura]
                else:
                    newprev = min(newprev,i)
            tempdp[j] = newdp
            tempprev[j] = newprev
        dp = tempdp
        prev = tempprev
        for j in range(t+3000,-1,-1):
            if j-f[prev[j]][0]<t:
                ans = max(ans,dp[j])
                #print(i,j,ans,prev[j])
                break
    print(ans)
        

if __name__=="__main__":
    n,t,f = getval()
    #print(f)
    main(n,t,f)

Submission status: Wrong Answer

Solution code:
def getval():
    n,t = map(int,input().split())
    f = [list(map(int,input().split())) for i in range(n)]
    f.sort()
    return n,t,f

def main(n,t,f):
    dp = [0 for i in range(1+t+3000)]
    prev = [-1 for i in range(1+t+3000)]
    ans = 0
    for i in range(n):
        cura = f[i][0]
        curb = f[i][1]
        tempdp = [0 for i in range(1+t+3000)]
        tempprev = [-1 for i in range(1+t+3000)]
        for j in range(1,1+t+3000):
            newdp = dp[j]
            newprev = prev[j]
            update = tempdp[j-1]
            if update>newdp:
                newdp = update
                newprev = tempprev[j-1]
            if j>=cura:
                update = dp[j-cura] + curb
                if update>newdp:
                    newdp = update
                    newprev = i
            tempdp[j] = newdp
            tempprev[j] = newprev
        dp = tempdp
        prev = tempprev
        for j in range(t+3000,-1,-1):
            if j-f[prev[j]][0]<t:
                ans = max(ans,dp[j])
                break
    print(ans)
        

if __name__=="__main__":
    n,t,f = getval()
    main(n,t,f)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
I=lambda:int(input())
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

N,T=MI()
dp=[[0]*(T+1) for _ in [0]*(N+1)]

for n in range(N):
    a,b=MI()
    for t in range(T):
        # たべる
        dp[n+1][min(t+a,T)]=max(dp[n][t]+b,dp[n+1][min(t+a,T)])
        # たべない
        dp[n+1][t]=max(dp[n][t],dp[n+1][t])
print(dp[N][T])

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

DP=[[0 for i in range(T)] for j in range(N+1)]

for i in range(N):
  for j in range(T):
    if j-L[i][0]>=0 and i>0:
      DP[i][j]=max(DP[i-1][j-L[i-1][0]]+L[i-1][1],DP[i-1][j])
    else:
      DP[i][j]=DP[i-1][j]
ans=0
for i in range(N+1):
  if i!=N:
    ans=max(ans,DP[i][T-1]+L[i][1])
  else:
    ans=max(ans,DP[i][T-1])

print(ans)

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

DP=[[0 for i in range(T)] for j in range(N+1)]

for i in range(N):
  for j in range(T):
    if j-L[i][0]>=0 and i>0:
      DP[i][j]=max(DP[i-1][j-L[i-1][0]]+L[i-1][1],DP[i-1][j])
ans=0
for i in range(N):
  if i!=N:
    ans=max(ans,DP[i][T-1]+L[i][1])
  else:
    ans=max(ans,DP[i][T-1])

print(ans)

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

DP=[[0 for i in range(T)] for j in range(N+1)]

for i in range(N):
  for j in range(T):
    if j-L[i][0]>=0 and i>0:
      DP[i][j]=max(DP[i-1][j-L[i-1][0]]+L[i-1][1],DP[i-1][j])
ans=0
for i in range(N):
  if i!=N:
    ans=max(ans,DP[i][T-1]+L[i][1])
  else:
    ans=max(ans,DP[i][T-1])

print(ans)

Submission status: Wrong Answer

Solution code:
N,T=[int(s) for s in input().split()]
food=[[int(s) for s in input().split()] for i in range(N)]
food.sort()

#i番目までの料理でt秒以内に食べきるときのDP
DP=[[0 for t in range(T+1)] for i in range(N)]
for i in range(N):
  time,point=food[i]
  for t in range(min([T,time])):
    DP[i][t]=DP[i-1][t]
  for t in range(time,T):
    DP[i][t]=max([DP[i-1][t] , DP[i-1][t-time]+point])
  DP[i][T]=DP[i-1][T-1]+point #最後にi番目の料理を注文する場合

print(DP[-1][-1])

Submission status: Wrong Answer

Solution code:
N,T=[int(s) for s in input().split()]
food=[[int(s) for s in input().split()] for _ in range(N)]
#food=sorted(food)

ans=set()
for f in range(N): #fを除外
#T-1秒までに食べきるとしてDP
  DP=[[0 for t in range(T)] for i in range(N+1)] #1～i番目までの料理のみでT秒以内
  for i in range(1,N+1):
    if i-1==f:
      for t in range(T):
        DP[i][t]=DP[i-1][t]
      continue
    for t in range(T):
      if t<=food[i-1][0]:
        DP[i][t]=DP[i-1][t]
      else:
        DP[i][t]=max([DP[i-1][t],DP[i-1][t-food[i-1][0]]+food[i-1][1]])
 # print(DP,food[f][1])
  ans.add(DP[N][T-1]+food[f][1])
print(max(ans))

Submission status: Wrong Answer

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

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

# from top
for i in range(n):
    for time in range(t+1):
        if time - dishes[i][0] >= 0:
            dp1[i+1][time] = max(
                dp1[i][time-dishes[i][0]] + dishes[i][1],
                dp1[i][time]
            )
        dp1[i+1][time] = max(dp1[i][time], dp1[i+1][time])


for j in reversed(range(2, n+2)):
    i = j-2
    for time in range(t+1):
        if time - dishes[i][0] >= 0:
            dp2[i-1][time] = max(
                dp2[i][time-dishes[i][0]] + dishes[i][1],
                dp2[i][time]
            )
        dp2[i-1][time] = max(dp2[i][time], dp2[i-1][time])


ans = 0
for i in range(1, n+1):
    for time in range(t+1):
        ans = max(
            ans,
            dp1[i-1][time] + dishes[i-1][1] + dp2[i][t - time]
        )
print(ans)

Submission status: Wrong Answer

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

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

# from top
for i in range(n):
    for time in range(t+1):
        if time - dishes[i][0] >= 0:
            dp1[i+1][time] = max(
                dp1[i][time-dishes[i][0]] + dishes[i][1],
                dp1[i][time]
            )
        dp1[i+1][time] = max(dp1[i][time], dp1[i+1][time])


for j in reversed(range(2, n+2)):
    i = n - j + 1
    for time in range(t+1):
        if time - dishes[i][0] >= 0:
            dp2[i-1][time] = max(
                dp2[i][time-dishes[i][0]] + dishes[i][1],
                dp2[i][time]
            )
        dp2[i-1][time] = max(dp2[i][time], dp2[i-1][time])


ans = 0
for i in range(1, n+1):
    for time in range(t+1):
        ans = max(
            ans,
            dp1[i-1][time] + dishes[i-1][1] + dp2[i][t - time]
        )
print(ans)

Submission status: Wrong Answer

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

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

# from top
for i in range(n):
    for time in range(t+1):
        if time + dishes[i][0] <= t:
            dp1[i+1][time+dishes[i][0]] = max(
                dp1[i][time+dishes[i][0]],
                dp1[i][time] + dishes[i][1]
            )
        dp1[i+1][time] = max(dp1[i][time], dp1[i+1][time])


for j in reversed(range(2, n+2)):
    i = n - j + 1
    for time in range(t+1):
        if time + dishes[i][0] <= t:
            dp2[i-1][time+dishes[i][0]] = max(
                dp2[i][time+dishes[i][0]],
                dp2[i][time] + dishes[i][1]
            )
        dp2[i-1][time] = max(dp2[i][time], dp2[i-1][time])


ans = 0
for i in range(1, n+1):
    for time in range(t+1):
        ans = max(
            ans,
            dp1[i-1][time] + dishes[i-1][1] + dp2[i][t - time]
        )
print(ans)

Submission status: Wrong Answer

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

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

# from top
for i in range(n):
    for time in range(t+1):
        if time + dishes[i][0] <= t:
            dp1[i+1][time+dishes[i][0]] = max(
                dp1[i][time+dishes[i][0]],
                dp1[i][time] + dishes[i][1]
            )
        dp1[i+1][time] = max(dp1[i][time], dp1[i+1][time])


for j in reversed(range(2, n+2)):
    i = n - j + 1
    for time in range(t+1):
        if time + dishes[i][0] <= t:
            dp2[i-1][time+dishes[i][0]] = max(
                dp2[i][time+dishes[i][0]],
                dp2[i][time] + dishes[i][1]
            )
        dp2[i-1][time] = max(dp2[i][time], dp2[i-1][time])


ans = 0
for i in range(n):
    for time in range(t+1):
        ans = max(
            ans,
            dp1[i][time] + dishes[i][1] + dp2[i+1][t - time]
        )
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
fs = [tuple(map(int, input().split())) for _ in range(N)]
# fs = sorted(fs)
dp = [0] * 6005

for a, b in fs:
    for t in range(T + a - 1, a - 1, -1):
        dp[t] = max(dp[t - a] + b, dp[t])
print(max(dp))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
fs = [tuple(map(int, input().split())) for _ in range(N)]
fs = sorted(fs)
dp = [0] * 6005

for a, b in fs:
    for t in range(T + a, a - 1, -1):
        dp[t] = max(dp[t - a] + b, dp[t])
print(max(dp))

Submission status: Wrong Answer

Solution code:

from itertools import chain
N, T = map(int, input().split())
A = [0]*(N+1)
B = [0]*(N+1)
for i in range(1, N+1):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

dp = [[-1]*(6000+1) for _ in range(3000+1)]
#dp[i][t]: 注文後にt秒になっている時、i番目までを考慮したとき、
#の最大
dp[0][0] = 0
for i in range(1, N+1):
    for t in range(T):
        dp[i][t+A[i]] = max(dp[i][t+A[i]], dp[i-1][t] + B[i])
        dp[i][t] = max(dp[i][t], dp[i-1][t])
print(max(chain.from_iterable(dp)))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
A = [0]*(N+1)
B = [0]*(N+1)
for i in range(1, N+1):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

dp = [[-1]*(6000+1) for _ in range(3000+1)]
#dp[i][t]: 注文後にt秒になっている時、i番目までを考慮したとき、
#の最大
dp[0][0] = 0
for i in range(1, N+1):
    for t in range(T):
        dp[i][t+A[i]] = max(dp[i][t+A[i]], dp[i-1][t] + B[i])
        dp[i][t] = max(dp[i][t], dp[i-1][t])
print(max(dp[N]))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
A = [0]*(N+1)
B = [0]*(N+1)
for i in range(1, N+1):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

dp = [[0]*(6000+1) for _ in range(3000+1)]
#dp[i][t]: 注文後にt秒になっている時、i番目までを考慮したとき、
#の最大

for i in range(1, N+1):
    for t in range(T):
        dp[i][t+A[i]] = max(dp[i][t+A[i]], dp[i-1][t] + B[i])
        dp[i][t] = max(dp[i][t], dp[i-1][t])
print(max(dp[N]))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from functools import lru_cache
import bisect
import copy
import heapq
import itertools
import math
import random
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, T = read_values()
    P = [tuple(read_values()) for _ in range(N)]

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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from functools import lru_cache
import bisect
import copy
import heapq
import itertools
import math
import random
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, T = read_values()
    P = [tuple(read_values()) for _ in range(N)]

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

    for i, (a, b) in enumerate(P):
        for t in range(T):
            s = min(T, t + a)
            dp[i + 1][s] = max(dp[i + 1][s], dp[i][t] + b)
            dp[i + 1][t] = max(dp[i + 1][t], dp[i][t])
    print(max(dp[N]))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
ab = []
for i in range(n):
    a,b = map(int, input().split())
    ab += [(a,b)]
ab.sort()
m = ab[-1][0]
dp = [[0]*(t+m) for _ in range(n)]
for i in range(t):
    dp[0][i+ab[0][0]] = ab[0][1]

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

ans = 0
for i in range(n):
    ans = max(ans, max(dp[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
ab = []
for i in range(n):
    a,b = map(int, input().split())
    ab += [(a,b)]
ab.sort()
m = ab[-1][0]
dp = [[0]*(t+m) for _ in range(n)]
for i in range(t):
    dp[0][i+ab[0][0]] = ab[0][1]

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

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

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
ab = []
for i in range(n):
    a,b = map(int, input().split())
    ab += [(a,b)]
ab.sort()
m = ab[-1][0]
dp = [[0]*(t+m) for _ in range(n)]
for i in range(t):
    dp[0][i+ab[0][0]] = ab[0][1]

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

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
ab = []
for i in range(n):
    a,b = map(int, input().split())
    ab += [(a,b)]
ab.sort()
dp = [[0]*(t+ab[-1][0]) for _ in range(n)]
for i in range(ab[0][0], t):
    dp[0][i] = ab[0][1]

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

Submission status: Wrong Answer

Solution code:
N, T = list(map(int, input().split()))
L = [list(map(int, input().split())) for _ in range(N)]
L = sorted(L, key=lambda x: x[1], reverse=True)
L = sorted(L, key=lambda x: x[0])
# print(L)

dp = [[0]*(T+3001) for _ in range(N+1)]
for i in range(1, N+1):
    l = L[i-1]
    for j in range(T):
        dp[i][j+l[0]] = max(dp[i-1][j]+l[1], dp[i-1][j+l[0]])
    for j in range(T, T+3001):
        dp[i][j] = max(dp[i-1][j], dp[i][j])
# print(dp)
print(max(dp[N]))

Submission status: Wrong Answer

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

dp = [[0]*(T+3001) for _ in range(N+1)]
for i in range(1, N+1):
    l = L[i-1]
    for j in range(T):
        dp[i][j+l[0]] = max(dp[i-1][j]+l[1], dp[i-1][j+l[0]])
    for j in range(T, T+3001):
        dp[i][j] = max(dp[i-1][j], dp[i][j])
# print(dp)
print(max(dp[N]))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = 10**6#float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
#from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, T = MAP()
AB = [LIST() for _ in range(N)]

t = [0]*T
ans = 0

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

ans = max(ans, max(t))
print(ans)

Submission status: Wrong Answer

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

ablist=[]
bsum=0
for _ in range(N):
  a,b=map(int,input().split())
  ablist.append((a,b))
  bsum+=b
ablist.sort()
  
dp=[[0]*T for _ in range(N+1)]
for i in range(1,N+1):
  a,b=ablist[i-1]
  for j in range(T):
    dp[i][j]=dp[i-1][j]
    if j-a>=0:
      dp[i][j]=max(dp[i][j],dp[i-1][j-a]+b)      
#print(dp[N])

if dp[N][T-1]==bsum:
  print(bsum)
else:
  eat_flag=[False]*N
  t=T-1
  for i in reversed(range(1,N+1)):
    if dp[i][t]>dp[i-1][t]:
      eat_flag[i-1]=True
      t-=ablist[i-1][0]      
  #print(eat_flag)
  
  bmax=0
  for i in range(N):
    if not eat_flag[i]:
      bmax=max(bmax,ablist[i][1])
      
  print(dp[N][T-1]+bmax)

Submission status: Wrong Answer

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

ablist=[]
bsum=0
for _ in range(N):
  a,b=map(int,input().split())
  ablist.append((a,b))
  bsum+=b
  
dp=[[0]*T for _ in range(N+1)]
for i in range(1,N+1):
  a,b=ablist[i-1]
  for j in range(T):
    dp[i][j]=dp[i-1][j]
    if j-a>=0:
      dp[i][j]=max(dp[i][j],dp[i-1][j-a]+b)      
#print(dp[N])

if dp[N][T-1]==bsum:
  print(bsum)
else:
  eat_flag=[False]*N
  t=T-1
  for i in reversed(range(1,N+1)):
    if dp[i][t]>dp[i-1][t]:
      eat_flag[i-1]=True
      t-=ablist[i-1][0]      
  #print(eat_flag)
  
  bmax=0
  for i in range(N):
    if not eat_flag[i]:
      bmax=max(bmax,ablist[i][1])
      
  print(dp[N][T-1]+bmax)

Submission status: Wrong Answer

Solution code:
import sys 
sys.setrecursionlimit(10**7)
N, T = map(int, input().split())
A = []
for _ in range(N):
  a, b = map(int,input().split())
  A.append((a, b))
A.sort(key=lambda x: x[1], reverse=True)
A.sort(key=lambda x: x[0])
ans = []
max_point = [-1 for _ in range(T)]



def solve(point, x, unvisit_list):
  if x >= T or len(unvisit_list) == 0:
    ans.append(point)
    return 0
  elif max_point[x] >= point:
    return 0
  else:
    max_point[x] = point
    for k in range(len(unvisit_list)):
      new_unvisit = unvisit_list[k+1:]
      if x + unvisit_list[k][0] >= T:
        ans.append(point+max(unvisit_list, key=lambda x: x[1])[1])
        return 0
      solve(point+unvisit_list[k][1], x + unvisit_list[k][0], new_unvisit)
    return 0

while A:
  item = A[0]
  del A[0]
  solve(item[1], item[0], A)
print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys 
sys.setrecursionlimit(10**7)
N, T = map(int, input().split())
A = []
for _ in range(N):
  a, b = map(int,input().split())
  A.append((a, b))
A.sort(key=lambda x: x[1], reverse=True)
A.sort(key=lambda x: x[0])
ans = []
max_point = [-1 for _ in range(T)]
for k in range(N):
  if A[k][0] > T-1:
    break
  else:
    max_point[A[k][0]] = max(max_point[A[k][0]], A[k][1])


def solve(point, x, unvisit_list):
  if x >= T or len(unvisit_list) == 0:
    ans.append(point)
    return 0
  elif max_point[x] > point:
    return 0
  else:
    max_point[x] = point
    for k in range(len(unvisit_list)):
      new_unvisit = unvisit_list[:k] + unvisit_list[k+1:]
      if x + unvisit_list[k][0] >= T:
        ans.append(point+max(unvisit_list, key=lambda x: x[1])[1])
        return 0
      solve(point+unvisit_list[k][1], x + unvisit_list[k][0], new_unvisit)
    return 0

while A:
  item = A[0]
  del A[0]
  solve(item[1], item[0], A)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
dp = [-float("inf") for i in range(t + 3000)]
dp[0] = 0
ab = []
for i in range(n):
    ab.append(list(map(int, input().split())))
ab.sort(key = lambda x: x[1])
for i in range(n):
    a, b = ab[i]
    for j in range(t - 1, -1, -1):
        dp[j + a] = max(dp[j + a], dp[j] + b)
print(max(dp))

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
dp = [-float("inf") for i in range(t + 3000)]
dp[0] = 0
for i in range(n):
    a, b = map(int, input().split())
    for j in range(t - 1, -1, -1):
        dp[j + a] = max(dp[j + a], dp[j] + b)
print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, T = map(int, input().split())
    dp = [0] * (T + 1)
    AB = []
    for k in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))

    AB.sort()
    maxdp = 0
    temp = 1
    for i in range(N):
        a, b = AB[i]  # a:time  b:value
        temp += a
        dp[T] = max(maxdp + b, dp[T])
        for j in range(min(T - 1, temp), a - 1, -1):
            if dp[j] < dp[j - 1] + b:
                dp[j] = dp[j - a] + b
                maxdp = max(dp[j], maxdp)

    print(dp[T])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, T = map(int, input().split())

    dp = [0] * (T + 1)

    AB = []

    for k in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))

    maxdp = 0

    for i in range(N):
        a, b = AB[i]  # a:time  b:value

        dp[T] =max( maxdp + b,dp[T])
        for j in range(T - 1, a - 1, -1):
            dp[j] = max(dp[j], dp[j - a] + b)
            maxdp= max(dp[j],maxdp)
    print(dp[T])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
dp = [[0 for j in range(T)] for i in range(N)]
W = []
V = []
for i in range(N):
    w, v = map(int, input().split())
    W += [w]
    V += [v]
    
W, V = zip(*sorted(zip(W, V), key=lambda x: x[1]))
for i in range(N-1):
    for j in range(T):
        if j - W[i] < 0 :
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j], dp[i][j-W[i]] + V[i])
ans = 0
for i in range(N):
    ans = max(ans, dp[i][-1] + V[i])
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
dp = [[0 for i in range(T)] for j in range(N)]
is_eat = [False for i in range(N)]
W = []
V = []
for i in range(N):
    w, v = map(int, input().split())
    W += [w]
    V += [v]
    
W, V = zip(*sorted(zip(W, V), key=lambda x: x[1]))

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

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

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
dp = [[0 for i in range(T)] for j in range(N+1)]
xy = [[(0, 0) for i in range(T)] for j in range(N+1)]
is_eat = [False for i in range(N)]
W = []
V = []
for i in range(N):
    w, v = map(int, input().split())
    W += [w]
    V += [v]
for i in range(N):
    for j in range(T):
        if j - W[i] >= 0 and dp[i][j] <= dp[i][j-W[i]] + V[i]:
            dp[i+1][j] = dp[i][j-W[i]] + V[i]
            xy[i+1][j] = (i, j-W[i])
        else:
            dp[i+1][j] = dp[i][j]
            xy[i+1][j] = (i, j)
x, y = N, T-1
while not (x == y == 0):
    px, py = xy[x][y]
    if y != py:
        is_eat[px] = True
    x, y = px, py
last_eat = 0
for i in range(N):
    if not is_eat[i]:
        last_eat = max(last_eat, V[i])

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

Submission status: Wrong Answer

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

dp = [[0]*W for _ in range(n+1)]
for i in range(1, n+1):
    w, v = ab[i-1]
    for j in range(W):
        if j-w < 0:
            continue
        if dp[i][j] < dp[i-1][j]:
            dp[i][j] = dp[i-1][j]
        if dp[i][j] < dp[i-1][j-w]+v:
            dp[i][j] = dp[i-1][j-w]+v


ans = 0
for i in range(n):
    a, b = ab[i]
    if ans < dp[i][W-1]+b:
        ans = dp[i][W-1]+b
print(ans)