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:
n,t = map(int,input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
point = ab[-1][1]
n -= 1
dp = [[0 for i in range(t+1)]for j in range(n+1)]
for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])
print(dp[-1][t-1]+point)

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
ab = sorted([list(map(int, input().split())) for i in range(n)])
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        else:
            dp[i+1][j] = max(dp[i][j],dp[i+1][j])

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = sorted([list(map(int, input().split())) for i in range(n)])
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for item in range(n):
    ti,vi = ab[item]
    for time in range(t+1):
        if time + ti <= t:
            dp[item+1][time+ti] = max(dp[item+1][time+ti],dp[item][time]+vi)
        else:
            dp[item+1][time] = max(dp[item][time],dp[item+1][time])

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab = sorted(ab)
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for item in range(n):
    ti,vi = ab[item]
    for time in range(t+1):
        if time + ti <= t:
            dp[item+1][time+ti] = max(dp[item+1][time+ti],dp[item][time]+vi)
        else:
            dp[item+1][time] = max(dp[item][time],dp[item+1][time])

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

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

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

for item in range(n):
    ti,vi = ab[item]
    for time in range(t+1):
        if time + ti <= t:
            dp[item+1][time+ti] = max(dp[item+1][time+ti],dp[item][time]+vi)
        else:
            dp[item+1][time] = max(dp[item][time],dp[item+1][time])

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

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

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

for item in range(n):
    ti,vi = ab[item]
    for time in range(t+1):
        if time + ti <= t:
            dp[item+1][time+ti] = max(dp[item+1][time+ti],dp[item][time]+vi)
        else:
            dp[item+1][time] = max(dp[item][time],dp[item+1][time])

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort()
ma = ab[-1][1]
ab = ab[:-1]

n -= 1

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

for item in range(n):
    ti,vi = ab[item]
    for time in range(t+1):
        if time + ti <= t:
            dp[item+1][time+ti] = max(dp[item+1][time+ti],dp[item][time]+vi)
        else:
            dp[item+1][time] = max(dp[item][time],dp[item+1][time])

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

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

n -= 1

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

for item in range(n):
    ti,vi = ab[item]
    for time in range(t+1):
        if time + ti <= t:
            dp[item+1][time+ti] = max(dp[item+1][time+ti],dp[item][time]+vi)
        else:
            dp[item+1][time] = max(dp[item][time],dp[item+1][time])

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort(key=lambda x:(x[1],x[0]))
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for item in range(n):
    for time in range(t+1):
        #間に合う
        if time -ab[item][0] >= 0:
            dp[item+1][time] = max(dp[item][time],dp[item][time-ab[item][0]]+ab[item][1])
        else:
            dp[item+1][time] = dp[item][time]

ma = 0
for i ,(ti,vi) in enumerate(ab):
    ma = max(ma,dp[i][t-1]+vi)
print(ma)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort()
point = ab[-1][1]
ab = ab[:-1]

n -= 1

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

for item in range(n):
    for time in range(t+10):
        #間に合う
        if time -ab[item][0] >= 0:
            dp[item+1][time] = max(dp[item][time],dp[item][time-ab[item][0]]+ab[item][1])
        else:
            dp[item+1][time] = dp[item][time]

print(dp[-1][t-1]+point)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort(key=itemgetter(1,0))
point = ab[-1][1]
ab = ab[:-1]

n -= 1

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

for item in range(n):
    for time in range(t+10):
        #間に合う
        if time -ab[item][0] >= 0:
            dp[item+1][time] = max(dp[item][time],dp[item][time-ab[item][0]]+ab[item][1])
        else:
            dp[item+1][time] = dp[item][time]

print(dp[-1][t-2]+point)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort(key=itemgetter(1,0))
point = ab[-1][1]
ab = ab[:-1]

n -= 1

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

for item in range(n):
    for time in range(t+10):
        #間に合う
        if time -ab[item][0] >= 0:
            dp[item+1][time] = max(dp[item][time],dp[item][time-ab[item][0]]+ab[item][1])
        else:
            dp[item+1][time] = dp[item][time]

print(dp[-1][t-1]+point)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort(key=itemgetter(1,0))
point = ab[-1][1]
ab = ab[:-1]

n -= 1
t -= 1

