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())
x=[]
for i in range(N):
    Ai,Bi=map(int,input().split())
    x.append((Ai,Bi))

x=sorted(x,key=lambda x:(x[1],x[0]))
print(x)
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:
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a)]
c.sort()
DP=[[0 for j in range(b+1)]for i in range(a)]
for i in range(c[0][0],b+1):
  DP[0][i]+=c[0][1]
for i in range(1,a):
  for j in range(c[i][0],b):
    DP[i][j]=max(DP[i-1][j-c[i][0]]+c[i][1],DP[i-1][j])
  DP[i][b]=max(DP[i-1][b-1]+c[i][1],DP[i-1][b])
print(DP[-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,w = inpl()
wv = [inpl() for i in range(n)]
dp = [[0 for i in range(w+1)] for j in range(n+1)]
wv.sort(key=lambda x:x[1])
ww = [0] * n
vv = [0] * n
for i in range(n):
    ww[i],vv[i] = wv[i][0], wv[i][1]
for i in range(n-1):
    for j in range(w+1):
        if j >= ww[i]:
            dp[i+1][j] = max(dp[i][j], dp[i][j-ww[i]]+vv[i])
        else:
            dp[i+1][j] = dp[i][j]
print(dp[n-1][w-1]+wv[n-1][1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

n,t = map(int,input().split())

abl = []
for _ in range(n):
    a,b = map(int,input().split())
    abl.append((a,b))
    
dp = [[0 for i in range(t-1)] for j in range(n+1)]

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

for _ in range(n):
    m = max([i[1] for i in abl])
    rest_food.append(m)
    abl.pop(0)

ans = 0
for i in range(n):
    tmp = rest_food[i] + dp[i][t-2]
    if tmp > ans:
        ans = tmp

print(ans)

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-0.5:
			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))

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()
    maxA=max(a,maxA)
    c.append((a,b))

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])
#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 = [[0] * (T+2) for _ in range(N+1)]
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+2):
        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(dp[N][T])

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,w = inpl()
wv = [inpl() for i in range(n)]
dp = [[0 for i in range(w+1)] for j in range(n+1)]
wv.sort()
wv.sort(key=lambda x:x[1])
ww = [0] * n
vv = [0] * n
for i in range(n):
    ww[i],vv[i] = wv[i][0], wv[i][1]
for i in range(n-1):
    for j in range(w+1):
        if j >= ww[i]:
            dp[i+1][j] = max(dp[i][j], dp[i][j-ww[i]]+vv[i])
        else:
            dp[i+1][j] = dp[i][j]
print(dp[n-1][w-1]+wv[n-1][1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import  numpy as np

def solve():
    N, T = map(int, input().split())
    C = [tuple(map(int, input().split())) for _ in range(N)]
    C.sort(key=lambda c: c[0]/c[1])
    dp = np.zeros(T+1, dtype=int)
    t = np.arange(T+1)
    for a,b in C:
        s = np.minimum(T, t+a)
        dp[s] = np.maximum(dp[t]+b, dp[s])
    return dp[T]

if __name__ == '__main__':
    print(solve())

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,x-1,-1):
        if 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]]

        elif DP[time][0]==DP[time-x][0]+y:

            if DP[time-x][1]==ind:
                DP[time][1]=min(ind+1,DP[time][1])
            else:
                DP[time][1]=min(DP[time-x][1],DP[time][1])

    #print(DP)

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:
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)
        return max(vl, vr)

