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:
"""
a=int(input())
s=input()
print(s if a>=3200 else "red")
"""
"""
n=int(input())
a=list(map(int,input().split()))
x=0
for t in a:
	x+=1/t
print(1/x)
"""
"""
n=int(input())
v=list(map(int,input().split()))
v.sort()
x=v[0]
for i in range(1,n):
	x=(x+v[i])/2
print(x)
"""


"""

"""

"""
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n-1)]
pq=[list(map(int,input().split())) for _ in range(m)]
ab2=sorted(ab, key=lambda x:x[0])
pq2=sorted(pq, key=lambda x:x[0])

res=[0 for _ in range(n)]
for p,q in pq2:
	res[p-1]+=q
for a,b in ab2:
	res[b-1]+=res[a-1]
ans=str(res[0])
for i in range(1,n):
	ans+=" "+(str(res[i]))
print(ans)
"""
"""
s=input()
t=input()
x=list(s)
y=list(t)
import sys
if len(set(y)-set(x))!=0:
	print(-1)
	sys.exit()
a=[[] for _ in range(26)]
for i in range(len(s)):
	a[ord(x[i])-97].append(i)
import bisect

repeat=0
last=-1
for i in range(len(t)):
	temp=a[ord(y[i])-97]
	if last>=temp[-1]:
		repeat+=1
		last=temp[0]
	else:
		last=temp[bisect.bisect_right(temp,last)]
	#print(temp,repeat,last)
print(repeat*len(s)+last+1)
"""

"""
m,d=map(int,input().split())

res=0
for x in range(1,m+1):
	for y in range(20,d+1):
		if y%10>=2 and (y%10)*(y//10)==x:
			res+=1
print(res)
"""

"""
n,k=map(int,input().split())
a=list(map(int,input().split()))

x=[0 for _ in range(n)]
for i in range(1,n):
	for j in range(i):
		if a[j]>a[i]:
			x[i]+=1

y=[0 for _ in range(n)]
for i in range(1,n):
	for j in range(i):
		if a[j]<a[i]:
			y[i]+=1


print((sum(x)*k*(k+1)//2+sum(y)*k*(k-1)//2)%(10**9+7))
"""
"""
def kaijo(x):
	a=1
	for i in range(1,x+1):
		a*=i
		a%=10**9+7
	return a

n=int(input())
s=input()
b,w=0,0
for i in range(n):



mod=10**9+7
if b>=0:
	print(pow(3,b,mod)*pow(2,w,mod)*kaijo(n)%mod)
"""
"""
a,b,t=map(int,input().split())
x=b
while x<t:
	x+=b-a
print(x)
"""

"""
s=input()
t=input()
res=0
for i in range(len(s)):
	if s[i]==t[i]:
		res+=1
print(res)
"""

"""
a,b=map(int,input().split())
print((-(-b//a)) if b>1 else 0)
"""
"""
n=int(input())
h=list(map(int,input().split()))
res=[]
t=0
for i in range(1,n):
	if h[i]<=h[i-1]:
		t+=1
	else:
		res.append(t)
		t=0
res.append(t)
print(max(res))
"""

"""
n=int(input())
print(n*(n-1)//2)
"""
"""
a,b=map(int,input().split())
x=1
res=0
while(x<b):
	x+=a-1
	res+=1
print(res)
"""
###########################################

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

res=[]
tx,ty=0,0
for x,y in xy:
	if x>0 and y>0:
		tx+=x
		ty+=y
for x,y in xy:
	if x+y<=0:
		if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
			tx+=x
			ty+=y
res.append((tx**2+ty**2)**0.5)

tx,ty=0,0
for x,y in xy:
	if x-y>0:
		tx+=x
		ty+=+y
for x,y in xy:
	if x-y<=0:
		if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
			tx+=x
			ty+=y
res.append((tx**2+ty**2)**0.5)

tx,ty=0,0
for x,y in xy:
	if -x+y>0:
		tx+=x
		ty+=y
