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:
#ABC145 E
N,T = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N)]
ab.sort()

a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
    
dp = [[0]*T for _ in range(N+1)]
# i番目より前まで注文してT分以内(max T-1)で完食する
def chmax(a,b):
    if a < b:
        return b
    else:
        return a 
    
for i in range(N):
    for t in range(T): #T-1分がmax
        if t+a[i] <= T-1:
        	dp[i+1][t+a[i]] =chmax(dp[i+1][t+a[i]],dp[i][t]+b[i])
        
        dp[i+1][t] = chmax(dp[i+1][t],dp[i][t])

#一番遅いものをラスト(T-1分)に注文
#i番目をラストに注文 = dp[i][t-1] + b[i]
ans = 0
for i in range(N):
    ans = chmax(ans,dp[i][t-1]+b[i])
    
print(ans)

Submission status: Wrong Answer

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

dp = [[0]*T for _ in range(N+1)]
# i番目より前まで注文してT分以内(max T-1)で完食する
def chmax(a,b):
    if a < b:
        return b
    else:
        return a 
    
for i in range(N):
    for t in range(T): #T-1分がmax
        if t+ab[i][0] <= T-1:
        	dp[i+1][t+ab[i][0]] =chmax(dp[i+1][t+ab[i][0]],dp[i][t]+ab[i][1])
        
        dp[i+1][t] = chmax(dp[i+1][t],dp[i][t])

#一番遅いものをラスト(T-1分)に注文
#i番目をラストに注文 = dp[i][t-1] + b[i]
ans = 0
for i in range(N):
    ans = chmax(ans,dp[i][t-1]+ab[i][1])
    
print(ans)

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 for _ in range(N+1)]
# i番目より前まで注文してT分以内(max T-1)で完食する
def chmax(a,b):
    if a < b:
        return b
    else:
        return a 
    
for i in range(N):
    for t in range(T): #T-1分がmax
        if t+ab[i][0] <= T-1:
        	dp[i+1][t+ab[i][0]] =chmax(dp[i+1][t+ab[i][0]],dp[i][t]+ab[i][1])
        
        dp[i+1][t] = chmax(dp[i+1][t],dp[i][t])