def knapsack(n,w,li):
    #li[i][0]が重さ,li[i][1]が価値(ときどき逆になることがあるので注意)
    dp = [[0 for i in range(w+1)]for j in range(n+1)]
    #dp[i][total_w]はi-1回目までの商品を加えた時に重さがtotal_wを超えないような値の最大値
    for i in range(n):
        for total_w in range(w+1):
            if total_w - li[i][0] >= 0:
                dp[i+1][total_w] = max(dp[i][total_w],dp[i][total_w-li[i][0]]+li[i][1])
            else:
                dp[i+1][total_w] = dp[i][total_w]
    return max(dp[-1])

print(knapsack(n,t,ab)+point)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort(key=itemgetter(1,0))

point = ab[-1][1]
ab = ab[:-1]

def knapsack(n,w,li):
    #li[i][0]が時間,li[i][1]が価値(ときどき逆になることがあるので注意)
    dp = [[0 for i in range(w+1)]for j in range(n+1)]
    #dp[i][total_w]はi-1回目までの商品を加えた時に重さがtotal_wを超えないような値の最大値
    for i in range(n):
        for total_w in range(w+1):
            if total_w - li[i][0] >= 0:
                dp[i+1][total_w] = max(dp[i][total_w],dp[i][total_w-li[i][0]]+li[i][1])
            else:
                dp[i+1][total_w] = dp[i][total_w]
    return dp[-1]
print(knapsack(n-1,t,ab)[t-1]+point)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort(key=itemgetter(1,0))

point = ab[-1][1]
ab = ab[:-1]

def knapsack(n,t,li):
    dp = [[0 for i in range(t)]for j in range(n)]
    for i in range(n-1):
        for total_t in range(t):
            if total_t - li[i][0] >= 0:
                dp[i+1][total_t] = max(dp[i][total_t],dp[i][total_t-li[i][0]]+li[i][1])
            else:
                dp[i+1][total_t] = dp[i][total_t]
    return max(dp[-1])

print(knapsack(n,t,ab)+point)

Submission status: Wrong Answer

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

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

dp = [[0]*(t+x[-1][0]) for i in range(n+1)]

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

ans = 0
for i in range(n):
    if dp[-i-1][t+x[-i-1][0]-1] != dp[-i-2][t+x[-i-1][0]-1]:
        ans = max(ans, dp[-i-1][t+x[-i-1][0]-1])

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

n,t = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
dp1 = [[-(10 ** 18)] * t for i in range(n)]
dp2 = [[-(10 ** 18)] * t for i in range(n)]
dp1[0][0] = 0
dp2[0][0] = 0
for i,(a,b) in enumerate(ab[:-1]):
  for j in range(t-a):
    dp1[i+1][j] = max(dp1[i+1][j],dp1[i][j])
    if j < t-a:
      dp1[i+1][j+a] = max(dp1[i+1][j+a],dp1[i][j]+b)
for i,(a,b) in enumerate(ab[n-1:0:-1]):
  for j in range(t):
    dp2[i+1][j] = max(dp2[i+1][j],dp2[i][j])
    if j < t-a:
      dp2[i+1][j+a] = max(dp2[i+1][j+a],dp2[i][j]+b)
ans = 0
for key,(i,j,(a,b)) in enumerate(zip(dp1,dp2[::-1],ab)):
  nowi,nowj = 0,0
  I,J = [],[]
  for x,y in zip(i,j):
    nowi = max(nowi,x)
    nowj = max(nowj,y)
    I.append(nowi)
    J.append(nowj)
  J.reverse()
  tmp = 0
  for x,y in zip(I,J):
    tmp = max(tmp,x+y)
  tmp += b
  ans = max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
N, T = map(int, input().split())
dp = [[0]*(T+1) for _ in range(N+1)]  #dp1[i][t]:[1,i]での食べ物でt-1以下で最大

li = []
for _ in range(N):
    a, b = map(int, input().split())
    k = a*(10**6)+b
    li.append(k)
li.sort()
#dp
for i in range(1, N+1):
    a = li[i-1] // (10**6)
    b = li[i-1] % (10**6)
    for t in range(1, T+1):
        if t - a >= 0:
            dp[i][t] = max(dp[i-1][t-a]+b, dp[i-1][t])
        else:
            dp[i][t] = max(dp[i-1][t], dp[i][t])