for x,y in xy:
	if -x+y<=0:
		if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
			tx+=x
			ty+=y
res.append((tx**2+ty**2)**0.5)
tx,ty=0,0
for x,y in xy:
	if -x-y>0:
		tx+=x
		ty+=y
for x,y in xy:
	if -x-y<=0:
		if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
			tx+=x
			ty+=y
res.append((tx**2+ty**2)**0.5)

print(max(res))




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

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

xy1=[] #(+,+)
xy2=[] #(-,+)
xy3=[] #(-,-)
xy4=[] #(+,-)
for x,y in xy:
	if x>=0 and y>=0:
		xy1.append([x,y])
	if x<0 and y>=0:
		xy2.append([x,y])
	if x<0 and y<0:
		xy3.append([x,y])
	if x>=0 and y<0:
		xy4.append([x,y])
#print(xy1,xy2,xy3,xy4)
res=[]
tx,ty=0,0
for x,y in xy1:
	tx+=x
	ty+=y
txy=[]
for x,y in xy2:
	if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
		txy.append([x,y])
for x,y in xy4:
	if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
		txy.append([x,y])
for x,y in txy:
	tx+=x
	ty+=y
res.append(tx**2+ty**2)

tx,ty=0,0
for x,y in xy2:
	tx+=x
	ty+=y
txy=[]
for x,y in xy1:
	if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
		txy.append([x,y])
for x,y in xy3:
	if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
		txy.append([x,y])
for x,y in txy:
	tx+=x
	ty+=y
res.append(tx**2+ty**2)

tx,ty=0,0
for x,y in xy3:
	tx+=x
	ty+=y
txy=[]
for x,y in xy2:
	if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
		txy.append([x,y])
for x,y in xy4:
	if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
		txy.append([x,y])
for x,y in txy:
	tx+=x
	ty+=y
res.append(tx**2+ty**2)

tx,ty=0,0
for x,y in xy4:
	tx+=x
	ty+=y
txy=[]
for x,y in xy1:
	if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
		txy.append([x,y])
for x,y in xy3:
	if (tx+x)**2+(ty+y)**2>tx**2+ty**2:
		txy.append([x,y])
for x,y in txy:
	tx+=x
	ty+=y
res.append(tx**2+ty**2)
print(max(res)**0.5)
"""
"""
s=input()
if s[0]=="S":
	print("Cloudy")
elif s[0]=="C":
	print("Rainy")
else:
	print("Sunny")
"""
"""
s=input()
n=len(s)
j=True
for i in range(0,n,2):
	if s[i] not in list("RUD"):
		j=False
for i in range(1,n,2):
	if s[i] not in list("LUD"):
		j=False
print("Yes" if j else "No")
"""
"""
n,k,q=map(int,input().split())
a=[int(input()) for _ in range(q)]
x=[k-q for _ in range(n)]
for t in a:
	x[t-1]+=1
for t in x:
	if t>0:
		print("Yes")
	else:
		print("No")
"""
"""
n,m=map(int,input().split())
a=list(map(int,input().split()))

import heapq
i=0
heap=[]
res=[]
for t in a:
	heapq.heappush(heap,-t)
