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:
def dp_bu(i, max_w):

    def dp(i, max_w):

        if i == 0:
            return 0

        elif max_w >= weight[i-1]:

            if memo[max_w - weight[i-1]] + value[i-1] > memo[max_w]:
                return memo[max_w - weight[i-1]] + value[i-1]
            else:
                return memo[max_w]

        else:
            return memo[max_w]
        

    memo = [0,] * (max_w + 1)

    for j in range(i + 1):
        new_memo = [dp(j, w) for w in range(max_w + 1)]
        memo = new_memo
        # print(memo)
    
    return memo[-1]

value = []
weight = []

n, t = map(int, input().split())
for _ in range(n):
    a, b = map(int, input().split())
    value.append(b)
    weight.append(a)


print(dp_bu(n, t))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, T = map(int, input().split())
D = []
for _ in range(N):
    a, b = map(int, input().split())
    D.append((a,-b))

D.sort()

dp = [[0] * (T+1) for _ in range(N+1)]
pa = [0] * (T+1)  # prev a
for i in range(1, N+1):
    a, b = D[i-1]
    b = -b
    for t in range(1,(T+1)):
        if pa[t] <= t:
            upd = dp[i-1][t-pa[t]]+b
            if upd > dp[i-1][t]:
                dp[i][t] = upd
                pa[t] = a
            else:
                dp[i][t] = dp[i-1][t]
        else:
            if b > dp[i-1][t]:
                dp[i][t] = b
                pa[t] = a
            else:
                dp[i][t] = dp[i-1][t]

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

Submission status: Wrong Answer

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

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

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

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

ans = dp[N][T-1]
print(ans)

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 = [-1] * 6001
dp[0] = 0
for a, b in zip(A, B):
    dp_new = dp[:]
    for k in range(1 + T):
        if dp[k] == -1:
            continue
        dp_new[k + a] = max(dp[k + a], dp[k] + b)
    dp = dp_new
print(max(dp))

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[1])
a=[t[0] for t in ab]
b=[t[1] for t in ab]

dp2=[0]

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]=max(dp[i][w-a[i]]+b[i], dp[i][w])
		else:
			dp[i+1][w]=dp[i][w]
		if w<t+1 and dp[i][w]+a[i]>t:
			dp2.append(dp[i][w]+b[i])

#print(dp)
#print(dp[-1][-1])
#print(dp2)
print(max(max(dp2),dp[-1][-1]))

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,t = LI()
    g = LIR(n)
    dp = [0]*(t+3001)
    ans = 0
    for i in range(n):
        w,v = g[i]
        for j in range(w,t+3001)[::-1]:
            nd = dp[j-w]+v
            if j-w < t:
                if ans < nd:
                    ans = nd
            if dp[j] < nd:
                dp[j] = nd
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

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:
# from collections import Counter,defaultdict,deque
import sys
# import bisect
# import math
# import itertools
# import string
# import queue
# import copy
# from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])

def Knapsack2(W,w,v): # n個の品物(重さw[i],価値v[i]) 価値iを達成する重量の最小化 計算量O(V*N)
    n=len(w)
    v_max=max(sum(w),W)+10
    # dp[i] : 価値iの組み合わせを達成する重量の最小値
    dp=[[10**8,10**8] for _ in range(v_max+1)]
    dp[0]=[0,0]
    for i in range(n):
        for j in range(v_max,v[i]-1,-1):
            if j >= v[i]:
                dp[j]=[ min(dp[j][0],dp[j-v[i]][0]+w[i]) , min(dp[j][0],dp[j-v[i]][0]) ] 
    ans=0
    for i in range(v_max+1):
        if dp[i][1]<=W-0.5:
            ans=i
    print(ans)

def main():
    n,t=inpm()
    a=[]
    b=[]
    for _ in range(n):
        x,y=inpm()
        a.append(x)
        b.append(y)
    Knapsack2(t,a,b)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
n, t = map(int, input().split())

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