#一番遅いものをラスト(T-1分)に注文
#i番目をラストに注文 = dp[i][t-1] + b[i]
ans = 0
for i in range(N):
    ans = chmax(ans,dp[i][t-1]+ab[i][1])
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[[[0,0] for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
foodmax=[0]*n
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
foodmax[-1]=food[-1][1]
for i in range(n-1)[::-1]:
  foodmax[i]=max(foodmax[i+1],food[i][1])
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j][0]=max(dp[i][j][0],dp[i][j-food[i][0]][0]+food[i][1])
      if dp[i][j][0]>=dp[i][j-food[i][0]][0]+food[i][1]:
        dp[i+1][j][1]=dp[i][j][1]
      else:
        dp[i+1][j][1]=i
if dp[-1][-1][1]==n-1:print(dp[-1][-1][0]);exit()
ans=0
for i in range(n+1):
  if ans<dp[i][t-1][0]+foodmax[dp[i][t-1][1]+1]:
    ans=dp[i][t-1][0]+foodmax[dp[i][t-1][1]+1]
print(ans)

Submission status: Wrong Answer

Solution code:
from numpy import*
(_,t),*l=loadtxt(open(0))
d=zeros(1e4,int)
for a,b in l:p=d[a:a+t];maximum(p,d[:t]+b,p)
print(max(d))

Submission status: Wrong Answer

Solution code:
inf = float('inf')

N, T = map(int,input().split())
A = []; B = []
for _ in range(N):
    a, b = map(int,input().split())
    A.append(a)
    B.append(b)

A = sorted(A)

M = T + max(A)
dp = [[-inf] * (M) for _ in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for t in range(M):
        if t < 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 = max(dp[N])
print(ans)

Submission status: Wrong Answer

Solution code:
from numpy import*
(N,T),*L=loadtxt(open(0),int)
d=zeros(1e4,int)
for w,v in sort(L,0):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Wrong Answer

Solution code:
from numpy import*
(N,T),*L=loadtxt(open(0),int)
d=zeros(1e4,int)
for w,v in sort(L):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Wrong Answer

Solution code:
from numpy import*
N,T,*L=int64(open(0).read().split())
d=zeros(1e4)
for w,v in sorted(zip(*[iter(L)]*2)):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Wrong Answer

Solution code:
inf = float('inf')

N, T = map(int,input().split())
A = []; B = []
for _ in range(N):
    a, b = map(int,input().split())
    A.append(a)
    B.append(b)

M = T + max(A)
dp = [[-inf] * (M) for _ in range(N+1)]
dp[0][0] = 0

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 = -inf
for line in dp:
    for res in line:
        ans = max(ans, res)

print(ans)

Submission status: Wrong Answer

Solution code:
inf = float('inf')

N, T = map(int,input().split())
A = []; B = []
for _ in range(N):
    a, b = map(int,input().split())
    A.append(a)
    B.append(b)

M = T + max(A)
dp = [[-inf] * (M) for _ in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for t in range(M):
        if t < 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 = -inf
for line in dp:
    for res in line:
        ans = max(ans, res)

print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
A = [0 for i in range(N)]
B = [0 for i in range(N)]
for i in range(N):
    A[i], B[i] = map(int, input().split())
maxT = T + max(A)
dp = [[0 for j in range(maxT)] for i in range(N)]
gans = B[0]
dp[0][A[0]] = B[0]
for i in range(1,N):
    for j in range(maxT):
        nowans = dp[i-1][j]
        if j >= A[i] and j <= T-1+A[i]:
            nowans = max(nowans, dp[i-1][j-A[i]] + B[i])
        dp[i][j] = nowans
        gans = max(gans, nowans)
#print(dp)
print(gans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
food = [list(map(int, input().split())) for _ in range(n)]
food = sorted(food, key=lambda x: x[1])
dp_food = food[:-1]
last_food = food[-1]
dp = [[0 for _ in range(t-1)] for _ in range(n-1)]
for i in range(t-1):
    if i >= food[0][0]:
        dp[0][i] = food[0][1]
for i in range(1, n-1):
    for j in range(t-1):
        time = food[i][0]
        if j >= time:
            dp[i][j] = max(food[i][1]+dp[i-1][j-time], dp[i-1][j])
        else:
            dp[i][j] = dp[i-1][j]
print(dp[-1][-1]+last_food[1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# coding: utf-8
 
# 整数列 '1 2' > 整数型タプル
def IsIt(): return map(lambda x: int(x), input().split())
 
def temp_dp(n,w,weight,value):
    dp = [[0]*(w+1)] #初期化 dpij i皿までを使って，j秒までに食べ切れる皿の組み合わせ
 
    for i in range(n): #loop about n(dish) 0~n
        dp_ip1 = []
        for i_w in range(w+1): #loop about t 0~t
            if weight[i]<=i_w:
                # 漸化式
                dp_ip1.append(max(dp[i][i_w-weight[i]]+value[i], dp[i][i_w]))
            else:
                # 漸化式
                dp_ip1.append(dp[i][i_w])
 
        dp.append(dp_ip1)
        
    return dp[n][w]
 
# weight = [2,1,3,2,1,5]
# value = [3,2,6,1,3,85]
# n = 6
# w = 8
 
# print(bdtemp_dp(n,w,weight,value)[n][w])
 
n,t = IsIt()
time = []
point = []
point_max = 0
# point_maxi = 0
point_maxt = 0
for i in range(n):
    a, b= IsIt()
    if (b ==  point_max and point_maxt < a) or (b >  point_max):
        point_max = b
        point_maxt = a
        point_maxi = i
    time.append(a)
    point.append(b)
 
del time[i]
del point[i]
 
print(temp_dp(n-1,t-1,time,point)+point_max)

Submission status: Wrong Answer

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

def solve():
  n,t  = (int(i) for i in input().split())
  query = []
  for i in range(n):
    a,b = (int(i) for i in input().split())
    query.append((b,a))
  query= sorted(query,reverse = False)
  Inf = float("inf")
  dp = [[-1*Inf]*(t) for _ in range(n+1)]
  for i in range(t):
    dp[0][i] = 0

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

  ans = 0
  ans = max(ans,dp[n-1][t-1])
  #print(dp[n-1])
  b,a = query[-1]
  #print(b)
  print(ans+b)
solve()

Submission status: Wrong Answer

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

def solve():
  n,t  = (int(i) for i in input().split())
  query = []
  for i in range(n):
    a,b = (int(i) for i in input().split())
    query.append((a,b))
  query= sorted(query,reverse = False)
  Inf = float("inf")
  dp = [[-1*Inf]*(t) for _ in range(n+1)]
  for i in range(t):
    dp[0][i] = 0

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

  ans = 0
  for time in range(t):
    ans = max(ans,dp[n-1][time])
  #print(dp[n-1])
  a,b = query[-1]
  #print(b)
  print(ans+b)
solve()

Submission status: Wrong Answer

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

def solve():
  n,t  = (int(i) for i in input().split())
  query = []
  for i in range(n):
    a,b = (int(i) for i in input().split())
    query.append((b/a,a,b))
  query= sorted(query,reverse = False)
  Inf = float("inf")
  dp = [[-1*Inf]*(t) for _ in range(n+1)]
  for i in range(t):
    dp[0][i] = 0

  for i in range(n):
    _,a,b = query[i]
    for j in range(t):
      if j+a <= t-1:
        dp[i+1][j+a] = max(dp[i][j+a],dp[i][j]+b)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])
      else:
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])

  ans = 0
  for time in range(t):
    ans = max(ans,dp[n-1][time])
  #print(dp[n-1])
  _,a,b = query[-1]
  #print(b)
  print(ans+b)
solve()

Submission status: Wrong Answer

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


N, T = map(int, input().split())
AB = []
for _ in range(N):
    a, b = map(int, input().split())
    AB.append([a,b])

# Aを昇順にソート
AB.sort(key=lambda x:x[0])

# 残りのうち(j+1番目以降)で最も美味しいもの
bmax_res = [0]*N
bmax = -1
for i in range(N-1, 0, -1):
    a, b = AB[i]
    bmax = max(bmax, b)
    bmax_res[i-1] = bmax

# DPで解く
# DP[i][j] = i番目までの料理でj分以内に完食できる美味しさの合計の最大値
DP = [[0] * (T+1) for _ in range(N+1)]

res = 0
# update
for i in range(1,N+1):
    a, b = AB[i-1]
    for j in range(1,T+1):
        if a <= j:
            DP[i][j] = max(DP[i-1][j-a] + b, DP[i-1][j])
        else:
            DP[i][j] = max(DP[i-1][j], DP[i][j])
    res = max(res, DP[i][-1]+bmax_res[i-1])
print(res)

Submission status: Wrong Answer

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


N, T = map(int, input().split())
AB = []
for _ in range(N):
    a, b = map(int, input().split())
    AB.append([a,b])

# Aを昇順にソート
AB.sort(key=lambda x:x[0])

# 残りのうち(j+1番目以降)で最も美味しいもの
bmax_res = [0]*N
bmax = -1
for i in range(N-1, 0, -1):
    a, b = AB[i]
    bmax = max(bmax, b)
    bmax_res[i-1] = bmax

# DPで解く
# DP[i][j] = i番目までの料理でj分以内に完食できる美味しさの合計の最大値
DP = [[0] * (T+1) for _ in range(N+1)]

res = 0
# update
for i in range(1,N+1):
    a, b = AB[i-1]
    for j in range(1,T+1):
        if a <= j:
            DP[i][j] = max(DP[i-1][j-a] + b, DP[i-1][j])
        else:
            DP[i][j] = max(DP[i-1][j], DP[i][j])
        res = max(res, DP[i][-1]+bmax_res[i-1])
print(res)

Submission status: Wrong Answer

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

def solve():
  n,t  = (int(i) for i in input().split())
  query = []
  for i in range(n):
    a,b = (int(i) for i in input().split())
    query.append((b,a))
  query= sorted(query,reverse = False)
  Inf = float("inf")
  dp = [[-1*Inf]*(t) for _ in range(n+1)]
  for i in range(t):
    dp[0][i] = 0

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

  ans = 0
  for time in range(t):
    ans = max(ans,dp[n-1][time])
  #print(dp[n-1])
  b,a = query[-1]
  #print(b)
  print(ans+b)
solve()

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3005)
ans=chk=0
w=[]
for i in range(n):
    a,b=map(int,input().split())
    w.append((b,-a))
w.sort()

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

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3005)
ans=chk=0
w=[]
for i in range(n):
    a,b=map(int,input().split())
    w.append((b,a))
