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 = [[int(i) for i in input().split()] for j in range(n)]

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


ans = 0

for i in range(n):
    for j in range(t):
        #for k in range(2):
        dp[i+1][j] = max(dp[i][j],dp[i-1][j])
        #print(j,ab[i][0])
        if j >= ab[i][0]:
        #if dp[i][j] < dp[i][j-ab[i][0]] + ab[i][1]:
        #    dp[i+1][j] = dp[i][j-ab[i][0]] + ab[i][1]
            dp[i+1][j] = max(dp[i][j],dp[i][j-ab[i][0]] + ab[i][1])
        #print(dp)
    #print(h,dp)

#print(ab)
#print(dp)


max_num = [0]*n

for i in reversed(range(n)):
    #print(i)
    if i == 0:
        break
    if max_num[i] < ab[i][1]:
        max_num[i-1] = ab[i][1]
    else:
        max_num[i-1] = max_num[i]
#print(max_num)
#exit()

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

Submission status: Wrong Answer

Solution code:
#input
N, T = map(int, input().split())
C = []
for i in range(N):
    a, b = map(int, input().split())
    C.append([a, b])

#output
import numpy as np
C.sort(key = lambda x:x[0])

dp = np.zeros((N+1, T))
#dp[品物][時間]


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

answer = 0
for i in range(N):
    answer = max(dp[i][T-2] + C[i][1], answer)

print(int(answer))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter

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

AB = sorted([[int(i) for i in input().split()] for _ in range(N)], key=itemgetter(1,0))
# ラストオーダーは一番美味しい料理
last_order = AB.pop()
AB.sort(key=itemgetter(0,1))

# dp[i][j]: AB[0]~AB[i-1]の料理でj分以下に完食できる美味しさの合計の最大値
dp = [[0]*T for _ in range(N)]

# 配るDP
for i in range(N-1):
    for j in range(T):
        # i番目を食べる場合
        if j + AB[i][0] <= T-1:
            dp[i+1][j+AB[i][0]] = max(dp[i+1][j+AB[i][0]], dp[i][j]+AB[i][1])
        # 食べない場合
        dp[i+1][j] = max(dp[i+1][j],dp[i][j])

answer = max(dp[-1]) + last_order[1]
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter

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

AB = sorted([list(map(int,input().split())) for _ in range(N)],key=itemgetter(1))
# ラストオーダーは一番美味しい料理
last_order = AB.pop()
AB = sorted(AB)

# dp[i][j]: AB[0]~AB[i-1]の料理でj分以下に完食できる美味しさの合計の最大値
dp = [[0]*T for _ in range(N)]

# 配るDP
for i in range(N-1):
    for j in range(T):
        # i番目を食べる場合
        if j + AB[i][0] <= T-1:
            dp[i+1][j+AB[i][0]] = max(dp[i+1][j+AB[i][0]], dp[i][j]+AB[i][1])
        # 食べない場合
        dp[i+1][j] = max(dp[i+1][j],dp[i][j])

answer = dp[-1][-1] + last_order[1]
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter

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

AB = sorted([[int(i) for i in input().split()] for _ in range(N)], key=itemgetter(1,0))
# ラストオーダーは一番美味しい料理
last_order = AB.pop()
AB.sort()

# dp[i][j]: AB[0]~AB[i-1]の料理でj分以下に完食できる美味しさの合計の最大値
dp = [[0]*T for _ in range(N)]

# 配るDP
for i in range(N-1):
    for j in range(T):
        # i番目を食べる場合
        if j + AB[i][0] <= T-1:
            dp[i+1][j+AB[i][0]] = max(dp[i+1][j+AB[i][0]], dp[i][j]+AB[i][1])
        # 食べない場合
        dp[i+1][j] = max(dp[i+1][j],dp[i][j])

answer = dp[-1][-1] + last_order[1]
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter

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

AB = sorted([[int(i) for i in input().split()] for _ in range(N)], key=itemgetter(1,0))
# ラストオーダーは一番美味しい料理
last_order = AB.pop()

# dp[i][j]: AB[0]~AB[i-1]の料理でj分以下に完食できる美味しさの合計の最大値
dp = [[0]*T for _ in range(N)]