dp = [[None] * (3000 + 1) for _ in range(3000 + 1)]
def f(i=0, time=0):
    if time >= t:
        return 0
    if i >= n:
        return 0
    if dp[i][time] is not None:
        return dp[i][time]
    r = max(f(i + 1, time + ab[i][0]) + ab[i][1], f(i + 1, time))
    dp[i][time] = r
    return r

print(f())

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
time = [0 for i in range(3000)]
now = 0
ans = 0
lis = [list(map(int,input().split())) for i in range(n)]
lis.sort(key=lambda x:x[1])
for i in range(n):
    for j in range(t-1,-1,-1):
        if lis[i][0] + j >= t:
            ans = max(ans,time[j]+lis[i][1])
        else:
            time[j+lis[i][0]] = max(time[j+lis[i][0]],time[j]+lis[i][1])
print(max(ans,max(time)))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
T -= 1
Meals = sorted([list(map(int, input().split())) for i in range(N)], key=lambda x:-x[1])
dp = [[0] * (T + 1) for i in range(N + 1)]
prev = [[-1] * (T + 1) for i in range(N + 1)]


for i, (cost, value) in enumerate(Meals):
    for j in range(T + 1):
        if j >= cost:
            if dp[i + 1][j] < dp[i][j - cost] + value:
                dp[i + 1][j] = dp[i][j - cost] + value
                prev[i + 1][j] = j - cost

        if dp[i + 1][j] < dp[i][j]:
            dp[i + 1][j] = dp[i][j]
            prev[i + 1][j] = j

# 復元
selected = set()
cur_w = T
for i in range(N - 1, -1, -1):
    if prev[i + 1][cur_w] == cur_w - Meals[i][0]:
        selected.add(i)
    cur_w = prev[i + 1][cur_w]

non_selected = set(list(range(N))) - selected
ans = dp[N][T]
add = 0
for ni in non_selected:
    add = max(add, Meals[ni][1])

print(dp[N][T] + add)

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)

li.sort(key = lambda c:c[0]/c[1],reverse = True)
t = t-1
dp = [[0,[0 for i in range(n)]] for i in range(t+1)]

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

dp.sort(reverse = True)
ans = dp[0][0]
for i in range(n):
    if dp[0][1][ma[i][1]] == 0:
        ans += ma[i][0]
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
def main():  
    n,t = map(int,input().split())
    ab = [list(map(int,input().split())) for _ in range(n)]
    ans = []
    for x in range(n):
        ab_ = copy.deepcopy(ab)
        ab_[x][0] = 0
        c = [[0]*(t) for _ in range(n+1)]
        m = 0
        for i in range(1,n+1):
            for j in range(1,t):
                if ab_[i-1][0] <= j:
                    if ab_[i-1][1] + c[i-1][j-ab_[i-1][0]] > c[i-1][j]:
                        c[i][j] = ab_[i-1][1]+c[i-1][j-ab_[i-1][0]]
                        m = max([c[i][j],m])
                    else:
                        c[i][j] = c[i-1][j]
        ans.append(m)
    print(max(ans))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

tlis = [[0] * (T+1) for i in range(N+1)]


for i in range(N):

    A,B = map(int,input().split())

    for j in range(T+1):

        tlis[i+1][j] = tlis[i][j]

    tlis[i+1][min(T,A)] = max(tlis[i+1][min(T,A)],B)
    
    for j in range(T):

        if tlis[i][j] != 0:
            tlis[i+1][ min(T , j+A) ] = max(tlis[i+1][ min(T , j+A) ] , tlis[i][j] + B)
    

    
ans = 0
for i in range(T+1):

    ans = max(ans , tlis[-1][i])
    