for _ in range(m):
	t=heapq.heappop(heap)
	heapq.heappush(heap,-(-t//2))
print(-sum(heap))
"""

"""
a,b=map(int,input().split())
if a<10 and b<10:
	print(a*b)
else:
	print(-1)
"""

"""
import sys
n=int(input())
for i in range(1,10):
	for j in range(1,10):
		if i*j==n:
			print("Yes")
			sys.exit()
print("No")
"""

"""
import sys
n=int(input())
res=[]
for i in range(1,int(n**0.5)+1):
	if n%i==0:
		res.append(i+n//i-2)
print(min(res))
"""
"""
a,b,x=map(int,input().split())
import math

ans=math.degrees(math.atan(2*(a**2*b-x)/(a**3)))
if ans>=math.degrees(math.atan(b/a)):
	ans=90-math.degrees(math.atan(2*x/(a*(b**2))))
print(ans)
"""

"""
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=list(map(int,input().split()))

tk=k

a.sort()
l.sort()
l=l[::-1]

bd=((sum(a)-k)*sum(l))//(n*n)+1
#print(bd)

import sys
from heapq import *
if sum(a)<=k:
	print(0)
	sys.exit()

h=[]
for i in range(n):
	heappush(h,(-a[i]*l[i], a[i],l[i]))
#print(h)
while(tk>0):
	t=heappop(h)
	if -t[0]>bd and tk>0:
		td=min(t[1],-(-((-t[0])-bd)//t[2]))
		tk-=td
		#print(-(t[1]-td)*t[2],t[1]-td,t[2])
		heappush(h,(-(t[1]-td)*t[2],t[1]-td,t[2]))
	elif tk>0:
		td=min(t[1],tk//n)
		tk-=td
		#print(-(t[1]-td)*t[2],t[1]-td,t[2])
		heappush(h,(-(t[1]-td)*t[2],t[1]-td,t[2]))
	else:
		heappush(h,t)
		break
	#print(tk)


#for i in range(n):
	#print(heappop(h))

print(-heappop(h)[0])
"""

"""
n=int(input())
print((n-1)//2)

"""

"""
import sys
n=int(input())
d=list(map(int,input().split()))
d.sort()
from collections import Counter
cs=Counter(d)
b=list(cs.values())
c=list(cs.keys())
#print(b,c)
if c!=[i for i in range(max(c)+1)]:
	print(0)
	sys.exit()
t=1
for i in range(len(b)-1):
	t*=b[i]**b[i+1]
	t%=998244353
print(t)
"""

"""
import sys
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=sorted(a)
d=sorted(b)
for i in range(n):
	if c[i]>d[i]:
		print("No")
		sys.exit()
if len(set(a))<n:
	print("Yes")
	sys.exit()
res=0
for i in range(n):
	if a[i]>b[i]:
		res+=1
if res<n:
	print("Yes")
else:
	print("No")
"""
"""
import itertools
n=4
x=[1,2,2,3]
p=itertools.permutations(x,n)
for t in p:
	c=0
	for i in range(n):
		if x[i]!=t[i]:
			c+=1
	if c==n:
		print(t)
"""
"""
r=int(input())
print(r**2)
"""
"""
n=int(input())
s=input()
import sys
if n%2!=0:
	print("No")
	exit()
elif s[0:n//2]!=s[n//2:n]:
	print("No")
else:
	print("Yes")
"""

"""

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

res=0
def func(n):
	x=1
	for i in range(1,n+1):
		x*=i
	return x

for i in range(n-1):
	for j in range(i+1,n):
		res+=((xy[i][0]-xy[j][0])**2+(xy[i][1]-xy[j][1])**2)**0.5*2*func(n-1)
print(res/func(n))

"""

"""
import sys
x,y=map(int,input().split())
n=(x+y)//3
a,b=-1,-1
if (n*3)!=(x+y):
	print(0)
	sys.exit()
for i in range(n+1):
	#print(i*2+(n-i),i+(n-i)*2)
	if i*2+(n-i)==x and i+(n-i)*2==y:
		a,b=i,n-i
#a:x+1 y+2
#b:x+2 y+1
if a<0 or b<0:
	print(0)
	sys.exit()

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
print(cmb(a+b,a)%(10**9+7))
"""
"""
n,t=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n)]
ab=sorted(ab, key=lambda x:x[1])

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


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:
import math
import itertools

def read():
    N, T = list(map(int, input().strip().split()))
    A = [0 for _ in range(N+1)]
    B = [0 for _ in range(N+1)]
    for i in range(N):
        A[i], B[i] = list(map(int, input().strip().split()))
    return N, T, A, B

def init_dp(N, T):
    return [[0 for t in range(T+3001)] for i in range(N+3001)]

def solve(N, T, A, B):
    dp = init_dp(N, T)  # dp[N][T]
    max_A = [0 for _ in range(T+3001)]
    # dp[i][t] = i番目以降の食べ物から、合計時間t以下となるように選んだ時の満足度
    for t in range(T+3001):
        dp[N][t] = 0
    for i in range(N-1, -1, -1):
        for t in range(T+3001):
            if t+max_A[t] < A[i]:
                dp[i][t] = dp[i+1][t]
            else:
                dp[i][t] = max(dp[i+1][t], dp[i+1][t-A[i]] + B[i])
                max_A[t] = max(max_A[t], A[i])
    return dp[0][(T-1)+max_A[T-1]]
    
if __name__ == '__main__':
    inputs = read()
    output = solve(*inputs)
    print("%d" % output)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, T = map(int, input().split())
        AB = [list(map(int, input().split())) for _ in range(N)]
        AB.sort(key=lambda ab: ab[1] - ab[0], reverse=True)

        total_t = 0
        ans = 0
        umami_min = 3000
        boder = 0
        for i, ab in enumerate(AB):
            if total_t + ab[0] > T - 0.5:
                boder = i
                break
            else:
                umami_min = min(ab[1], umami_min)
                ans += ab[1]
                total_t += ab[0]

        nokori = AB[boder:]
        if len(nokori) > 1:
            nokori.append([0, umami_min])
            nokori.sort(key=lambda ab: ab[1], reverse=True)

            ans -= umami_min
            for i in range(2):
                ans += nokori[i][1]
        else:
            ans += AB[boder][1]

        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
# 時間ギリギリまで時間単価の幸福度が大きいものから食べて
# 最後は幸福度の最大の料理

done_li = [[0] for _ in range(N)]
sAB = sorted(AB, key=lambda x: - x[1] / x[0])
t = 0
ans = 0
for itr, val in enumerate(sAB):
  a, b = val
  if t + a < T:
    t += a
    ans += b
    done_li[itr] = 1

maxv = 0
for itr, val in enumerate(sAB):
  if done_li[itr] == 0:
    maxv = max(maxv, val)
    
# print(stop_index)

ans += maxv

print(ans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
d = {}
for i in range (n):
  a, b = map(int, input().split())
  d.setdefault(a, [])
  d[a].append(b)
l = sorted([(x, sorted(y, reverse=True)) for x, y in d.items()])
x = res = num = 0
while x < t and num < n:
  for i in l:
    for j in i[1]:
      x += i[0]
      res += j
      num += 1
if num < n:
	res += max([j for j in t[1] for t in l][num+1:])
print(res)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
X = []
for _ in range(n):
    a, b = map(int, input().split())
    X += [(-b, a)]

X = sorted(X)

from heapq import heappush, heappop

x1 = heappop(X)

from itertools import permutations
import math

def calc_ha(z_list, time):
    _time = 0
    deli = 0
    for _x2, _y2 in z_list:
        _time += _y2
        deli += _x2
        if _time >= time:
            deli -= _x2
            break
    return abs(deli)

ans = 0
for z in permutations(X):
    _ans = calc_ha(z, t)
    if ans < _ans:
        ans = _ans

print(ans+abs(x1[0]))

Submission status: Wrong Answer

Solution code:
import math
import numpy
import copy as cp


def dp(A,B,N,T,test):
    if T>=0 and test<=0:
        return True
    if T <= 0:
        return False
    if N==1:
        return B[0]>=test
    else:
        totalA=sum(A)-max(A)
        if totalA < T:
            return sum(B) >= test
        else:
            for i in range(N):
                A_n = A[:]
                B_n = B[:]
                A_n = A_n[:i]+A_n[i+1:]
                B_n = B_n[:i]+B_n[i+1:]
                t = T - A[i]
                ans = dp(A_n,B_n,N-1,t,test-B[i])
                #print(ans,A_n,B_n,N-1,t,test-B[i])
                if ans:
                    return True
            return False
        

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

start = 0
end= sum(B)
if dp(A,B,N,T,end):
    out = end
else:
    while abs(end-start) != 1:
        test = (start+end)//2
        ans = dp(A,B,N,T,test)
        #print(ans,test)
        if ans:
            start = test
        else:
            end = test
    out = start
print(out)

Submission status: Wrong Answer

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

foods.sort(reverse=True)
time = 0
umasa = 0

foods_before = [f for f in foods if f[2] < t]
foods_after = [f for f in foods if f[2] >= t]
for food in foods_before:
    if time + food[2] >t:
        break
    else:
        umasa += food[1]
        time += food[2]

if len(foods_after)>=1:
    umasa += max(foods_after)[1]

print(umasa)

Submission status: Wrong Answer

Solution code:
def main():  
  n,t = map(int,input().split())
  ab = [list(map(int,input().split())) for _ in range(n)]
  c = [[0]*(6000) for _ in range(n+1)]
  mt = [[0]*(6000) for _ in range(n+1)]
  m = 0
  for i in range(1,n+1):
    for j in range(1,6000):
      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]]
          mt[i][j] = max([mt[i-1][j-ab[i-1][0]],ab[i-1][0]])
          if j-mt[i][j] < t:
            m = max([c[i][j],m])
          else:
            c[i][j] = c[i-1][j]
  print(m)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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

amax = max(AB)[0]
tmax = T+amax-1

dp = [[[] for i in range(tmax)] for i in range(N+1)]
for i in range(tmax):
	dp[0][i] = 0
i = 0
for ai, bi in AB:
	for j in range(tmax):
		if j>=ai:
			dp[i+1][j] = max(dp[i][j-ai]+bi, dp[i][j])
		else:
			dp[i+1][j] = dp[i][j]
	i += 1
print(dp[-1][-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)]
AB.sort(key=lambda x: x[0])
def dfs(i, sum, time, remain):
    global answer
    if i == N or time + AB[i][0] > T:
        if time < T and len(remain) >= 1:
            sum += AB[min(remain)][1]
        if sum > answer:
            answer = sum
    else:
        dfs(i+1, sum, time, remain)
        if time < T:
            dfs(i+1, sum + AB[i][1], time + AB[i][0], remain-{i})

answer = 0
dfs(0, 0, 0, set(range(N)))
print(answer)

Submission status: Wrong Answer

Solution code:
def solve():
    import sys

    input = sys.stdin.readline

    n, t = map(int, input().split())  # t - 1まで注文OK
    ab = tuple(tuple(map(int, input().split())) for _ in range(n))

    dp = [[[0] * t for _ in range(n)] for _ in range(n)]
    # dp[最後の皿][時間] = 最大おいしさ
    # dp[T-1] + 残りで一番美味しいもの
    # 先にどの皿を最後に食べるか決めておいて、それ以外でdp
    # 3000回dp

    ret = 0
    last_dish = n - 1
    _, last_b = ab[-1]

    for dish, (a, b) in enumerate(ab):
        if dish == last_dish:
            for t_ in range(t - 1, a - 1, -1):
                dp[last_dish][dish][t_] = dp[last_dish][dish][t_]
            for t_ in range(min(max(0, a - 1), t - 1), -1, -1):
                dp[last_dish][dish][t_] = dp[last_dish][dish][t_]
        else:
            for t_ in range(t - 1, a - 1, -1):
                dp[last_dish][dish][t_] = max(dp[last_dish][dish][t_], dp[last_dish][dish][t_ - a] + b)
            for t_ in range(min(max(0, a - 1), t - 1), -1, -1):
                dp[last_dish][dish][t_] = dp[last_dish][dish][t_]

    ret = max(ret, max(dp[last_dish][n - 2]) + last_b)

    # print(dp, ret)

    for last_dish, (_, last_b) in enumerate(ab[:-1]):
        for dish, (a, b) in enumerate(ab):
            if dish < last_dish:
                continue
            if dish == last_dish:
                for t_ in range(t - 1, a - 1, -1):
                    dp[last_dish][dish][t_] = dp[n - 1][dish - 1][t_]
                for t_ in range(min(max(0, a - 1), t - 1), -1, -1):
                    dp[last_dish][dish][t_] = dp[last_dish][dish - 1][t_]
            else:
                for t_ in range(t - 1, a - 1, -1):
                    dp[last_dish][dish][t_] = max(dp[last_dish][dish][t_], dp[last_dish][dish][t_ - a] + b)
                for t_ in range(min(max(0, a - 1), t - 1), -1, -1):
                    dp[last_dish][dish][t_] = dp[last_dish][dish][t_]
        ret = max(ret, max(dp[last_dish][n - 1]) + last_b)

    # print(dp)

    return ret


print(solve())

Submission status: Wrong Answer

Solution code:
def get():
    return map(int,input().split())

N,W=get()
w=[0]*N
v=[0]*N
for i in range(N):
    w[i],v[i]=get()
if W>=sum(w):
    print(sum(v))
else:
    dp=[0]*W
    dp2=[0]*W
    for i in range(N):
        for j in range(W-1,w[i]-1,-1):
            dp2[j]=max(dp[j]+v[i], dp2[j],dp2[j-w[i]]+v[i])
            dp[j]=max(dp[j],dp[j-w[i]]+v[i])
    print(dp2[W-1])

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

for i in range(n):
    a,b=LI()
    c.append((a,b))


def solve(order,T):
    rt=0
    for i in range(n-1):
        show('-------------',i)
        tmp=0
        l_a,l_b=order[i]
        q=[]
        for j in range(n-1):
            if i!=j:
                a,b=order[j]
                heappush(q,(-b/a,j))
        
        t=0
        while t<T and q:
            ef,i=heappop(q)
            a,b=c[i]
            if t+a>T-0.5:
                break
            tmp+=b
            t+=a
            show('t,a,b,tmp',t,a,b,tmp)
        tmp+=l_b*0
        show(tmp,l_b)
        rt=max(rt,tmp)
        
    return rt


t=0
ans=0
for i in range(n):
    if i==n-1:
        ans=max(solve(c[:i],T)+c[i][1],ans)
    else:
        ans=max(solve(c[:i]+c[i+1:],T)+c[i][1],ans)

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)]
AB.sort(key=lambda x: x[0])
print(AB)
def dfs(i, sum, time, remain):
    global answer
    if i == N or time + AB[i][0] > T:
        if time < T and len(remain) >= 1:
            sum += AB[min(remain)][1]
        if sum > answer:
            answer = sum
    else:
        dfs(i+1, sum, time, remain)
        if time < T:
            dfs(i+1, sum + AB[i][1], time + AB[i][0], remain-{i})

answer = 0
dfs(0, 0, 0, set(range(N)))
print(answer)

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)
    for a,b in C:
        s = np.minimum(T, t+a)
        dp[s] = np.maximum(dp[t]+b, dp[s])
    return max(dp)

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

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
dishes = [tuple(map(int, input().split())) for _ in range(N)]
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:
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)
        #print(mb,a)
        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(q) == 0 and curr < T and len(tmp) > 0:
        mb, a, i = heapq.heappop(tmp)
        ans += -1*mb
        curr +=  a


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

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())