w.sort()

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

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3005)
ans=chk=0
for i in range(n):
    a,b=map(int,input().split())
    for j in range(t-2,-1,-1):
        dp[j+a+1]=max(dp[j+a+1],dp[j+1]+b)
        ans=max(ans,dp[j+a+1])
print(ans)

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3005)
ans=chk=0
for i in range(n):
    a,b=map(int,input().split())
    for j in range(t-1,-1,-1):
        dp[j+a+1]=max(dp[j+a+1],dp[j]+b)
        ans=max(ans,dp[j+a+1])
print(ans)

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3005)
ans=chk=0
for i in range(n):
    a,b=map(int,input().split())
    for j in range(t-1,-1,-1):
        if dp[j]==0: dp[j+a]=dp[j]+b
        elif j<t: dp[j+a+1]=max(dp[j+a+1],dp[j]+b)
        ans=max(ans,dp[j+a],dp[j+a+1])
print(ans)

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3001)
ans=chk=0
for i in range(n):
    a,b=map(int,input().split())
    a+=1
    for j in range(t-1,-1,-1):
        dp[j+a]=max(dp[j+a],dp[j]+b)
        ans=max(ans,dp[j+a])
print(ans)

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3001)
ans=chk=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)
        ans=max(ans,dp[j+a])