print (ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, T = map(int, input().split())
D = []
for _ in range(N):
    a, b = map(int, input().split())
    D.append((a,b))

D.sort()

dp = [[0] * (T+1) for _ in range(N+1)]
pa = [0] * (T+1)  # prev a
for i in range(1, N+1):
    a, b = D[i-1]
    for t in range(1,(T+1)):
        if pa[t] <= t:
            upd = dp[i-1][t-pa[t]]+b
            if upd > dp[i-1][t]:
                dp[i][t] = upd
                pa[t] = a
            else:
                dp[i][t] = dp[i-1][t]
        else:
            if b > dp[i-1][t]:
                dp[i][t] = b
                pa[t] = a
            else:
                dp[i][t] = dp[i-1][t]

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

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,t = LI()
    g = LIR(n)
    dp = [0]*(t+3001)
    ans = 0
    for i in range(n):
        w,v = g[i]
        for j in range(w,t+3001)[::-1]:
            nd = dp[j-w]+v
            if dp[j] < nd:
                if j-w < t:
                    if ans < nd:
                        ans = nd
                dp[j] = nd
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

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[1], x[0]))

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

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

        # これは配るDP
        if t_sum+items[i][0] < T:
            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])

# print(dp)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[[0 for i in range(t)] for j in range(n)]
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:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)

N, T = map(int, input().split())
vw = []
ma, mb = 0, 0
for _ in range(N) :
    A, B = map(int, input().split())
    if B > mb or (B == mb and A > ma) :
        if not ma == mb == 0 :
            vw.append((mb, ma))
        ma, mb = A, B
    else :
        vw.append((B, A))
        
Vmax = max([v for v, _ in vw])
Wmax = max([w for _, w in vw])
vw.sort(key=lambda x: x[1], reverse=True)
dp = [-float('inf')] * (N*Wmax+1)
dp[0] = 0
for i in range(N - 1) :
    for j in range(N*Wmax, vw[i][1]-1, -1) :
        dp[j] = max(dp[j], dp[j-vw[i][1]]+vw[i][0])

print(max(dp[:T]) + mb)

Submission status: Wrong Answer

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

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

    lastDish = AB[0]
    indexOfLastDish = 0

    for i, ab in enumerate(AB):
        if  ab[1] > lastDish[1] or \
            ab[1] == lastDish[1] and ab[0] > lastDish[0]:

            lastDish = ab
            indexOfLastDish = i

    AB[indexOfLastDish] = (10000, 0)

    X = [0]*T
    
    for a, b in AB:
        for i in range(T-1, a-1, -1):
            X[i] = max(X[i], X[i-a]+b)
            
    print(max(X) + lastDish[1])



if __name__ == '__main__':
    main()

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[1])
a=[t[0] for t in ab]
b=[t[1] for t in ab]

dp2=[0]

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]=max(dp[i][w-a[i]]+b[i], dp[i][w])
		else:
			dp[i+1][w]=dp[i][w]
		if w!=0:
			dp2.append(dp[i][w]+b[i])

#print(dp)
print(max(max(dp2),dp[-1][-1]))

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
ab = []
for i in range(n):
    ab.append(list(map(int, input().split())))
max_t = max([ab[i][0] for i in range(n)])
score = [-float("inf")] * (t + max_t)
score[0] = 0
for i in range(n):
    for j in range(t - 1, -1, -1):
        if score[j] != -float("inf"):
            score[j + ab[i][0]] = max(score[j + ab[i][0]], score[j] + ab[i][1])
print(max(score))

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[1])

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

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

        # これは配るDP

        if t_sum+items[i][0] < T:
            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])

# print(dp)

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

Submission status: Wrong Answer

Solution code:
#import numpy as np

def knapsack2(N,W,weight,value,WW):
  #初期化
  inf=float("inf")
  dp=[[-inf for i in range(WW)] for j in range(N+1)]
  for i in range(WW):
      dp[0][i]=0
  maxval = 0
  #DP
  for i in range(N):
    for w in range(WW):
      if weight[i]<=w:
        if w-weight[i]>=W: #時間切れ
          dp[i+1][w]=dp[i][w]
        else: #注文可能
          dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
      else: #NG
        dp[i+1][w]=dp[i][w]
    maxval = max([maxval, max(dp[i+1])])
  return maxval
  #return np.max(np.array(dp))
  #return dp[N][2*W]