max_ = 0
for i in range(1, N+1):
    a = li[i-1] // (10**6)
    b = li[i-1] % (10**6)
    max_ = max((dp[i-1][T]+b, max_))
print(max_)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
N, T = map(int, input().split())
dp = [[0]*(T+1) for _ in range(N+1)]  #dp1[i][t]:[1,i]での食べ物でt-1以下で最大

li = []
for _ in range(N):
    a, b = map(int, input().split())
    k = a*(10**6)+b
    li.append(k)
    
#dp
for i in range(1, N+1):
    a = li[i-1] // (10**6)
    b = li[i-1] % (10**6)
    for t in range(1, T+1):
        if t - a >= 0:
            dp[i][t] = max(dp[i-1][t-a]+b, dp[i-1][t])
        else:
            dp[i][t] = max(dp[i-1][t], dp[i][t])

max_ = 0
for i in range(1, N+1):
    a = li[i-1] // (10**6)
    b = li[i-1] % (10**6)
    max_ = max((dp[i-1][T]+b, max_))
print(max_)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,T = map(int,input().split())
DP=[[0 for a in range(T+1)] for c in range(N)]
A0,B0 = map(int,input().split())
if A0<T:
 DP[0][A0]=B0
 b=B0
else:
 DP[0][T]=B0
 b=0
for i in range(1,N):
 A,B = map(int,input().split())
 DP[i][T]=max(DP[i-1][T],b+B)
 b=0
 for j in range(T):
  if j>=A:
   DP[i][j]=max(DP[i-1][j],DP[i-1][j-A]+B)
  else:
   DP[i][j]=DP[i-1][j]
  b=max(b,DP[i][j])
print(DP[N-1][T])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,T = map(int,input().split())
DP=[[0 for a in range(T+1)] for b in range(N)]
A0,B0 = map(int,input().split())
if A0<T:
 DP[0][A0]=B0
 b=B0
else:
 DP[0][T]=B0
 b=0
for i in range(1,N):
 A,B = map(int,input().split())
 DP[i][T]=max(DP[i-1][T],b+B)
 b=0
 for j in range(T):
  if j>=A:
   DP[i][j]=max(DP[i-1][j],DP[i-1][j-A]+B)
  else:
   DP[i][j]=DP[i-1][j]
  b=max(b,DP[i][j])
print(DP[N-1][T])

Submission status: Wrong Answer

Solution code:
from collections import*
import sys
input=sys.stdin.readline


d=defaultdict(list)
n,t=map(int,input().split())

x=sorted(list(map(int,input().split()))for i in range(n))
x=[x[i]+[i]for i in range(n)]


d[0]=[0,[]]
for time,value,idx in x:
    for k,v in d.copy().items():
        if k+time<t:
            if len(d[k+time])==2:
                if v[0]+value>=d[k+time][0]:
                    
                    d[k+time]=[v[0]+value,v[1]+[idx]]
            else:
                d[k+time]=[v[0]+value,v[1]+[idx]]
        else:
            break


ans=0
for v,k in sorted([[k,set(v)] for k,v in d.values()],reverse=1)[:200]:
    tmp=0
    for i in x:
        if i[2]not in k:
            if i[1]>tmp:
                tmp=i[1]
    if ans<v+tmp:
        ans=v+tmp
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import*
import sys
input=sys.stdin.readline


d=defaultdict(list)
n,t=map(int,input().split())

x=sorted(list(map(int,input().split()))for i in range(n))
x=[x[i]+[i]for i in range(n)]


d[0]=[0,[]]
for time,value,idx in x:
    for k,v in d.copy().items():
        if k+time<t:
            if len(d[k+time])==2:
                if v[0]+value>=d[k+time][0]:
                    
                    d[k+time]=[v[0]+value,v[1]+[idx]]
            else:
                d[k+time]=[v[0]+value,v[1]+[idx]]

ans=0
for v,k in sorted([[k,set(v)] for k,v in d.values()],reverse=1)[:500]:
    tmp=0
    for i in x:
        if i[2]not in k:
            if i[1]>tmp:
                tmp=i[1]
    if ans<v+tmp:
        ans=v+tmp
print(ans)

Submission status: Wrong Answer

Solution code:
# ABC145 - Virtual
def fact(n,mod):
    ret=1
    for i in range(2,n+1):
        ret=(ret*i)%mod
    return ret
# A
if False: print(int(input())**2)