foods = [list(map(int, input().split())) for i in range(n)]
foods.sort(key=lambda x: (x[0]))
times = [x[0] for x in foods]

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

# i: i 番目のfood
# sum_t: 総時間
for i in range(n):
    for sum_t in range(t + max(times)):
        if sum_t - foods[i][0] >= 0:
            # i 番目のアイテムを選ぶ場合
            dp[i + 1][sum_t] = max(dp[i + 1][sum_t], dp[i][sum_t - foods[i][0]] + foods[i][1])
        # i 番目のアイテムを選ばない場合
        dp[i + 1][sum_t] = max((dp[i + 1][sum_t], dp[i][sum_t]))

print(dp[n][t + max(times) - 9])

Submission status: Wrong Answer

Solution code:
# 2019-11-16 21:01:15(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    N,T = [int(x) for x in sys.stdin.readline().split()]
    time, deliciousness = [], []
    for _ in range(N):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        time.append(a)
        deliciousness.append(b)

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, t = [int(x) for x in input().split()]
xs   = []
ys   = []
dp   = []

for _ in range(0, 6001):
	dp.append([0] * 6001)

for _ in range(0, n):
	x, y = [int(x) for x in input().split()]
	xs.append(x)
	ys.append(y)
	
s = t + max(xs)

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