# 配るDP
for i in range(N-1):
    for j in range(T):
        # i番目を食べる場合
        if j + AB[i][0] <= T-1:
            dp[i+1][j+AB[i][0]] = max(dp[i+1][j+AB[i][0]], dp[i][j]+AB[i][1])
        # 食べない場合
        dp[i+1][j] = max(dp[i+1][j],dp[i][j])

answer = dp[-1][-1] + last_order[1]
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

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

AB = sorted([list(map(int,input().split())) for _ in range(N)])
# ラストオーダーは一番美味しい料理
last_order = AB.pop(np.argmax(AB, axis=0)[1])

# dp[i][j]: AB[0]~AB[i-1]の料理でj分以下に完食できる美味しさの合計の最大値
dp = [[0]*T for _ in range(N)]

# 配るDP
for i in range(N-1):
    for j in range(T):
        # i番目を食べる場合
        if j + AB[i][0] <= T-1:
            dp[i+1][j+AB[i][0]] = max(dp[i+1][j+AB[i][0]], dp[i][j]+AB[i][1])
        # 食べない場合
        dp[i+1][j] = max(dp[i+1][j],dp[i][j])

answer = dp[-1][-1] + last_order[1]
print(answer)

Submission status: Wrong Answer

Solution code:
import math
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N,T=LI()
lis=[tuple(LI()) for i in range(N)]
lis.sort(key=lambda x: x[1])
RES=[[0]*(T+1) for i in range(N+1)]
for i in range(1, N+1):
    A, B = lis[i-1]
    for t in range(T+1):
        if (t-A >= 0):
            RES[i][t] = max(RES[i-1][t], RES[i-1][t-A]+B)
        else:
            RES[i][t] = RES[i-1][t]

cand2 = RES[N-1][T-1] + lis[-1][1]
if RES[N][T] > cand2:
    print(RES[N][T])
else:
    print(cand2)

Submission status: Wrong Answer

Solution code:
import math
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N,T=LI()
lis=[tuple(LI()) for i in range(N)]
RES=[[0]*(T+1) for i in range(N+1)]
for i in range(1, N+1):
    A, B = lis[i-1]
    for t in range(T):
        if (t-A >= 0):
            RES[i][t] = max(RES[i-1][t], RES[i-1][t-A]+B)
        else:
            RES[i][t] = RES[i-1][t]
    if (T-A >= 0):
        RES[i][T] = max(RES[i-1][T], RES[i-1][T-1]+B, RES[i-1][T-A]+B)
    else:
        RES[i][T] = max(RES[i-1][T], RES[i-1][T-1]+B)

print(RES[N][T])

Submission status: Wrong Answer

Solution code:
import math
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N,T=LI()
lis=[tuple(LI()) for i in range(N)]
lis.sort(key=lambda x: x[1])
RES=[[0]*(T+1) for i in range(N+1)]
for i in range(1, N+1):
    for t in range(T+1):
        A, B = lis[i-1]
        if (t-A >= 0):
            RES[i][t] = max(RES[i-1][t], RES[i-1][t-A]+B)
        else:
            RES[i][t] = RES[i-1][t]

cand2 = RES[N-1][T-1] + lis[-1][1]
if RES[N][T] > cand2:
    print(RES[N][T])