N, T = map(int, input().split())
AB = [None] * N
for i in range(N):
    AB[i] = tuple(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 = [SegmentTree([0]+[-INF]*T) for i in range(N+1)]
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j:
            tmp = dp2[i].seq[j-A]+B
            tmp2 = dp2[i].seq[j]
            if tmp > tmp2:
                dp2[i+1].update(j, tmp)
            else:
                dp2[i+1].update(j, tmp2)
        else:
            dp2[i+1].update(j, dp2[i].seq[j])

ans = max(dp1[N])

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

_ans = 0
B = AB[0][1]
for j in range(T):
    tmp = dp1[N-1][j]+B
    if tmp > _ans:
        _ans = tmp
ans = max(ans, _ans)

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

Submission status: Wrong Answer

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

L.sort(key=lambda x:x[2], reverse=True)
summation = 0
while t-1 >= 0:
    for i, elm in enumerate(L):
        if elm[0] <= t-1:
            L.pop(i)
            summation += elm[1]
            t -= elm[0]
            break
    else:
        break

if len(L) != 0:
    resmax = max(L, key=lambda x:x[1])[1]
    summation += resmax

print(summation)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

n,t=LI()
t-=1
dp=[[-1]*5010 for _ in range(5010)]
l=[LI() for _ in range(n)]

def rec(i,j,cnt):
  if dp[i][j]>=0:
    return dp[i][j]

  if i==n:
    res=0
  elif j<l[i][0]:
    if cnt==n-1:
      cnt+=1
      res=max(rec(i+1,j,cnt),rec(i+1,j-l[i][0],cnt)+l[i][1])
    else:
      res=rec(i+1,j,cnt)
  else:
    cnt+=1
    res=max(rec(i+1,j,cnt),rec(i+1,j-l[i][0],cnt)+l[i][1])
  dp[i][j]=res
  return res

def main():
  return rec(0,t,0)

# main()
print(main())

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
dp=[-1 for i in range(t+1)]
s=[0 for i in range(t+1)]
d=[]
dp[0]=0
s[0]=0
for i in range(n):
    d.append(list(map(int,input().split())))
d=sorted(d,key=lambda x: -x[1])
for i in range(n):
    for j in range(t,-1,-1):
        if dp[j]>=0 and j+d[i][0]<t:
            if dp[j+d[i][0]]<dp[j]+d[i][1]:
                dp[j+d[i][0]]=dp[j]+d[i][1]
                if i<n-1:
                    s[j+d[i][0]]=d[i+1][1]
ans=0
for i in range(t):
    ans=max(ans,dp[i]+s[i])
print(max(ans,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)]

for i in range(n):
    li[i].append(i)

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

ma.sort(reverse = True)

t = t-1
dp = [[0,0] 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]
                pi = 2**li[i][2]
                dp[k][1] = pi | dp[k-li[i][0]][1] 


dp.sort(reverse = True)
ans = dp[0][0]
re = 1
for i in range(n):
    kkk = ma[i][1]
    if not (2 ** kkk) & dp[0][1]:
        ans += ma[i][0]
        break
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[0])
ab.sort(key=lambda x:x[1], reverse=True)

for i in range(n):
    a,b=ab[i]
    c=0
    now=0
    for j in range(n):
        if i==j:
            continue
        now+=ab[j][0]
        if now<t:
            c+=ab[j][1]
        else:
            now-=ab[j][0]
    ans=max(ans,c+b)
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[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())
arr = []
for i in range(n):
    arr.append(list(map(int,input().split())))
arr.sort(key = lambda x:x[0])
dp = [[0]*(t+1) for _ in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j > 0:
            if j - arr[i][0] > 0:
                dp[i + 1][j-arr[i][0]] = max(dp[i + 1][j-arr[i][0]],dp[i][j] + arr[i][1])
            else:
                dp[i + 1][0] = max(dp[i + 1][0],dp[i][j] + arr[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())

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

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

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

for x in range(1, N + 1):
    for y in range(T + MAX):
        if y >= A[x - 1] and (y - A[x - 1]) < T:
            dp[x][y] = max(dp[x - 1][y], dp[x - 1][y - A[x - 1]] + B[x - 1])
        
        else:
            dp[x][y] = dp[x - 1][y]
        
        
ans = -1
for d in dp:
    ans = max(ans, max(d))

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
I = [int(_) for _ in open(0).read().split()]
N, T = I[:2]
A, B = I[2::2], I[3::2]
dp = np.array([-1] * 6001, dtype=np.int64)
dp[0] = 0
for a, b in zip(A, B):
    dp_new = dp[:]
    dp_new[a:a + T] = np.maximum(dp[a:a + T], dp[:T] + b)
    dp = dp_new
print(max(dp))

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)+max(w)+10
    # dp[i] : 価値iの組み合わせを達成する重量の最小値
    dp=[0]+[10**8 for _ in [0]*(v_max+1)]
    over=[0 for _ in [0]*(v_max+2)]
    for i in range(n):
        for j in range(v_max,v[i]-1,-1):
            if j >= v[i]:
                a=dp[j]
                dp[j]=min(dp[j],dp[j-v[i]]+w[i])
                if a!=dp[j]:
                    over[j]=max(over[j],w[i])
    ans=0
    for i in range(v_max+1):
        if dp[i]-over[j]<=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:
# -*- coding: utf-8 -*-

import copy

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)]
tabelog_old = [[0] * N for i in range(T)]
tabelog_new = [[0] * N for i in range(T)]