# B
if False:
    N=int(input())
    S=input()
    print('Yes' if N%2==0 and S[:N//2]*2==S else 'No')
# C
if False:
    import itertools

    N=int(input())
    X=[tuple(map(int,input().split())) for _ in range(N)]
    ans=0
    for p in itertools.permutations(X):
        # print(p)
        sx,sy=p[0]
        for x,y in p:
            ans+=((sx-x)**2+(sy-y)**2)**0.5
            sx,sy=x,y
        # print(ans)
    ans/=fact(N,10**9+7)
    print(ans)

# D
if False:

    def pCq(p,q,mod):
        pCq=1
        for i in range(q):
            pCq*=p-i
            pCq=pCq%mod
        den=fact(q,mod)
        inv=pow(den,mod-2,mod)
        pCq=(pCq*inv)%mod
        return pCq
    
    X,Y=map(int,input().split())
    MOD=10**9+7
    ans=0
    n,m=2*X-Y,2*Y-X
    if n%3==0 and m%3==0:
        n//=3
        m//=3
        ans+=pCq(m+n,max(m,n),MOD)
    print(ans)

# E
if True:
    N,T=map(int,input().split())
    D=[tuple(map(int,input().split())) for _ in range(N)]
    D.sort(key=lambda x: x[1])
    ans=D.pop()[1]
    dp=[[-1 for j in range(T+1)] for i in range(N)]
    # print(dp)
    for i in range(N):
        dp[i][0]=0
    for j in range(T):
        dp[0][j]=0
    for i in range(1,N):
        for j in range(1,T+1):
            dp[i][j]=max(dp[i-1][j],dp[i-1][j-D[i-1][0]]+D[i-1][1] if j-D[i-1][0]>=0 else -1)
    ans+=dp[-1][-1]
    print(ans)

Submission status: Wrong Answer

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

dp = [-1]*(t+1)
dp[0] = 0

for a, b in ab:
    tmp = dp[:]
    for i in range(t):
        if dp[i] >= 0 and dp[i]+b > tmp[min(i+a, t)]:
            tmp[min(i+a, t)] = dp[i]+b
    dp = tmp
print(max(dp))

Submission status: Wrong Answer

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

dp = [0]*(t+1)

for a, b in ab:
    tmp = dp[:]
    for i in range(t):
        if dp[i]+b > tmp[min(i+a, t)]:
            tmp[min(i+a, t)] = dp[i]+b
    dp = tmp
print(dp[-1])

Submission status: Wrong Answer

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

dp = [-10000]*(t+1)
dp[0] = 0

for a, b in ab:
    tmp = dp[:]
    for i in range(t):
        if dp[i]+b > dp[min(i+a, t)]:
            tmp[min(i+a, t)] = dp[i]+b
    dp = tmp
print(dp[-1])

Submission status: Wrong Answer

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

dp = [0]*(t+1)

for a, b in ab:
    tmp = dp[:]
    for i in range(t):
        if dp[i]+b > dp[min(i+a, t)]:
            tmp[min(i+a, t)] = dp[i]+b
    dp = tmp
print(max(dp))

Submission status: Wrong Answer

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

dp = [0]*(t+1)

for a, b in ab:
    tmp = dp[:]
    for i in range(t):
        if dp[i]+b > dp[min(i+a, t)]:
            tmp[min(i+a, t)] = dp[i]+b
    dp = tmp
print(dp[-1])

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    dp1 = [[0]*(T+1) for _ in range(N+2)] # [0, i]
    dp2 = [[0]*(T+1) for _ in range(N+2)] # [i, N]

    # 貰うdp
    for i in range(N):
        cost, value = cv[i]

        # dp1
        for now in range(min(T+1, cost)):
            dp1[i+1][now] = dp1[i][now]

        for now in range(cost, T+1):
            old = now - cost
            dp1[i+1][now] = max(dp1[i][now], dp1[i][old] + value)

        # dp2
        cost, value = cv[(N-1)-1]
        for now in range(min(T+1, cost)):
            dp2[N-i][now] = dp2[N-(i-1)][now]

        for now in range(cost, T+1):
            old = now - cost
            dp2[N-i][now] = max(dp2[N-(i-1)][now], dp2[N-(i-1)][old] + value)
    
    ans = 0
    for i in range(1, N+1):
        i_value = cv[i-1][1]
        i_max = max([dp1[i-1][j] + dp2[i+1][(T-1)-j] + i_value for j in range(T)])
        ans = max(ans, i_max)
    print(ans)


    

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]

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

    # 貰うdp

    ans = 0
    for i in range(N):
        cost, value = cv[i]
        for now in range(min(T+1, cost)):
            dp[i+1][now] = dp[i][now]

        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)
        
        ans = max(ans, dp[i][T-1] + value)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    cv = sorted(cv, key=lambda x : x[1])

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


    # 貰うdp
    for i in range(N):
        cost, value = cv[i]
        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)

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

    print(dp_ext[N][T])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    cv = sorted(cv, key=lambda x : x[1])

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


    # 貰うdp
    for i in range(N):
        cost, value = cv[i]
        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)

        for j in range(1, T+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i+1][j-1])
        
        for j in range(1, T+1):
            dp_ext[i+1][j] = max(dp[i+1][j], dp[i][j-1] + value, dp_ext[i][j])

    print(dp_ext[N][T])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    cv = sorted(cv, key=lambda x : (x[0], -x[1]))

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


    # 貰うdp
    for i in range(N):
        cost, value = cv[i]
        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)

        for j in range(1, T+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i+1][j-1])
        
        for j in range(1, T+1):
            dp_ext[i+1][j] = max(dp[i+1][j], dp[i][j-1] + value, dp_ext[i][j])

    print(dp_ext[N][T])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    cv = sorted(cv, key=lambda x : x[0])

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


    # 貰うdp
    for i in range(N):
        cost, value = cv[i]
        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)

        for j in range(1, T+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i+1][j-1])
        
        for j in range(1, T+1):
            dp_ext[i+1][j] = max(dp[i+1][j], dp[i][j-1] + value, dp_ext[i][j])

    print(dp_ext[N][T])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]
    cv = sorted(cv, key=lambda x : x[1], reverse=True)

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


    # 貰うdp
    for i in range(N):
        cost, value = cv[i]
        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)

        for j in range(1, T+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i+1][j-1])
        
        for j in range(1, T+1):
            dp_ext[i+1][j] = max(dp[i+1][j], dp[i][j-1] + value, dp_ext[i][j])

    print(dp_ext[N][T])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]


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


    # 貰うdp
    for i in range(N):
        cost, value = cv[i]
        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)

        for j in range(1, T+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i+1][j-1])
        
        for j in range(1, T+1):
            dp_ext[i+1][j] = max(dp[i+1][j], dp[i][j-1] + value, dp_ext[i][j])

    print(dp_ext[N][T])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    cv = [tuple(map(int, input().split())) for _ in range(N)]


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


    # 配るdp
    for i in range(N):
        cost, value = cv[i]
        for now in range(cost, T+1):
            old = now - cost
            dp[i+1][now] = max(dp[i][now], dp[i][old] + value)

        for j in range(1, T+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i+1][j-1])
        
        for j in range(1, T+1):
            dp_ext[i+1][j] = max(dp[i+1][j], dp[i][j-1] + value, dp_ext[i][j])

    print(dp_ext[N][T])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, T = map(int,input().split())
AB = [0] * N
sum = 0
ans = 0
for i in range(N):
    AB[i] = list(map(int,input().split()))
    AB[i].append(AB[i][1]/AB[i][0])

AB.sort(key=lambda x: x[2])

while T > sum and len(AB) != 0:
    for i in range(1, len(AB)+1):
        if AB[-i][0] < T-sum:
            sum += AB[-i][0]
            ans += AB[-i][1]
            AB.pop(-i)
            break
        if i == len(AB):
            sum += AB[-1][0]
            ans += AB[-1][1]
            AB.pop(-1)
            break
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,boundary=1,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

def gcj(t,*x):
    print("Case #"+str(t)+":",*x)
    return

n,t=LI()
N=6001
N=31
dp=[[0]*t for i in range(n+1)]
c=[]
for i in range(n):
    a,b=LI()
    c+=[(a,b)]
c.sort(key=lambda x:x[0]*4000-x[1])
for i in range(n):
    a,b=c[i]
    ans=max(ans,dp[i][-1]+b)
    for j in range(t+N):
        if j+a<t:
            dp[i+1][j+a]=max(dp[i][j]+b,dp[i][j+a])

print(ans)