else:
    print(cand2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline().rstrip
show_flg=False
#show_flg=True

ans=0

n,T=LI()
c=[]
maxA=0
for i in range(n):
    a,b=LI()
    c.append((a,b))
c=sorted(c,key=lambda x:x[0])
maxA=max([a for a,b in c])

dp=[[0 for i in range(T+maxA+2)] for i in range(n+1)]

for i in range(n):
    a,b=c[i]
    show(dp)
    for t in range(a,T+maxA+1):
        dp[i+1][t]=dp[i][t]
        if t-a<T-0.5:
            dp[i+1][t]=max(dp[i][t-a]+b,dp[i+1][t])
        if t-a<=T-1 and T<=t+a:
            dp[i+1][t]=max(dp[i][t-a]+b,dp[i+1][t])
show(dp)
ans=max(dp[n])
        
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline().rstrip
show_flg=False
#show_flg=True

ans=0

n,T=LI()
c=[]
maxA=0
for i in range(n):
    a,b=LI()
    c.append((a,b))
c=sorted(c,key=lambda x:x[0])
maxA=max([a for a,b in c])

dp=[[0 for i in range(T+maxA+2)] for i in range(n+1)]

for i in range(n):
    a,b=c[i]
    show(dp)
    for t in range(a,T+maxA+1):
        dp[i+1][t]=dp[i][t]
        if t-a<T-0.5:
            dp[i+1][t]=max(dp[i][t-a]+b,dp[i+1][t])
show(dp)
ans=max(dp[n])
        
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline().rstrip
show_flg=False
#show_flg=True

ans=0

n,T=LI()
c=[]
maxA=0
for i in range(n):
    a,b=LI()
    c.append((a,b))
c=sorted(c)#,key=lambda x:x[1]/x[0])
l_a,l_b=c.pop()
n-=1
maxA=max([a for a,b in c])

dp=[[0 for i in range(T+maxA+2)] for i in range(n+1)]

for i in range(n):
    a,b=c[i]
    show(dp)
    for t in range(a,T+maxA+1):
        dp[i+1][t]=dp[i][t]
        if t-a<T-0.5:
            dp[i+1][t]=max(dp[i][t-a]+b,dp[i+1][t])
show(dp)
ans=0
ans=max(dp[n][:T])+l_b
#for t in range(T+maxA+2):
#    ans=max(ans,dp[n][t])
        
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=[6007*[0]for _ in range(n)]
ans=0
for i in range(n):
  a,b=ab[i]
  for j in range(6007):
    if i==0:
      if j>=a and j<t:dp[i][j]=b
    elif j>=a and j-a<t:dp[i][j]=max(dp[i-1][j-a]+b,dp[i-1][j])
    else:dp[i][j]=dp[i-1][j]
    ans=max(ans,dp[i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split(' '))
tmp_food_table = []
for _ in range(n):
    tmp_food_table.append(tuple(map(int, input().split(' '))))
food_table = []
for i in tmp_food_table:
    food_table.append((i[0],i[1],i[1] / i[0]))
max_food = max(food_table,key=lambda x:x[1])
food_table.remove(max_food)
food_table = sorted(food_table,key=lambda x:x[1],reverse=True)
t = t - 1
current_t = 0
result = 0
for i in food_table:
    current_t = current_t + i[0]
    if current_t > t:
        break
    result = result + i[1]
result = result + max_food[1]
print(result)

Submission status: Wrong Answer

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

N,T = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*N]

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

for i in range(N):
    a,b = AB[i]
    for t in range(T):
        dp[i+1][t] = dp[i][t]
        if t-a>=0:
            dp[i+1][t] = max(dp[i+1][t],dp[i][t-a]+b)

s = []
t = T-1
for j in range(N-1,-1,-1):
    a,b = AB[j]
    while (dp[j+1][t] == dp[j+1][t-1]) and t>0:
        t -= 1
    if dp[j+1][t] == dp[j][t]:
        pass
    else:
        t -= a
        s.append(j)

ans0 = dp[N][-1]
ans = ans0
for i in range(N):
    if s and i==s[-1] :
        s.pop()
        continue
    ans = max(ans,ans0+AB[i][1])
print(ans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
f = [tuple(map(int, input().split())) for i in range(n)]
dp = [[[0, 0] for j in range(t + 3101)] for i in range(n + 1)]  # i個目の料理まででj分以内に得られる最大値（kは短縮フラグ）
for i in range(n):
    ft, fv = f[i]
    for j in range(t):
        for k in range(2):
            c = dp[i][j][k]
            dp[i + 1][j][k] = max(dp[i + 1][j][k], c)
            dp[i + 1][j + ft][k] = max(dp[i + 1][j + ft][k], c + fv)
            if k == 0:
                dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], c + fv)
print(max(dp[n][t]))

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, T = map(int, readline().split())

AB= list(map(int, read().split()))
A = AB[0::2]
B = AB[1::2]
ls = sorted(zip(A, B))

deli = [0]*N
deli[- 1] = ls[-1][- 1]
for i in range(N - 2, 0, - 1):
    deli[i] = max(deli[i + 1], ls[i][- 1])