for i in range(1, N+1):
    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
            tabelog_new[t] = copy.copy(tabelog_old[t-a])
            tabelog_new[t][i-1] = 1
        else:
            dp[i][t] = dp[i-1][t]
            tabelog_new[t] = copy.copy(tabelog_old[t])

    tabelog_old = copy.copy(tabelog_new)

ans = dp[N][T-1]
betsubara = max([AB[i][1] for i in range(i) if tabelog_old[T-1][i] == 0] + [0])
print(ans + betsubara)

Submission status: Wrong Answer

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


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]


sorted_b = sorted(b)
print(knapsack(N=n, W=t+sorted_b[1], weight=a, value=b))

Submission status: Wrong Answer

Solution code:

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 


#x,y = [int(i) for i in readline().split()]
#n = int(input())

n,t = [int(i) for i in readline().split()]
ab = [[int(i) for i in readline().split()] for i in range(n)]


dp = [0]*(t)
val = 0

for a,b in ab:
    for i in range(max(0,t-a),t):
        val = max(val,dp[i]+b)
    
    for i in range(t-1,-1,-1):
        if i-a >= 0:
            if dp[i-a] != 0 or i==a:
                dp[i] = max(dp[i], dp[i-a] + b)
        else:
            break
    #print(dp,val)

print(max(max(dp),val))

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)] 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-1):
    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())

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

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

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

for x in range(1, N + 1):
    for y in range(T + MAX):
        if y >= A[x - 1] and (y - A[x - 1]) < T:
            dp[x][y] = max(dp[x - 1][y], dp[x - 1][y - A[x - 1]] + B[x - 1])
        
        else:
            dp[x][y] = dp[x - 1][y]
        
        
ans = -1
for d in dp:
    ans = max(ans, max(d))

print(ans)

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

dp = [[0] * (3000 + 1) for _ in range(3000 + 1)]
for i in reversed(range(n)):
    for time in range(3000):
        if time >= t:
            break

        if time + ab[i][0] < t:
            dp[i][time] = max(dp[i + 1][time + ab[i][0]] + ab[i][1], dp[i + 1][time])
        else:
            dp[i][time] = dp[i + 1][time]
print(dp[0][0])

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 for _ in range(N+1)]
pa = [0] * T  # prev a
for i in range(1, N+1):
    a, b = D[i-1]
    b = -b
    for t in range(T):
        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:
n,t=map(int,input().split())
dp=[-1 for i in range(t+1)]
s=[0 for i in range(t+1)]
d=[]
dp[0]=0
s[0]=0
for i in range(n):
    d.append(list(map(int,input().split())))
d=sorted(d,key=lambda x: -x[1])
for i in range(n-1):
    for j in range(t,-1,-1):
        if dp[j]>=0 and j+d[i][0]<=t:
            if dp[j+d[i][0]]<dp[j]+d[i][1]:
                dp[j+d[i][0]]=dp[j]+d[i][1]
                s[j+d[i][0]]=d[i+1][1]
ans=0
for i in range(t+1):
    ans=max(ans,dp[i]+s[i])
print(ans)

Submission status: Wrong Answer

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

