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:
import sys
input = sys.stdin.readline

n,t=map(int,input().split())
time=0
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append((a,b))
l.sort()
ans=0
i=0
while 1:
    if time>=t-1 or i==n:
        break
    ans+=l[i][1]
    i+=1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
from collections import defaultdict
N, T = map(int, input().split())

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

q = []

for i, (a,b) in enumerate(AB):
    heapq.heappush(q, (-b, a, i))

used = [False] * N

curr = 0
ans = 0

if True:

    tmp = []

    while len(q) > 0 and curr < T:
        mb, a, i = heapq.heappop(q)

        if curr + a >= T:
            heapq.heappush(tmp, (mb, a, i))
        else:
            ans += -1*mb
            curr +=  a


            while len(tmp) > 0:
                mb, a, i = heapq.heappop(tmp)
                heapq.heappush(q, (mb, a, i))

    
    if len(tmp) > 0 and curr < T:
        mb, a, i = heapq.heappop(tmp)
        ans += -1*mb
        curr +=  a
        


print(ans)

Submission status: Wrong Answer

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

n,t=map(int,input().split())
time=0
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append((a,b))
l.sort()
ans=0
i=0
while 1:
    if time>=t or i==n:
        break
    ans+=l[i][1]
    i+=1
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# coding: utf-8

# 整数 > 整数型
def I(): return int(input())

# 整数列 '1 2 3 4' > 整数型リスト
def IsIl(): return list(map(lambda x: int(x), input().split()))

# 改行挟む整数列　'1/n 2/n 3/n' > 整数型りすと
def InIl(n): return [input() for i in range(n)]

# 整数列 '1 2' > 整数型タプル
def IsIt(): return map(lambda x: int(x), input().split())

# 改行挟む整数列　'1\n 2\n 3\n' > 整数型たぷる
def InIl(n): return map(input() for i in range(n))

# 文字列 'abcd' > 文字型リスト
def SsSl(): return list(map(lambda x: x, input()))

# 整数 '1234' > 整数型リスト

# 文字列 'aba' > 辞書（文字型:個数）{'a':2,'b':1}
def SSd():
    from collections import defaultdict
    A = input() # [int(i) for i in input().split()]
    dd=defaultdict(int)
    for a in A:
        dd[a]+=1
    return dd


def temp_dp(n,w,weight,value):
    dp = [[0]*(w+1)] #初期化

    for i in range(n):
        dp_ip1 = []
        for i_w in range(w+1):
            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 :
        point_max = b
        point_maxt = t
        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:
n,t=map(int,input().split())
dp=[[0 for i in range(t)] for j in range(n)]
food=[list(map(int,input().split())) for i in range(n)]
food.sort(key=lambda x:x[0])
for i in range(n-1):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j]=max(dp[i][j],dp[i][j-food[i][0]]+food[i][1])
print(max(dp[-1])+food[-1][1])

Submission status: Wrong Answer

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

xy=ab[-1]

ab=ab[:-1:]
a=[t[0] for t in ab]
b=[t[1] for t in ab]

#print(ab)

dp=[[-1 for _ in range(t)] for _ in range(n)]
for i in range(t):
	dp[0][i]=0

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

#print(dp)
print(dp[-1][-1]+xy[1])

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
N, T = map(int, input().split())
CD = [list(map(int, input().split())) for _ in range(N)]
dp = [[0] * (T+1) for _ in range(N+1)]
AB = deepcopy(CD)
AB.sort(key = lambda x:x[0], reverse=True)
AB[0][0] = 1
for i in range(1,N+1):
    for j in range(1,T+1):
        if j < AB[i-1][0]:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j-AB[i-1][0]] + AB[i-1][1], dp[i-1][j])