print(ans)

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[0]*(t+3000)
ans=chk=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)
        ans=max(ans,dp[j+a])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, T = map(int, readline().split())
m = map(int, read().split())
AB = sorted(zip(m, m))

dp = np.zeros(T, np.int64)

ans = 0
table = np.zeros(T, int)
for a, b in AB:
    ans = max(ans, max(table) + b)
    np.maximum(table[:-a] + b, table[a:], out=table[a:])
    print(table)
print(ans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a_max = max([y[0] for y in x])

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(ai):
        dp[j] = dp_pre[j]
    for j in range(ai, t + ai):
        dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi)
    for j in range(t + ai, t + a_max):
        dp[j] = dp_pre[j]
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
D = []
for i in range(N):
    t, v = map(int, input().split())
    D.append((t, v))

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

DP1 = [[0 for j in range(M)] for i in range(N + 2)]
DP2 = [[0 for j in range(M)] for i in range(N + 2)]

for i in range(1, N + 1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP1[i][j] = max(DP1[i - 1][j], DP1[i - 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP1[i][j] = DP1[i - 1][j]

for i in range(N, 0, -1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP2[i][j] = max(DP2[i + 1][j], DP2[i + 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP2[i][j] = DP2[i + 1][j]

mv = 0
for i in range(N):
    for j in range(M):
        mv = max(DP1[i][j] + DP2[i + 2][M - 2 - j] + D[i][1], mv)

print(mv)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
para = [0 for j in range(N)]
for i in range(N):
    a,b = map(int,input().split())
    para[i] = [a,b]
para = sorted(para,key=lambda x:x[1])
dp = [[-1 for i in range(T+2)] for j in range(N+1)]
dp[0][0] = 0
for i in range(N):
    for j in range(T+1):
        if j  > T - 0.5:
            dp[i+1][T+1] = max(dp[i+1][T+1],dp[i][j])
        elif j + para[i][0] > T - 0.5:
            dp[i+1][T+1] = max(dp[i+1][T+1],dp[i][j]+para[i][1])
        else:
            dp[i+1][j+para[i][0]] = max(dp[i][j+para[i][0]],dp[i][j]+para[i][1])
            dp[i+1][j] = max(dp[i+1][j],dp[i][j])
print(max(dp[N]))

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a_max = max([y[1] for y in x])

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(t + a_max):
        dp[j] = dp_pre[j]
        if 0 <= j - ai < t:
            dp[j] = max(dp[j], dp_pre[j - ai] + bi)
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a_max = max([y[1] for y in x])

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(t + a_max):
        dp[j] = max(dp_pre[j], dp[j])
        if 0 <= j - ai <= t:
            dp[j] = max(dp[j], dp_pre[j - ai] + bi)
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
T_MAX = T + 3000
dp = [[0 for _ in range(T_MAX+1)] for _ in range(N+1)]

for i in range(1, N+1):
    A, B = map(int, input().split())
    for j in range(T+A):
        dp[i][j] = max(dp[i][j], dp[i-1][j])
        if j >= A:
            dp[i][j] = max(dp[i][j], dp[i-1][j-A] + B)
print(max(dp[N]))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
# 美味しさのMAX
Tmax = 6001
dp = [[-1] * (Tmax) for i in range(N+1)]
dp[0][0] = 0
for i in range(N):
    dp[i+1][:] = dp[i][:]
    a,b = map(int, input().split())
    for j in range(T):    
        if dp[i][j] >= 0:
            dp[i+1][a+j] = max(dp[i][a+j], dp[i][j] + b)
print(max(dp[N]))
# print(dp[N])

Submission status: Wrong Answer

Solution code:
n, t = 0, 0
foods = []


def main():
    global n, t, foods
    n, t = map(int, input().split(' '))
    for _ in range(n):
        foods.append(list(map(int, input().split(' '))))

    b_max = -1
    idx = 0
    for i, f in enumerate(foods):
        if f[1] > b_max:
            b_max = f[1]
            idx = i
    del foods[idx]

    w_s = []
    v_s = []
    for f in foods:
        w_s.append(f[0])
        v_s.append(f[1])

    v = knapsack(n-1, t-1, w_s, v_s)
    print(v+b_max)


def knapsack(N, W, weight, value):
    inf = float("inf")
    dp = [[-inf for i in range(W+1)] for j in range(N+1)]
    for i in range(W+1):
        dp[0][i] = 0

    for i in range(N):
        for w in range(W+1):
            if weight[i] <= w:
                dp[i+1][w] = max(dp[i][w-weight[i]]+value[i], dp[i][w])
            else:
                dp[i+1][w] = dp[i][w]
    return dp[N][W]


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, t = 0, 0
foods = []


def main():
    global n, t, foods
    n, t = map(int, input().split(' '))
    for _ in range(n):
        foods.append(list(map(int, input().split(' '))))

    b_max = -1
    idx = 0
    for i, f in enumerate(foods):
        if f[1] > b_max:
            b_max = f[1]
            idx = i
    del foods[idx]

    w_s = []
    v_s = []
    for f in foods:
        w_s.append(f[0])
        v_s.append(f[1])

    v = knapsack(n-1, t, w_s, v_s)
    print(v+b_max)


def knapsack(N, W, weight, value):
    inf = float("inf")
    dp = [[-inf for i in range(W+1)] for j in range(N+1)]
    for i in range(W+1):
        dp[0][i] = 0

    for i in range(N):
        for w in range(W+1):
            if weight[i] <= w:
                dp[i+1][w] = max(dp[i][w-weight[i]]+value[i], dp[i][w])
            else:
                dp[i+1][w] = dp[i][w]
    return dp[N][W]


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
x=[None] # x[0]
for i in range(N):
    x.append(tuple(map(int,input().split()))) # x[1]~x[N]
# print(x) #DB★

dp1=[[0 for j in range(T)] for i in range(N+2)] # 0~N+1 dp1[i]はx[1]からx[i]まで使用（dp1[0]は使用するx[i]なしの初期値，dp1[N+1]は余り）
for i in range(1,N+1): # dp1[0]は初期値なのでいじらない。dp1[N+1]は余りなので計算しない。dp1[N]まで計算するので，iはNまで
    for j in range(T): # 0分後からT-1分後まで計算する
        if x[i][0]>j:
            dp1[i][j]=dp1[i-1][j]
        else:
            dp1[i][j]=max(dp1[i-1][j],dp1[i-1][j-x[i][0]]+x[i][1])

# print(dp1) #DB★

dp2=[[0 for j in range(T)] for i in range(N+2)] # 0~N+1 dp2[i]はx[i]からx[N]まで（x[N]からx[i]まで）使用（dp2[N+1]は使用するx[i]なしの初期値，dp2[0]は余り）
for i in range(N,0,-1): # dp2[N+1]は初期値なのでいじらない。dp2[0]は余りなので計算しない。dp2[1]まで計算するので，iは1まで
    for j in range(T):
        if x[i][0]>j:
            dp2[i][j]=dp2[i+1][j]
        else:
            dp2[i][j]=max(dp2[i-1][j],dp2[i-1][j-x[i][0]]+x[i][1])

# print(dp2) #DB★

cans=[0 for i in range(N+1)] # i番目の料理を最後に食べる場合の満足度の最大値
for i in range(1,N+1):
    cans[i]=max([dp1[i-1][j]+dp2[i+1][T-1-j] for j in range(T)])+x[i][1]

ans=max(cans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
x=[]
for i in range(N):
    Ai,Bi=map(int,input().split())
    x.append((Ai,Bi))

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

dp=[[0 for j in range(T)] for i in range(N)] # i番目までの料理（最後の料理を含まない）でj分後（<=T-1分後）までに得られる満足度の最大値（単純dp）

# 単純dp
for i in range(N-1):
    for j in range(T):
        if x[i][0]>j:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-x[i][0]]+x[i][1])

print(dp[N-1][T-1]+x[-1][1])

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
x=[]
for i in range(N):
    Ai,Bi=map(int,input().split())
    x.append((Ai,Bi)) # xでの添字は0-initial

x.sort() # Aiにつき昇順にソート

# print(x) #DB

dp=[[0 for j in range(T)] for i in range(N)] # i番目(1-initial)までの料理（最後(N番目)の料理を含まない。1 <= i <= N-1）でj分後（<=T-1分後）までに得られる満足度の最大値（単純dp）

# 単純dp
for i in range(N-1): # 0 <= i <= N-2
    for j in range(T): # 0 <= j <= T-1
        if x[i][0]>j: # x[i]はi+1番目の料理
            dp[i+1][j]=dp[i][j] # なのでx[i]まで検討した結果をdp[i+1]に入れる
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-x[i][0]]+x[i][1])

print(dp[N-1][T-1]+x[-1][1])

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
x=[]
for i in range(N):
    Ai,Bi=map(int,input().split())
    x.append((Ai,Bi)) # xでの添字は0-initial

x.sort() # Aiにつき昇順にソート

print(x) #DB

dp=[[0 for j in range(T)] for i in range(N)] # i番目(1-initial)までの料理（最後(N番目)の料理を含まない。1 <= i <= N-1）でj分後（<=T-1分後）までに得られる満足度の最大値（単純dp）

# 単純dp
for i in range(N-1): # 0 <= i <= N-2
    for j in range(T): # 0 <= j <= T-1
        if x[i][0]>j: # x[i]はi+1番目の料理
            dp[i+1][j]=dp[i][j] # なのでx[i]まで検討した結果をdp[i+1]に入れる
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-x[i][0]]+x[i][1])

print(dp[N-1][T-1]+x[-1][1])

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
x=[]
for i in range(N):
    Ai,Bi=map(int,input().split())
    x.append((Ai,Bi)) # xでの添字は0-initial

x.sort(key=lambda y:(y[1],y[0])) # Biにつき昇順，次いでAiにつき昇順にソート

dp=[[0 for j in range(T)] for i in range(N)] # i番目(1-initial)までの料理（最後(N番目)の料理を含まない。1 <= i <= N-1）でj分後（<=T-1分後）までに得られる満足度の最大値（単純dp）

# 単純dp
for i in range(N-1): # 0 <= i <= N-2
    for j in range(T): # 0 <= j <= T-1
        if x[i][0]>j: # x[i]はi+1番目の料理
            dp[i+1][j]=dp[i][j] # なのでx[i]まで検討した結果をdp[i+1]に入れる
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-x[i][0]]+x[i][1])