dp = [[0]*(t+1) for _ in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j > 0:
            if j - arr[i][0] > 0:
                dp[i + 1][j-arr[i][0]] = max(dp[i + 1][j-arr[i][0]],dp[i][j] + arr[i][1])
            else:
                dp[i + 1][0] = max(dp[i + 1][0],dp[i][j] + arr[i][1])
        dp[i + 1][j] = max(dp[i + 1][j],dp[i][j])
print(max(dp[n]))

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(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 and 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:
from operator import itemgetter
N,T = map(int,input().split())
menu = list()
for i in range(N):
	menu.append(list(map(int,input().split())))
# 時間が少ない順に計算
menu.sort() # 時間が少ない順にソート
good_1 = 0 # 美味しさ
time = 0 # 時間
for i in range(len(menu)):
	if time > T-0.5:
		break
	else:
		good_1 += menu[i][1]
		time += menu[i][0]

# 満足度が多い順に計算
menu.sort(key=itemgetter(1),reverse=True)
good_2 = 0 # 美味しさ
time = 0 # 時間
for i in range(len(menu)):
	if time > T-0.5:
		break
	else:
		good_2 += menu[i][1]
		time += menu[i][0]
print(max(good_1,good_2))

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
dp = [[0 for _ in range(t+1)]]
for _ in range(n):
  w,v = map(int,input().split())
  tmp = [0]
  for f in range(1,t):
    #tmp.append(max(tmp[-1],dp[-1][f-w]+v))
    if f - w >= 0:
      if dp[-1][f-w] + v > dp[-1][f]:
        tmp.append(dp[-1][f-w] + v)
      else:
        tmp.append(dp[-1][f])
    else:
      tmp.append(dp[-1][f])
  tmp.append(max(dp[-1][-2]+v,dp[-1][-1]))
  dp.append(tmp)
print(max(dp[-1][-1],dp[-1][-2]))
#for x in dp:
  #print('#',len(x),x)

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 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:
N, T = map(int, input().split())

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

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

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

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

print(max(dp[N][:]))

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)] for i in range(N)]

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

        # これは配る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-1):
    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())
c = [tuple(map(int,input().split())) for _ in range(n)]
c.sort(key=lambda x: -x[1])
dp = [[-1]*t for _ in range(n+1)]
dp[0][0] = 0
for r in range(n):
    a,b = c[r]
    for i in range(t):
        if dp[r][i] >= 0:
            dp[r+1][i] = dp[r][i]
            if i+a < t and dp[r+1][i+a] < dp[r][i] + b:
                dp[r+1][i+a] = dp[r][i] + b
ct,id = 0,0
for i in range(t):
    if ct < dp[-1][i]:
        id = i
        ct = dp[-1][i]
ans = dp[-1][id]
cr = n
s = set()
while cr > 0 and id > 0:
    a,b = c[cr-1]
    if id >= a and dp[cr-1][id-a] + b == dp[cr][id]:
        s.add(cr-1)
        id -= a
    cr -= 1
lis = enumerate(c)
for id, x in lis:
    if id not in s:
        ans += x[1]
        break
print(ans)

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]]

        elif 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,min(ind+1,DP[time][1])]
            else:
                DP[time]=[DP[time-x][0]+y,min(DP[time-x][1],DP[time][1])]

    #print(DP)

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

def main():
    n,t=map(int,input().split())
    AB=[list(map(int,input().split())) for _ in range(n)]
    AB.sort(key=lambda x: (x[0],-x[1]))
    DP=[[0]*(6001) for _ in range(n+1)]
    for i,(a,b) in enumerate(AB):
        for j in range(1,6001):
            if j<a:
                DP[i+1][j]=max(DP[i][j],DP[i+1][j-1])
            elif j-a<t:
                DP[i+1][j]=max(DP[i][j],DP[i+1][j-1],DP[i][j-a]+b)
            else:
                DP[i+1][j]=max(DP[i][j],DP[i+1][j-1])
    print(max(DP[-1]))
    print(AB)
    
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def solve():

    N, T = map(int, input().split())
    C =  [tuple(map(int, input().split())) for _ in range(N)]
    C.sort(key=lambda c: c[0]/c[1])
    dp = [0 for _ in range(T+1)]
    for a,b in C:
        for t in range(T-1, -1, -1):
            dp[min(T,t+a)] = max(dp[t]+b, dp[min(T,t+a)])
    return dp[T]

if __name__ == '__main__':
    print(solve())

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(T):
        if dp[k] == -1:
            continue
        dp_new[k + a] = max(dp[k + a], dp[k] + b)
    dp = dp_new[:]
print(max(dp))