a = dp[N][T]
dp = [[0] * (T+1) for _ in range(N+1)]
AB = deepcopy(CD)
AB.sort(key = lambda x:x[1], reverse=True)
AB[0][0] = 1
for i in range(1,N+1):
    for j in range(1,T+1):
        if j < AB[i-1][0]:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j-AB[i-1][0]] + AB[i-1][1], dp[i-1][j])
b = dp[N][T]


print(max(a,b))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# coding: utf-8

# 整数 > 整数型
def I(): return int(input())

# 整数列 '1 2 3 4' > 整数型リスト
def IsIl(): return list(map(lambda x: int(x), input().split()))

# 改行挟む整数列　'1/n 2/n 3/n' > 整数型りすと
def InIl(n): return [input() for i in range(n)]

# 整数列 '1 2' > 整数型タプル
def IsIt(): return map(lambda x: int(x), input().split())

# 改行挟む整数列　'1\n 2\n 3\n' > 整数型たぷる
def InIl(n): return map(input() for i in range(n))

# 文字列 'abcd' > 文字型リスト
def SsSl(): return list(map(lambda x: x, input()))

# 整数 '1234' > 整数型リスト

# 文字列 'aba' > 辞書（文字型:個数）{'a':2,'b':1}
def SSd():
    from collections import defaultdict
    A = input() # [int(i) for i in input().split()]
    dd=defaultdict(int)
    for a in A:
        dd[a]+=1
    return dd


def temp_dp(n,w,weight,value):
    dp = [[0]*(w+1)] #初期化

    for i in range(n):
        dp_ip1 = []
        for i_w in range(w+1):
            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 < t :
        point_max = b
        point_maxt = t
        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:
# dp[i][t_sum]: i番目までの品物で時間がt_sumを超えないように選んだときの、満足度の総和の最大値
N, T = map(int, input().split())
items = sorted([tuple(map(int, input().split()))
                for i in range(N)], key=lambda x: x[0])

dp = [[0 for t_sum in range(T+3003)] for i in range(N+1)]

for i in range(N):
    for t_sum in range(T):

        # これは配るDP
        dp[i+1][t_sum+items[i][0]] = max(dp[i+1][t_sum+items[i][0]],
                                         dp[i][t_sum]+items[i][1])

        # i-1番目の品物を選ばない場合
        dp[i+1][t_sum] = max(dp[i+1][t_sum], dp[i][t_sum])


ans = 0
for t in range(T+3003):
    ans = max(ans, dp[N][t])
print(ans)

Submission status: Wrong Answer

Solution code:
# dp[i][t_sum]: i番目までの品物で時間がt_sumを超えないように選んだときの、満足度の総和の最大値
N, T = map(int, input().split())
items = sorted([tuple(map(int, input().split()))
                for i in range(N)], key=lambda x: x[0])

dp = [[0 for t_sum in range(T+3003)] for i in range(N+1)]

for i in range(N):
    for t_sum in range(T):

        # これは配るDP
        dp[i+1][t_sum+items[i][0]] = max(dp[i+1][t_sum+items[i][0]],
                                         dp[i][t_sum]+items[i][1])

        # i-1番目の品物を選ばない場合
        dp[i+1][t_sum] = max(dp[i+1][t_sum], dp[i][t_sum])


ans = 0
for t in range(T+3003):
    ans = max(ans, dp[N-1][t])
print(ans)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
N, T = map(int, input().split())
CD = [list(map(int, input().split())) for _ in range(N)]
dp = [[0] * (T+1) for _ in range(N+1)]
AB = deepcopy(CD)
AB.sort(key = lambda x:x[0], reverse=True)
AB[0][0] = 0
for i in range(1,N+1):
    for j in range(1,T+1):
        if j < AB[i-1][0]:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j-AB[i-1][0]] + AB[i-1][1], dp[i-1][j])
a = dp[N][T]
dp = [[0] * (T+1) for _ in range(N+1)]
AB = deepcopy(CD)
AB.sort(key = lambda x:x[1], reverse=True)
AB[0][0] = 0
for i in range(1,N+1):
    for j in range(1,T+1):
        if j < AB[i-1][0]:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j-AB[i-1][0]] + AB[i-1][1], dp[i-1][j])