res = 0
dp = [0]*T
for i in range(N - 1):
    a, b = ls[i]
    for j in range(T - 1, a - 1, - 1):
        dp[j] = max(dp[j], dp[j - a] + b)
    if res > dp[- 1] + deli[i + 1]:
        res = max(res, dp[- 1] + deli[i + 1])
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, t = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
dp = [[0 for j in range(t+1)] for i in range(n+1)]

for i in range(n):
    for j in range(t):
        dp[i+1][j] = dp[i][j]
    for j in range(t):
        if j < t:
            next = min(j + ab[i][0], t)
            dp[i+1] [next] = max(dp[i+1][next], dp[i][j] + ab[i][1])
print(max(dp))

Submission status: Wrong Answer

Solution code:
# 基本的なナップサック問題
N, T = map(int,input().split())
ab_list = []
for _ in range(N):
    a, b = map(int,input().split())
    ab_list.append((a,b))

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

for i in range(N):
    a, b = ab_list[i]
    for j in range(T+1):
        if j-a >= 0:
            dp[i+1][j] = max(dp[i][j-a] + b, dp[i][j])
        else:
            dp[i+1][j] = dp[i][j]
    for k in range(T+1,T+1+3000):
        # T-1分までなら最後の一品注文可能
        if k-a >= 0 and k-a <= T-1:
            dp[i+1][k] = max(dp[i][k-a] + b, dp[i][k])
        else:
            dp[i+1][k] = dp[i][k]
            
print(max(dp[-1]))

Submission status: Wrong Answer

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

A = []
B = []
for _ in range(N):
    aa, bb = map(int, input().split())

    A.append(aa)
    B.append(bb)

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

for x in range(1, N + 1):
    for y in range(1, T):
        if y >= A[x - 1]:
            dp1[x][y] = max(dp1[x - 1][y], dp1[x - 1][y - A[x - 1]] + B[x - 1])
        
        else:
            dp1[x][y] = dp1[x - 1][y]

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

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

        else:
            dp2[x][y] = dp2[x + 1][y]

MAX = -1
for x in range(1, N + 1):
    for y in range(T):
        MAX = max(MAX, dp1[x - 1][y] + dp2[x + 1][T - 1 - y] + B[x - 1])

print(MAX)

Submission status: Wrong Answer

Solution code:
def test():
    N,W = [int(i) for i in input().split()]
    data = [[int(i) for i in input().split()] for j in range(N)]

    dp = [[0 for j in range(W)] for i in range(N)]
    dp[0][data[0][0]-1:] = [data[0][1]]*len(dp[0][data[0][0]-1:])
    #print(dp)
    for i in range(1,N):
        for sum_w in range(W):
            w = sum_w - data[i][0]
            if w<0:
                ans = dp[i-1][sum_w]
            else:
                ans = max(dp[i-1][sum_w],dp[i-1][w]+data[i][1])
            dp[i][sum_w] = ans    
    print(dp[-1][-1])


if __name__ == '__main__':
    test()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


N,T=map(int, input().split())
val=[list(map(int, input().split())) for i in range(N)]

#import sys
#input = sys.stdin.readline
#val = list(input()) #abc

val.sort()

A=[v[0] for v in val]
B=[v[1] for v in val]


#temp=np.zeros((T+1,N))
#Ts=T*np.ones(N-1)
#jun=np.arange(1,N)
#for itemp in range(T):
#    i=itemp
#    if i>=A[0]:
#        temp[i,0]=B[0]
#    if i>0:
#        temp[i,:]=np.amax([temp[i,:],temp[i-1,:]],0)
#    for jtemp in range(N-1):
#        j=jtemp+1
#        temp[i,j]=max(temp[i,j],temp[i,j-1])
#    jnext=np.amin([Ts,i+A[1:]],0)
#    jnext=[int(s) for s in jnext]
#    temp[jnext,jun]=np.amax([temp[i,:-1]+B[1:],temp[jnext,jun]],0)

temp=[[0]*N for i in range(T+1)]
for jtemp in range(N-1):
    j=jtemp+1

    for itemp in range(T):
        i=itemp
        jbfo=temp[i][j-1]
        ijnow=temp[i][j]
        if i>=A[0]:
            temp[i][0]=B[0]
        if i>0:
            temp[i][j]=max(ijnow,temp[i-1][j])
            
        temp[i][j]=max(ijnow,jbfo)
        if A[j]<T-i:
            temp[i+A[j]][j]=max(jbfo+B[j],temp[i+A[j]][j])
        else:
            temp[T][j]=max(jbfo+B[j],temp[T][j])