print(dp[N-1][T-1]+x[-1][1])

Submission status: Wrong Answer

Solution code:
def main():
	_, t, *L = map(int, open(0).read().split())
	d = [0] * t
	cand = 0
	for w, v in sorted((x, y) for x, y in zip(*[iter(L)] * 2)):
		if cand < d[-2] + v:
			cand = d[-2] + v
		for i in range(t - 1, w - 1, -1):
			if d[i - w] + v > d[i]:
				d[i] = d[i - w] + v
	print(max(cand, max(d)))

if __name__=="__main__":
	main()

Submission status: Wrong Answer

Solution code:
def main():
    N, T = map(int, input().split())
    l = []
    for _ in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    l.sort(key=lambda x: x[0])
    m = [[0] * T for _ in range(N)]
    m[0] = [0 if t < l[0][0] else l[0][1] for t in range(T)]
    for i in range(1, N):
        t, v = l[i]
        for j in range(t, T):
            m[i][j] = max(l[i][1] + m[i-1][j-t], m[i-1][j])
    return max(l[i][1] + m[i-1][-2] if i > 0 else l[i][1] for i in range(N))

print(main())

Submission status: Wrong Answer

Solution code:
def main():
    N, T = map(int, input().split())
    l = []
    for _ in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    l.sort(key=lambda x: x[0])
    m = [[0] * T for _ in range(N)]
    m[0] = [0 if t < l[0][0] else l[0][1] for t in range(T)]
    for i in range(1, N):
        t, v = l[i]
        for j in range(t, T):
            m[i][j] = max(l[i][1] + m[i-1][j-t], m[i-1][j])
    return max(l[i][1] + m[i-1][-1] if i > 0 else l[i][1] for i in range(N))