for i in range(0, n):
	for w in range(0, s + 1):
		if xs[i] <= w:
			dp[i + 1][w] = max(dp[i][w - xs[i]] + ys[i], dp[i][w])
		else:
			dp[i + 1][w] = dp[i][w]
			
print(dp[n][t])

Submission status: Wrong Answer

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


def main():
    N, T = map(int, input().split())
    AB = []
    for _ in range(N):
        a, b = map(int, input().split())
        AB.append([a, b])
    AB.append([0, 0])
    AB.sort(key=lambda x: x[1])
    AB.sort(key=lambda x: x[0])
    ma = AB[-1][1]
    AB.remove(AB[-1])
    dp = [[0]*(T) for _ in range(N)]
    for i in range(len(AB)-1):
        ab = AB[i+1]
        for t in range(T):
            if dp[i][t] + ab[0] >= T:
                dp[i+1][t] = dp[i][t]
            else:
                t1 = min(T-1, t+ab[0])
                dp[i+1][t1] = max(dp[i][t], dp[i][t] + ab[1])
    print(dp[-1][-2] + ma)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def LI(): return map(int,sys.stdin.readline().split())

def main():
    N,T = LI()
    AB = []
    for i in range(N):
        A,B = LI()
        AB.append([A,B])

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

    for i in range(N):
        for j in range(T+3000):
            A,B = AB[i]
            dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][0])
            dp[i+1][j][1] = min(dp[i+1][j][1],dp[i][j][1])
            if j+A<T+3000:
                dp[i+1][j+A][0] = max(dp[i+1][j+A][0],dp[i][j][0]+B)
                dp[i+1][j+A][1] = min(dp[i+1][j+A][1],max(dp[i][j][1],A))
    
    ans = 0
    for i in range(T+3000):
        if i-dp[N][i][1]<T or (dp[N][i][1] == 0 and i==T):
            ans = max(ans,dp[N][i][0])
    return ans


if __name__ == "__main__":
    print(main())

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]:
            if (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]

        else:
            dp[x][y] = dp[x - 1][y]
        
        #print(y, A[x -1 ],y - A[x - 1], dp[x - 1][y - A[x - 1]] + B[x - 1])
        
ans = -1
for d in dp:
    ans = max(ans, max(d))
    #print(d)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

dish.sort()
C = defaultdict(int)
G = defaultdict(lambda: False)
for i in range(N):
    for w in range(1, T + 3000):
        if dish[i][0] <= w:
            C[(i, w)] = max(dish[i][1] + C[(i - 1, w - dish[i][0])], C[(i - 1, w)])
            if dish[i][1] + C[(i - 1, w - dish[i][0])] > C[(i - 1, w)]:
                G[(i, w)] = True
        else:
            C[(i, w)] = C[(i - 1, w)]

ans = 0
for i in range(N):
    ans = max(C[(i, T)], ans)
    if G[(i, T + dish[i][0])]:
        ans = max(C[(i, T + dish[i][0] - 1)], ans)
print(ans)