print(int(max(temp[-1][:])))

Submission status: Wrong Answer

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

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

dish = []
for i in range(N):
    A, B = map(int, input().split())
    dish.append([A,B])

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

        n = (N-1) - i
        if n == 0:
            dp2[n][j] == dp2[n+1][j]
        elif j >= dish[n-1][0]:
            dp2[n][j] = max(dp2[n+1][j-dish[n-1][0]]+dish[n-1][1], dp2[n+1][j])
        else:
            dp2[n][j] = dp2[n+1][j]

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

print(ans)

Submission status: Wrong Answer

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

dish = []
for i in range(N):
    A, B = map(int, input().split())
    dish.append([A,B])

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

        n = (N-1) - i
        if n == 0:
            dp2[n][j] == dp2[n+1][j]
        elif j >= dish[n-1][0]:
            dp2[n][j] = max(dp2[n+1][j-dish[n-1][0]]+dish[n-1][1], dp2[n+1][j])
        else:
            dp2[n][j] = dp2[n+1][j]

        num = i + 1
        ans = max(ans, dp1[num-1][j]+dp2[num+1][T-1-j]+dish[num-1][1])

print(ans)

Submission status: Wrong Answer

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

dish = []
for i in range(N):
    A, B = map(int, input().split())
    dish.append([A,B])

time = 0
value = 0
dp1 = [[0]*(T) for i in range(N+1)]
for i in range(N):
    for j in range(T):
        if j >= dish[i][0]:
            dp1[i+1][j] = max(dp1[i][j-dish[i][0]]+dish[i][1], dp1[i][j])
        else:
            dp1[i+1][j] = dp1[i][j]

dp2 = [[0]*(T) for i in range(N+2)]
for i in reversed(range(N)):
    for j in range(T):
        if i == 0:
            dp2[i][j] == dp2[i+1][j]
        elif j >= dish[i-1][0]:
            dp2[i][j] = max(dp2[i+1][j-dish[i-1][0]]+dish[i-1][1], dp2[i+1][j])
        else:
            dp2[i][j] = dp2[i+1][j]

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