N, T = map(int,input().split())
WW = T+3000
ab = []
for i in range(N):
    ab.append(tuple(map(int,input().split())))

weight = [elem[0] for elem in ab]
value = [elem[1] for elem in ab]
res = knapsack2(N,T,weight,value,WW)
print(res)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

ans=0
AB.sort(key=lambda x:(x[1],x[0]),reverse=True)
ans1,time=0,0
for a,b in AB:
  ans1+=b
  time+=a
  if time>t-0.5:
    break
ans=max(ans,ans1)
    
AB.sort(key=lambda x:(x[1]))
ans1,time=0,0
for a,b in AB:
  ans1+=b
  time+=a
  if time>t-0.5:
    break
ans=max(ans,ans1)
    
AB.sort(key=lambda x:(x[0],x[1]),reverse=True)
ans1,time=0,0
for a,b in AB:
  ans1+=b
  time+=a
  if time>t-0.5:
    break
ans=max(ans,ans1)

AB.sort(key=lambda x:(x[0]))
ans1,time=0,0
for a,b in AB:
  ans1+=b
  time+=a
  if time>t-0.5:
    break
ans=max(ans,ans1)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, T = map(int, input().split())
    A = [0] * N
    B = [0] * N
    X = [list(map(int, input().split())) for _ in range(N)]
    X.sort(lambda x: x[0], reverse=True)
    for i in range(N):
        A[i], B[i] = X[i]
    M = T+max(A)
    dp = [[(0, 0)] * M for _ in range(2)]
    for i in range(N):
        a, b = A[i], B[i]
        for j in range(M):
            dp[i & 1][j] = dp[(i-1) & 1][j]
        for j in range(a, M):
            t, m = dp[(i-1) & 1][j-a]
            t += b
            m = max(m, a)
            if j - m < T and dp[i & 1][j][0] < t:
                dp[i & 1][j] = (t, m)
    ans = 0
    for i in range(M):
        ans = max(ans, dp[(N-1) & 1][i][0])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def examE():
    N, T = LI()
    A, B = [0] * N, [0] * N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    maxT = max(A)
    dp = [[[0 for _ in range(2)] for _ in range(T+ 1)] for _ in range(N + 1)]
    for i in range(N):
        for j in range(T+ 1):
            if j>=1:
                dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j-1][0] + B[i])
            if A[i] <= j:
                dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - A[i]][0] + B[i])
                dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j - A[i]][1] + B[i])
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0])
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1])

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

import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examE()

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
INF = float('inf')

dp = [-INF] * (T + 1)
dp[0] = 0

for a, b in AB:
    newDp = dp[::]
    for t, d in enumerate(dp[:T]):
        newDp[min(T, t + a)] = max(newDp[min(T, t + a)], d + b)
    dp = newDp

print(max(dp))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, t = map(int, input().split())
items = sorted(tuple(map(int, input().split())) for _ in range(n))
dp = np.zeros(t+1, dtype=int)
for i, (a, b) in zip(range(n), items):
    for ti in range(t-1, 0, -1):
        ati = min(a+ti, t)
        dp[ati] = max(dp[ti]+b, dp[ati])
print(dp[t])

Submission status: Wrong Answer

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

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

D.sort(key=lambda x:x[1],reverse=True)

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

for ind,(x,y) in enumerate(D):
    for time in range(T,-1,-1):
        if time-x>=0 and DP[time][0]<=DP[time-x][0]+y:

            if DP[time-x][1]==ind:
                DP[time]=[DP[time-x][0]+y,ind+1]
            else:
                DP[time]=[DP[time-x][0]+y,DP[time-x][1]]

D.append((0,0))
                
ANS=DP[-1][0]

for x,y in DP[:-1]:
    if x==0:
        continue
    ANS=max(ANS,x+D[y][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 for _ in range(t)] for _ in range(n+1)]

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