print(main())

Submission status: Wrong Answer

Solution code:
def main():
    N, T = map(int, input().split())
    l = []
    for _ in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    l.sort(key=lambda x: x[0])
    m = [[0] * T for _ in range(N)]
    m[0] = [0 if t <= l[0][0] else l[0][1] for t in range(T)]
    for i in range(1, N):
        t, v = l[i]
        for j in range(t+1, T):
            m[i][j] = max(l[i][1] + m[i-1][j-t-1], m[i-1][j])
    return max(l[i][1] + m[i-1][-2] if i > 0 else l[i][1] for i in range(N))

print(main())

Submission status: Wrong Answer

Solution code:
def main():
    N, T = map(int, input().split())
    l = []
    for _ in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    l.sort(key=lambda x: x[0])
    m = [[0] * T for _ in range(N)]
    m[0] = [0 if t <= l[0][0] else l[0][1] for t in range(T)]
    for i in range(1, N):
        t, v = l[i]
        for j in range(t+1, T):
            m[i][j] = max(l[i][1] + m[i-1][j-t], m[i-1][j])
    return max(l[i][1] + m[i-1][-2] if i > 0 else l[i][1] for i in range(N))

print(main())

Submission status: Wrong Answer

Solution code:
def main():
    N, T = map(int, input().split())
    l = []
    for _ in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    l.sort(key=lambda x: x[0])
    m = [[0] * T for _ in range(N)]
    m[0] = [0 if t <= l[0][0] else l[0][1] for t in range(T)]
    for i in range(1, N):
        t, v = l[i]
        for j in range(t+1, T):
            m[i][j] = max(l[i][1] + m[i-1][j-t], m[i-1][j])
    return max(l[i][1] + m[i-1][-1] if i > 0 else l[i][1] for i in range(N))