b = dp[N][T]


print(max(a,b))

Submission status: Wrong Answer

Solution code:
def maximize_value(N, T, AB):
    last = max(AB, key=lambda item: item[1])
    AB.remove(last)
    value = [[0 for _ in range(T-1)] for _ in range(N)]
    for i in range(1, N):
        for j in range(T - 1):
            t, v = AB[i - 1]
            if j - t >= 0:
                if value[i - 1][j - t] + v > value[i - 1][j]:
                    value[i][j] = value[i - 1][j - t] + v
                else:
                    value[i][j] = value[i - 1][j]
            else:
                value[i][j] = value[i - 1][j]
    return value[-1][-1] + last[1]


N, T = map(int, input().split())
AB = []
for i in range(N):
    AB.append(list(map(int, input().split())))
res = maximize_value(N, T, AB)
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(key = lambda x:x[1])
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] = max(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[n]))

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(key = lambda x:x[1])
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] = max(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(dp[n][t])

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

def cul(menu):
	good = 0 # 美味しさ
	time = 0 # 時間
	for i in range(len(menu)):
		if time >= T-1:
			break
		else:
			good += menu[i][1]
			time += menu[i][0]
	return good


N,T = map(int,input().split())
menu = list()
for i in range(N):
	menu.append(list(map(int,input().split())))
# 時間が少ない順に計算
menu.sort() # 時間が少ない順にソート
good_1 = cul(menu)

# 満足度が多い順に計算
menu.sort(key=itemgetter(1),reverse=True)
good_2 = cul(menu)
print(max(good_1,good_2))

# 満足度と時間を考えてソート

# menu_1 = dict()
# for i in range(len(menu)):
# 	if abs(menu[i][1]-menu[i][0]) not in menu_1:
# 		menu_1[abs(menu[i][1]-menu[i][0])] = menu[i]
# print(menu_1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# coding: utf-8

# 整数 > 整数型
def I(): return int(input())

# 整数列 '1 2 3 4' > 整数型リスト
def IsIl(): return list(map(lambda x: int(x), input().split()))

# 改行挟む整数列　'1/n 2/n 3/n' > 整数型りすと
def InIl(n): return [input() for i in range(n)]

# 整数列 '1 2' > 整数型タプル
def IsIt(): return map(lambda x: int(x), input().split())

# 改行挟む整数列　'1\n 2\n 3\n' > 整数型たぷる
def InIl(n): return map(input() for i in range(n))

# 文字列 'abcd' > 文字型リスト
def SsSl(): return list(map(lambda x: x, input()))

# 整数 '1234' > 整数型リスト

# 文字列 'aba' > 辞書（文字型:個数）{'a':2,'b':1}
def SSd():
    from collections import defaultdict
    A = input() # [int(i) for i in input().split()]
    dd=defaultdict(int)
    for a in A:
        dd[a]+=1
    return dd


def temp_dp(n,w,weight,value):
    dp = [[0]*(w+1)] #初期化

    for i in range(n):
        dp_ip1 = []
        for i_w in range(w+1):
            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
for i in range(n):
    a, b= IsIt()
    if b > point_max:
        point_max = b
        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:
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

AB.sort()
# N, T = 3000, 14
# AB = [(i**2, i) for i in range(N)]

# dp[i+1][j] = i番目までの料理で、j分経った時の、最高満足度
# jがT以上のときはj == T とする(T以上における差はないので)

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

for i, (a, b) in enumerate(AB):
    # print(a, b)
    for j in range(T):
        dp[i+1][j] = max(
            dp[i+1][j],
            dp[i][j]
        )
        if j+a < T:
            dp[i+1][j+a] = max(
                dp[i+1][j+a],
                dp[i][j] + b
            )
        else:
            dp[i+1][T] = max(
                dp[i+1][T],
                dp[i][j] + b
            )

# for dp_i in dp:
#     print(dp_i)

print(dp[N][T])

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
A,B,C=[],[],[]#A:time,B:deliciousness,C:effectiveness
for _ in range(n):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(b/a)
C_index = sorted(range(len(C)), key=lambda k: C[k],reverse=True)
B_index = sorted(range(len(B)), key=lambda k: B[k],reverse=True)
time = 0
kutta = []
for i in C_index:
    if time + A[i]<t:
        kutta.append(i)
        time += A[i]
sum = 0
for i in range(len(kutta)):
    sum += B[kutta[i]]
for i in B_index:
    if i not in kutta:
        sum += B[i]
        break
print(sum)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

def cul(menu):
	good = 0 # 美味しさ
	time = 0 # 時間
	for i in range(len(menu)):
		if time >= T-1:
			break
		else:
			good += menu[i][1]
			time += menu[i][0]
	return good


N,T = map(int,input().split())
menu = list()
for i in range(N):
	menu.append(list(map(int,input().split())))
# 時間が少ない順に計算
menu.sort() # 時間が少ない順にソート
good_1 = cul(menu)

# 満足度が多い順に計算
menu.sort(key=itemgetter(1),reverse=True)
good_2 = cul(menu)
print(max(good_1,good_2))

# 満足度と時間を考えてソート

menu_1 = dict()
for i in range(len(menu)):
	if abs(menu[i][1]-menu[i][0]) not in menu_1:
		menu_1[abs(menu[i][1]-menu[i][0])] = menu[i]
print(menu_1)

Submission status: Wrong Answer

Solution code:
import collections

n, t = map(int, input().split())
a = [None] * n
b = [None] * n
for i in range(n):
    a[i], b[i] = map(int, input().split())

dp = [collections.defaultdict(int) for _ in range(n + 1)]
dp[0][0] = 0
for i in sorted(range(n), key=lambda x: a[i]):
    for cur in dp[i]:
        dp[i + 1][cur] = max(dp[i + 1][cur], dp[i][cur])
        if cur <= t:
            dp[i + 1][cur + a[i]] = max(dp[i + 1][cur + a[i]], dp[i][cur] + b[i])
print(max(dp[n].values()))

Submission status: Wrong Answer

Solution code:
import sys; readline = sys.stdin.readline
from collections import deque
sys.setrecursionlimit(10000)


N, T = map(int, readline().split())
A = [list(map(int, readline().split())) for i in range(N)]
A.sort(key=lambda x:-x[0])

w, v = zip(*A)
dp = [[-1 for i in range(T + 1)] for j in range(N + 1)]
def rec_dp(i, j, mini):
    if dp[i][j] != -1:
        return dp[i][j]
    
    if i == N:
        if j > 0:
            res = 0 if mini == i else v[mini]
        else:
            res = 0
    elif j < w[i]:
        res = rec_dp(i + 1, j, min(mini, i))
    else:
        res = max(rec_dp(i + 1, j, min(mini, i)), rec_dp(i + 1, j - w[i], i+1 if i==mini else mini) + v[i])
    
    dp[i][j] = res
    return res

ans = rec_dp(0, T, 0)

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,T=map(int,input().split())

l=[]
for _ in range(N):
    a,b=map(int,input().split())
    l.append([a,b])
    
dp=[0]*(3000*2+3)
temp=dp[:]

for i in l:
    for index,item in enumerate(dp):
        if index==0 or index<(T):
            if index==0 or item!=0:
                temp[index+i[0]]=max(item+i[1],temp[index+i[0]])
    dp=temp[:]
print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys


def main():
    input = sys.stdin.readline
    N, T = map(int, input().strip().split())
    foods = [list(map(int, input().strip().split())) for _ in range(N)]

    ans = 0
    dp = [[0] * (T+1) for _ in range(N+1)]
    for j, food in enumerate(foods):
        n_a, n_b = food
        for k in range(T+1):
            if k == T:
                dp[j+1][k] = max(dp[j][k], dp[j][k-1]+n_b)
            elif k < n_a:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
    ans = dp[-1][-1]
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
dishes = [tuple(map(int, input().split())) for _ in range(N)]
dishes = sorted(dishes)
dp = [[-10**8 for j in range(T+3001)] for i in range(N+1)]
cand = []
for i in range(N+1):
    dp[i][0] = 0
for i in range(N):
    a, b = dishes[i]
    for j in range(a):
        dp[i+1][j] = dp[i][j]
    for j in range(a, T):
        dp[i+1][j] = max(dp[i][j], dp[i][j - a] + b)
    for j in range(T, T+a):
        if dp[i][j-a] < 0: continue
        dp[i+1][j] = dp[i][j-a] + b
        cand += [dp[i+1][j]]
cand += dp[N][:T]
print(max(cand))

Submission status: Wrong Answer

Solution code:
I = [int(_) for _ in open(0).read().split()]
N, T = I[:2]
A, B = I[2::2], I[3::2]
dp = [-float('inf')] * 6001
dp[0] = 0
for a, b in zip(A, B):
    for k in range(T - 1, -1, -1):
        if dp[k] == -float('inf'):
            continue
        dp[k + a] = max(dp[k + a], dp[k] + b)
print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, t = map(int, input().split())
a = []
b = []
for i in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)
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] = max(dp[i+1][j], dp[i][j])
    for j in range(t):
        if j < t:
            next = min(j + a[i], t)
            dp[i+1] [next] = max(dp[i+1][next], dp[i][j] + b[i])