#for x in dp: print(x)
ans = max(max(dp[n]), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])
def sortx(x,n,k):
    if k == 0:x.sort(key=lambda y:y[1,n])
    else:x.sort(reversed=True, key=lambda y:y[1,n])
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a=inp()
        a-=1
        g[i].append(a)
        g[a].append(i)
    return n,g
def graphm():
    n,m=inpm()
    g=[[] for _ in range(n)]
    for _ in range(m):
        a,b,w=inpm()
        a-=1
        b-=1
        g[a].append((b,w))
        g[b].append((a,w))
    return n,m,g

def dijkstra(s,n,g): # sからの最短距離 頂点数n
    s -= 1
    que = []
    d = [10**15 for _ in range(n)]
    d[s] = 0
    heappush( que,(0,s) )
    while len(que)>0:
        p =  heappop(que)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(g[v])):
            edge = g[v][i]
            if d[edge[0]] > d[v] + edge[1]:
                d[edge[0]] = d[v] + edge[1]
                heappush( que,(d[edge[0]] , edge[0]) )
    return d

# def Knapsack1(W,w,v): # n個の品物(重さw[i],価値v[i]) 総重量W以内での価値の最大化 計算量O(W*N)
#     n=len(w)
#     # dp[i][j] : i番目の品物で重さj以下の価値の最大値
#     dp=[[0 for _ in range(W+1)] for _ in range(n+1)]
#     for i in range(1,n+1):
#         for j in range(W+1):
#             dp[i][j]=max( dp[i][j],dp[i-1][j] )
#             if j >= w[i-1]:
#                 dp[i][j]=max(dp[i][j],dp[i-1][j-w[i-1]]+v[i-1])
#     print(dp)
#     print(max(dp[n]))
def Knapsack2(W,w,v): # n個の品物(重さw[i],価値v[i]) 価値iを達成する重量の最小化 計算量O(V*N)
    n=len(w)
    v_max=sum(w)+W+10
    # dp[i] : 価値iの組み合わせを達成する重量の最小値
    dp=[[10**10,10**10] for _ in range(v_max+1)]
    dp[0]=[0,0]
    for i in range(n):
        for j in range(v_max,v[i]-1,-1):
            if j >= v[i]:
                dp[j]=[ min(dp[j][0],dp[j-v[i]][0]+w[i]) , min(dp[j][0],dp[j-v[i]][0]+w[i])-w[i] ] 
    ans=0
    for i in range(v_max+1):
        if dp[i][1]<=W-0.5:
            ans=i
    print(ans)

def main():
    n,t=inpm()
    a=[]
    b=[]
    for _ in range(n):
        x,y=inpm()
        a.append(x)
        b.append(y)
    Knapsack2(t,a,b)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

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

# for dp_i in dp:
#     print(dp_i)

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

Submission status: Wrong Answer

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, T = MAP()
AB = []
for i in range(N):
    a, b = MAP()
    AB.append((a, b))

TMAX = 6007
dp = list2d(N+1, TMAX, 0)
for i in range(N):
    a, b = AB[i]
    for j in range(TMAX):
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        if j < T:
            dp[i+1][j+a] = max(dp[i+1][j+a], dp[i][j] + b)
print(max(dp[N]))

Submission status: Wrong Answer

Solution code:

import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    N, T = map(int,input().split())
    F = [list(map(int,input().split())) for k in range(N)]

    F = sorted(F, key = lambda x: (-x[1],-x[0]))
    t = F.pop(0)

    weight = [0 for k in range(N)]
    value = [0 for k in range(N)]
    for k in range(N-1):
        weight[k] = F[k][0]
        value[k] = F[k][1]

    dp = [[-10000000 for i in range(T+1)] for j in range(N+1)]

    for i in range(T+1):
        dp[0][i]=0

    for i in range(N-1):
        for w in range(T):
            if weight[i]<=w: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
                dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
            else: #入る可能性はない
                dp[i+1][w]=dp[i][w]
    print(dp[N-1][T-1] + t[1])

if __name__ == '__main__':
    main()