print(main())

Submission status: Wrong Answer

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

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


t -= 1


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

j = 0
for i in range(n):
    for j in range(t+1):
        if j < dishes[i][0] :
            dp[i+1][j] = dp[i][j]
        else:
            if dp[i][j] > dp[i][j-dishes[i][0]  ]+dishes[i][1]:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = dp[i][j-dishes[i][0]  ]+dishes[i][1]

# 食べてないやつを取り出してくる（動的計画法の経路復元的な）
not_eaten = [b for a,b in dishes]
i = n
j = t
while i > 0 and j > 0 :
    if dp[i][j] == dp[i-1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j-1]:
        j -= 1
    else:
        not_eaten.pop(i-1)
        i -= 1
        j -= dishes[i-1][0]

if not_eaten == []:
    last = 0
else:
    last = max(not_eaten)

print(dp[n][t]+last)

Submission status: Wrong Answer

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

t -= 1

for i in range(n):
  a[i], b[i] = [int(_) for _ in input().split()]

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

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

# 食べてないやつを取り出してくる（動的計画法の経路復元的なやつがしたい）
not_eaten = b[:]
i = n
j = t
while i > 0 and j > 0 :
    if dp[i][j] == dp[i-1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j-1]:
        j -= 1
    else:
        not_eaten.pop(i-1)
        i -= 1
        j -= a[i]

if not_eaten == []:
    last = 0
else:
    last = max(not_eaten)

print(dp[n][t] + last)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
import numpy as np
A,B = np.array([list(map(int,input().split())) for _ in range(N)]).T
dp1 = np.array([[0]*T for _ in range(N+1)])
rev_A,rev_B = A[::-1],B[::-1]
dp2 = np.array([[0]*T for _ in range(N+1)])


for i in range(1,N+1):
  a = A[i-1]
  rev_a = rev_A[i-1]
  b = B[i-1]
  rev_b = rev_B[i-1]
  
  dp1[i,:a] = dp1[i-1,:a]
  dp2[i,:rev_a] = dp2[i-1,:rev_a]
  dp1[i,a:] = np.fmax(dp1[i-1,a:],dp1[i-1,:-a]+b)
  dp2[i,rev_a:] = np.fmax(dp2[i-1,rev_a:],dp2[i-1,:-rev_a]+rev_b)

      
lis = [0]*N
for i in range(1,N):
  lis[i-1] = (np.max(dp1[i-1]+dp2[N-i][::-1]) + B[i-1])
 
    
print(max(lis))