print(max(dp[n]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
# from bisect import *
# from collections import *
# from heapq import *
# import functools
# import itertools
# import math
INF = float('inf')
MOD = 10**9+7

class SegmentTree:

    def __init__(self, seq, _INF = -INF):
        self.N, N = 1, len(seq)
        while self.N < N:
            self.N *= 2
        self.data = [_INF for i in range(self.N*2-1)]
        self.seq = seq
        for k in range(N):
            self.update(k, seq[k])

    def update(self, k, a):
        self.seq[k] = a
        k += self.N-1
        self.data[k] = a
        while k > 0:
            k = (k-1)//2
            self.data[k] = max(self.data[k*2+1], self.data[k*2+2])

    def rmq(self, a, b, k=0, l=0, r=-1):
        if r == -1: r = self.N
        if r <= a or b <= l:
            return -INF
        if a <= l and r <= b:
            return self.data[k]
        vl = self.rmq(a, b, k*2+1, l, (l+r)//2)
        vr = self.rmq(a, b, k*2+2, (l+r)//2, r)
        if vl < vr:
            return vr
        else:
            return vl

N, T = map(int, input().split())
AB = [None] * N
for i in range(N):
    AB[i] = list(map(int, input().split()))
dp1 = [[-INF]*(T+1) for i in range(N+1)]
dp1[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j and dp1[i][j-A]+B > dp1[i][j]:
            dp1[i+1][j] = dp1[i][j-A]+B
        else:
            dp1[i+1][j] = dp1[i][j]

AB = AB[::-1]
dp2 = [[-INF]*(T+1) for i in range(N+1)]
dp2[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j and dp2[i][j-A]+B > dp2[i][j]:
            dp2[i+1][j] = dp2[i][j-A]+B
        else:
            dp2[i+1][j] = dp2[i][j]

ans = max(dp1[N])

_ans = 0
B = AB[-1][1]
_ans = max(dp2[N-1][:T])+B
if _ans > ans:
    ans = _ans

_ans = 0
B = AB[0][1]
_ans = max(dp1[N-1][:T])+B
if _ans > ans:
    ans = _ans

for i in range(1, N-1):
    A, B = AB[i]
    seg = SegmentTree(dp2[N-i-2])
    for j in range(T):
        tmp = dp1[i-1][j]+seg.rmq(0, T-j)+B
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


def main():
    input = sys.stdin.readline
    N, T = map(int, input().strip().split())
    foods = [list(map(int, input().strip().split())) for _ in range(N)]

    ans = 0
    dp = [[0] * (T+1) for _ in range(N+1)]
    for j, food in enumerate(foods):
        n_a, n_b = food
        for k in range(T+1):
            if k == T:
                dp[j+1][k] = max(dp[j][k], dp[j][k-1]+n_b)
            elif k < n_a:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
        ans = max(ans, dp[-1][-1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
DP = [[0 for _ in range(6001)] for _ in range(2)]
bit = 0
for i in range(N):
    A,B = map(int,input().split())
    for j in range(T):
        DP[bit][j+A] = max(DP[bit^1][j+A],DP[bit^1][j]+B,DP[bit][j+A-1])
    for j in range(T):
        DP[bit^1][j] = 0
    bit ^= 1
print(max(DP[bit^1]))

Submission status: Wrong Answer

Solution code:

import sys
n,t=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n)]
#ab=sorted(ab, key=lambda x:x[1])
a=[k[0] for k in ab]
b=[k[1] for k in ab]

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

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

#print(dp)

res2=0
for k in dp[-1][-1][1]:
	res2+=a[k]
#print(res2,t)
if res2==t:
	print(dp[-1][-1][0])
	sys.exit()
res=0
for i in range(n):
	if i not in dp[-1][-1][1]:
		res=max(res,b[i])
#print(res)
#print(dp[-1][-1])
print(dp[-1][-1][0]+res)
#print(max(max(dp2),dp[-1][-1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import sys

def _ia(): return [ int(x) for x in sys.stdin.readline().strip().split() ]

n,t = _ia()
ab = []
imax = -1
amax = -1
bmax = -1
for i in range(n):
    _a,_b = _ia()
    ab.append((_a,_b))
    if bmax<0 or (_b>=bmax and _a>ab[imax][0]):
        imax = i
        amax = _a
        bmax = _b
#
dp = [ 0 ] * (t+1)
for i, _ab in enumerate(ab):
    if i==imax: continue
    a,b = _ab
    prev = dp.copy()
    for atot in range(t+1):
        if atot>=a:
            dp[atot] = max(prev[atot],prev[atot-a]+b)
        else:
            dp[atot] = prev[atot]
#
print(max(dp[t-1]+bmax,dp[t]))

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n,t = m()

li = [l() for i in range(n)]

ma = []
for i in range(n):
    ma.append([li[i][1],i])

ma.sort(reverse = True)


t = t
dp = [0 for i in range(t + 3000)]

for i in range(n):
    for k in range(li[i][0]+t-1,li[i][0]-1,-1):
        if dp[k] <= dp[k-li[i][0]] + li[i][1]:
            dp[k] = dp[k-li[i][0]] + li[i][1]


dp.sort(reverse = True)
ans = dp[0]

print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
dp = [[0] * (T+4000) for _ in range(N+1)]
ret = 0
for i in range(1,N+1):
    for j in range(1,T+4000):
        if i == N:
            if j < AB[i-1][0]:
                dp[i][j] = dp[i-1][j]
            else:
                dp[i][j] = max(dp[i-1][j-AB[i-1][0]] + AB[i-1][1], dp[i-1][j])
                if j-AB[i-1][0] < N:
                    ret = max(ret, dp[i][j])
        else:    
            if j < AB[i-1][0]:
                dp[i][j] = dp[i-1][j]
            else:
                dp[i][j] = max(dp[i-1][j-AB[i-1][0]] + AB[i-1][1], dp[i-1][j])

print(max(ret,dp[N][T]))