print(ans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
w = []
v = []
for i in range(n):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
dp = [[0] * (t + 1) for i in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
    dp[i + 1][t] = dp[i + 1][t] = max(dp[i][t - 1] + v[i], dp[i][t])
print(dp[n][t])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
w = []
v = []
for i in range(n):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
dp = [[0] * (t + 1) for i in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
    if t >= w[i]:
        dp[i + 1][t] = dp[i + 1][t] = max(dp[i][t - 1] + v[i], dp[i][t])
    else:
        dp[i + 1][t] = dp[i][t]
print(dp[n][t])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
w = []
v = []
for i in range(n):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
dp = [[0] * (t + 1) for i in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
    if j >= w[i]:
        dp[i + 1][t] = dp[i + 1][t] = max(dp[i][t - 1] + v[i], dp[i][t])
    else:
        dp[i + 1][t] = dp[i][t]
print(dp[n][t])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
w = []
v = []
for i in range(n):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
dp = [[0] * (t + 1) for i in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
    dp[i + 1][t] = max(dp[i][t - 1] + v[i], dp[i][t])
print(dp[n][t])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
w = []
v = []
for i in range(n):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
dp = [[0] * (t + 1) for i in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
    dp[i + 1][t] = max(dp[i][j - 1] + v[i], dp[i][j])
print(dp[n][t])

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter as ITEM
input = sys.stdin.readline

N, T = tuple(map(int, input().split()))
vA = [tuple(map(int, input().split())) for _ in range(N)]

vA.sort(key=ITEM(0))
#print(vA)

# dp[i..N][t..T+1] := max(sum(B))
# dp[i][min(t+A,T+1)] = max(dp[i-1][min(t+A,T+1)], dp[i-1][t]+B)
# if t<=T-1: you can eat
# dp[0][0] = 0

dp = [[0,]*(T+2)]
dp_apnd = dp.append

for A,B in vA:
  pDP = dp[-1]
  nDP = [0,]*(T+2)
  #print(id(pDP),id(nDP))###
  for t in range(T):
    nDP[min(t+A,T+1)] = max(pDP[min(t+A,T+1)], pDP[t]+B)
  dp_apnd(nDP)
  #print(dp[-1][-4:])###

print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
n, T = map(int,input().split())
As, Bs = [], []
for _ in range(n):
  a, b = map(int,input().split())
  As.append(a)
  Bs.append(b)
W = T - 1

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

for i in range(n-1,-1,-1):
    for j in range(W+1):
        if j < As[i]:
            dp[i][j] = dp[i+1][j]
        else:
          if dp[i+1][j][0]>dp[i+1][j-As[i]][0]+Bs[i]:
            dp[i][j] = [dp[i+1][j][0],dp[i+1][j][1]]
          else:
            dp[i][j] = [dp[i+1][j-As[i]][0]+Bs[i],dp[i+1][j-As[i]][1]+[i]]
for i in dp[0][W][1]:
  del(Bs[i])
if len(Bs) > 0:
  print(dp[0][W][0] + max(Bs))
else:
  print(dp[0][W][0])

Submission status: Wrong Answer

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

def main(N,T,menu):
    #時間tを初期化
    t = 0
    #注文数の初期化
    i = 0
    sum = 0
    while t <= T-0.5:
        if i > N-1:
            break

        #時間の経過
        t += menu[i][0]
        #注文数のカウント増加
        sum += menu[i][1]
        i += 1

    return sum

print(main(N,T,menu))

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, t = list(map(int, input().rstrip('\n').split()))
    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    dp = [0] * (t - 1)
    mt = 0
    for a, b in ab:
        mt = max(mt, dp[-1] + b)
        for i in range(t - a - 2, -1, -1):
            dp[i+a] = max(dp[i+a], dp[i] + b)
    print(mt)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np


N, T = map(int, input().split())
array = []
for _ in range(N):
    array.append([int(i) for i in input().split()])

    
# 制限時間内になるような全ての組を列挙する

# bの昇順でソート 
a = np.array(array)
a = a[np.argsort(a[:, 1])]

candidate = []
_range = [i for i in range(N)]
for i in range(1, N+1):
    for j in itertools.combinations(_range, i):
        j = np.array(j)
        if a[j].sum(0)[0] < T:
            sum_A = a[j].sum(0)[1]
            for k in reversed(range(N)):
                if k not in j:
                    break
                else:
                    continue
            ret = a[j].sum(0)[1] + a[k, 1]
            candidate.append(ret)
print(max(candidate))

Submission status: Wrong Answer

Solution code:
def joint_sort(a,b):
  # a should be cost, b should be delicious
  a, b = zip(*sorted(zip(a,b),key = lambda x:x[0]))
  return list(a), list(b)

def dp(cost, taste, n, t):
  # table[i][t]
  if n < 0:
    return 0
  else:
    table = [[0 for j in range(t+1)] for i in range(n+1)]
    for i in range(cost[0], t+1):
      table[0][i] = taste[0]
    for i in range(1, n+1):
      for j in range(t+1):
          tmp1 = table[i-1][j]
          tmp2 = 0
          if cost[i] <= j:
              tmp2 = table[i-1][j-cost[i]] + taste[i]
          table[i][j] = max(tmp1, tmp2)
    return table
n, t = map(int, input().split())
a = []
b = []
for i in range(n):
  x, y = map(int, input().split())
  a.append(x)
  b.append(y)
a, b = joint_sort(a, b)
s = -1
table = dp(a, b, n-1, t-1)
for i in range(n):
  ns = b[i] + table[i-1][t-1]
  if ns > s:
    s = ns
print(s)

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

#一番遅いものをラスト(T-1分)に注文
#i番目をラストに注文 = dp[i][t-1] + b[i]
ans = 0
for i in range(N):
    ans = max(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()

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

#一番遅いものをラスト(T-1分)に注文
#i番目をラストに注文 = dp[i][t-1] + b[i]
ans = 0
for i in range(N):
    ans = max(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()

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)で完食する
    
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]] =max(dp[i+1][t+a[i]],dp[i][t]+b[i])
        
        dp[i+1][t] = max(dp[i+1][t],dp[i][t])

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