Problem:


Score : 500 points

Problem Statement

Takahashi is at an all-you-can-eat restaurant.

The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.

The restaurant has the following rules:

You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.

You cannot order the same kind of dish more than once.

Until you finish eating the dish already served, you cannot order a new dish.

After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.

Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.

What is the maximum possible happiness achieved by making optimal choices?

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.

Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

We cannot order three dishes, in whatever order we place them.

Sample Input 4

10 100
15 23
20 18
13 17
24 12
18 29
19 27
23 21
18 20
27 15
22 25

Sample Output 4

145



Sample input:
2 60
10 10
100 100


Sample output:
110

Submission status: Wrong Answer

Solution code:
import numpy as np

n, t = map(int,input().split())
dp2=[[0 for i in range(t+1)] for j in range(n+1)]
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][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
        dp2[i+1][w] = w - weight[i]
      else: #入る可能性はない
        dp[i+1][w]=dp[i][w]
        dp2[i+1][w] = w
  return dp[N][W]

ab = np.array([list(map(int,input().split())) for i in range(n)])
a = ab[:,0]
b = ab[:,1]
eat = np.ones(n)
for i in range(n-1, 0):
    if dp2[i+1][t] == t-a[i]:
        eat[i] = 0
print(int(np.max(ab.T * eat)+knapsack(n,t-1,a,b)))

Submission status: Wrong Answer

Solution code:
import copy
from operator import itemgetter
N,T = map(int, input().split())
A = [[0] * 3 for i in range(N)]
for i in range(N):
  A[i][0], A[i][1] = map(int, input().split())

for i in range(N):
  A[i][2] = A[i][1] / A[i][0]


B = sorted(A, reverse=True, key=itemgetter(2))
C = copy.deepcopy(B)
passTime = 0
Ans = 0
Best = 0

for i in range(N):
  Ans = 0
  Ans = Ans + B[i][1]
  B[i][0] = 0
  B[i][1] = 0
  B[i][2] = 0

  for j in range(N):
    if passTime + B[j][0] <= T - 0.5:
      passTime = passTime + B[j][0]
      Ans = Ans + B[j][1]

  if Ans > Best:
    Best = Ans
  
  B[i][0] = C[i][0]
  B[i][1] = C[i][1]
  B[i][2] = C[i][2]
  
print(Best)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
import copy
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])
ans=DP[-1][-1]
d=[c[-1]]
for i in range(a-1):
  d.append(c[i])
c=copy.copy(d)
c.sort(reverse=True)
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(max(ans,DP[-1][-1]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, t = map(int, input().split())
a = []
b = []
for i in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)
dp = [[0 for j in range(t+1)] for i in range(n+1)]

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

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
d=[None]*N
for i in range(N):
  a,b=map(int,input().split())
  d[i]=[a,b]

dp=[-1]*(3000*2+5)
dp[0]=0
for i in range(len(d)):
  for j in range(T,-1,-1):
    if dp[j]!=-1:
      if dp[j]+d[i][1]>dp[j+d[i][0]]:
        dp[j+d[i][0]]=dp[j]+d[i][1]
      
print(max(dp))

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
        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),ans)
    else:
        ans=max(solve(c[:i]+c[i+1:],T),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l=sorted(l, key=lambda x: x[1])
l=l[::-1]
#print(l)
dp=[[0]*(t+1) for i in range(n+1)]
q=[0]
p=[]
for i in range(1,n+1):
    for j in range(t+1):
        if j-l[i-1][0]>0:
            dp[i][j]=max(dp[i-1][j-l[i-1][0]]+l[i-1][1],dp[i-1][j])
        else:
            dp[i][j]=dp[i-1][j]
    if dp[i][-1]==dp[i-1][-1]:
        #print(l[i])
        q.append(l[i-1][1])
    else:
        p.append(l[i-1][0])
q.sort()
#print(dp)
#print(q,p)
if sum(p)<=t-0.5:
    print(dp[-1][-1]+q[-1])
else:
    print(dp[-1][-1])

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


items = sorted(items, key=lambda x: x[0])

if sum([v[0] for v in items[:-1]]) < T:
    print(sum([v[1] for v in items]))
    exit()
items = sorted(items, 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[N-1])

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:
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:
I = [int(_) for _ in open(0).read().split()]
N, T = I[:2]
A, B = I[2::2], I[3::2]
dp = [-float('inf')] * 6001
dp[0] = 0
for a, b in zip(A, B):
    dp_new = dp[:]
    for k in range(T):
        if dp[k] == -float('inf'):
            continue
        dp_new[k + a] = max(dp[k + a], dp[k] + b)
    dp = dp_new
print(max(dp))

Submission status: Wrong Answer

Solution code:
import copy
from operator import itemgetter
N,T = map(int, input().split())
A = [[0] * 3 for i in range(N)]
for i in range(N):
  A[i][0], A[i][1] = map(int, input().split())

for i in range(N):
  A[i][2] = A[i][1] / A[i][0]


B = sorted(A, reverse=True, key=itemgetter(2))
C = copy.deepcopy(B)
passTime = 0
NoChoice = [[0] * 3 for i in range(N)]
Ans = 0
Best = 0

for i in range(N):
  Ans = 0
  Ans = Ans + B[i][1]
  B[i][0] = 0
  B[i][1] = 0
  B[i][2] = 0

  for j in range(N):
    if passTime + B[j][0] <= T - 0.5:
      passTime = passTime + B[j][0]
      Ans = Ans + B[j][1]
    else:
      NoChoice[j][0] = B[j][0]
      NoChoice[j][1] = B[j][1]
      NoChoice[j][2] = B[j][2]

  if Ans > Best:
    Best = Ans
  
  B[i][0] = C[i][0]
  B[i][1] = C[i][1]
  B[i][2] = C[i][2]
  
print(Best)

Submission status: Wrong Answer

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

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

info = sorted(info,key = lambda x:x[2],reverse = True)
#print(info)

Mpt = [0] * N
Mpt[-1] = info[-1][1]
for i in range(2,N):
    Mpt[-i] = max(Mpt[-i+1],info[-i][1])
    #N-i番目以降でpt最高のもの
can = []

pt = 0
time = 0
Mpt_ = 0#使ってないものでpt最高のもの
for i in range(N):
    if time + info[i][0] >= T:
        can.append(pt + max(Mpt[i],Mpt_))
        Mpt_ = max(Mpt_,info[i][1])
    else:
        pt += info[i][1]
        time += info[i][0]
    #print(can)
can.append(pt+Mpt_)
print(max(can))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
weight = []
value = []

dp = [[0] * (6010) for i in range(3010)]

for i in range(N):
    w, v = map(int, input().split())
    weight.append(w)
    value.append(v)
#配る
addi = set([])
for i, (ww, vv) in enumerate(zip(weight, value)):
    for w in range(T):            
        dp[i+1][w] = max(dp[i+1][w], dp[i][w])
        if w+ww >= T:
            addi.add(w+ww)
        dp[i+1][w+ww] = max(dp[i][w+ww], dp[i][w] + vv)
        #else: dp[i+1][w] = dp[i][w]

    for w in addi:
        dp[i+1][w] = max(dp[i+1][w], dp[i][w])


print(max(dp[N]))
"""
for i, v in enumerate(dp[N][:200]):
    print("{}:{}, ".format(i, v), end="")
"""

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]*3010 for _ in range(3010)]
l=[LI() for _ in range(n)]
l=sorted(l,key=lambda x:x[1])

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

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

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

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()
dp=[[-1]*3010 for _ in range(3010)]
l=[LI() for _ in range(n)]
l=sorted(l,key=lambda x:x[1])

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

  res=0
  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:
    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:
def knapsack_DP(N, W, weight, value):
    dp = [0 for _ in range(W + 1)] 

    for i in range(N):
        dp_tmp = [] 
        for w in range(W + 1):
            if (w >= weight[i]): 
                dp_next = max(dp[w-weight[i]] + value[i], dp[w]) 
            else:
                dp_next = dp[w] 
            dp_tmp.append(dp_next)
        dp = dp_tmp 
    return dp[-1]

if __name__=="__main__":
    N, W = map(int, input().split())
    W=W-1
    weight = []
    value = []
    for i in range(N):
        w, v= map(int, input().split())
        weight.append(w)
        value.append(v)
    wmax=0
    vmax=0
    maxindex=0
    for i in range(N):
        if weight[i]>=wmax:
            if value[i]>vmax:
                maxindex=i
    w_m=weight.pop(maxindex)
    v_m=value.pop(maxindex)
    print(knapsack_DP(N-1, W, weight, value)+v_m)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
dish = sorted([list(map(int, input().split()))
               for i in range(n)], reverse=True, key=lambda x: x[1])
ans = 0
cnt = 0
res = []
for d in dish:
    if cnt + d[0] < t:
        ans += d[1]
        cnt += d[0]
    else:
        res.append(d[1])

if res != []:
    ans += max(res)
print(ans)

Submission status: Wrong Answer

Solution code:
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)

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

for i in range(N):
    for j in range(T):
        finish_time = ft[i][j]
        if j >= A[i]:
            finish_time = max(finish_time, ft[i][j-A[i]] + B[i])
        ft[i+1][j] = finish_time

        ot[i+1][j] = max(ot[i][j], ft[i][j] + B[i])

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

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l=sorted(l, key=lambda x: x[1])
l=l[::-1]
#print(l)
dp=[[0]*(t+1) for i in range(n+1)]
q=[0]
p=[]
for i in range(1,n+1):
    for j in range(t+1):
        if j-l[i-1][0]>0:
            dp[i][j]=max(dp[i-1][j-l[i-1][0]]+l[i-1][1],dp[i-1][j])
        else:
            dp[i][j]=dp[i-1][j]
    if dp[i][-1]==dp[i-1][-1]:
        #print(l[i])
        q.append(l[i-1][1])
    else:
        p.append(l[i-1][0])
q.sort()
#print(dp)
#print(q,p)
if sum(p)<=t-0.5:
    print(dp[-1][-2]+q[-1])
else:
    print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inp():
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl():
    return list(map(int, input().split()))
def inpls():
    return list(input().split())
def inplm(n):
    return list(int(input()) for _ in range(n))
def inpll(n):
    return sorted([list(map(int, input().split())) for _ in range(n)])

def Knapsack2(W,w,v):
    n=len(w)
    v_max=sum(v)+10
    dp=[0]+[10**8 for _ in range(v_max+1)]
    over=[0 for _ in range(v_max+2)]
    for i in range(n):
        for j in range(v_max,v[i]-1,-1):
            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+2):
        if dp[i]-over[i]<=(W-0.5):
            ans=i
    print(ans)
    for i in range(v_max):
        print(i,dp[i],over[i])

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:
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]=dp[j]+v[i]
            dp2[j]=max(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:
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,20000] 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],B))
    
    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=list(map(int,input().split()))
AB=[list(map(int,input().split())) for i in range(N)]
A=[AB[i][0] for i in range(N)]
B=[AB[i][1] for i in range(N)]

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

if T >= sum(A):
    print(max(max(dp)))
else:
    import numpy as np
    dparr=np.array(dp)
    def argmax_ndim(arg_array):
        return np.unravel_index(arg_array.argmax(), arg_array.shape)
    dpmax_index = list(argmax_ndim(dparr))
    if dpmax_index[0] == N-1:
        print(max(max(dp)))
    else:
        print(max(max(dp)) + max(B[dpmax_index[0]+1:]))

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
listf= []
maxsat = 0

for i in range(n):
    tmp = [int(j) for j in input().split()]
    if tmp[1]>maxsat:
        maxsat = tmp[1]
    listf.append(tmp)
    
if t == 1:
    ans = maxsat
else:
    roadf = {}
    roadset = {}
    roadf[0] = 0
    roadset[0] = set()
    
    for i in range(1,t):
        roadf[i] = roadf[i-1]
        roadset[i] = roadset[i-1]
        for j in range(n):
            time = listf[j][0]
            if time<=i:
                ind = i-time
                tmpoldset = set(roadset[ind])
                if j not in tmpoldset:
                    tmp = roadf[ind]+listf[j][1]
                    if tmp > roadf[i]:
                        roadf[i] = tmp
                        roadset[i] = tmpoldset
                        roadset[i].add(j)
    print(roadf)
    print(roadset)
    
    lastf =  roadf[t-1]
    lastset =  roadset[t-1]
    if len(lastset) == n:
        ans = lastf
    else:
        ans = lastf
        tmpmax = 0
        for i in range(n):
            if i not in lastset:
                tmpf = listf[i][1]
                if tmpf>tmpmax:
                    tmpmax = tmpf
                    
        ans += tmpmax
        
print(ans)

Submission status: Wrong Answer

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

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

n,t = m()

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

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

ma.sort(reverse = True)


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

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


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

print(ans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())

a = [0]*3333
b = [0]*3333
dp = [0]*6666


for i in range(1, n+1):
	a[i], b[i] = map(int, input().split())

for i in range(1, n+1):
	for j in range(6000, a[i]-1, -1):
		if ((a[i] <= j) and ((j-a[i])<t)):
			dp[j] = max(dp[j], dp[j - a[i]] + b[i])

#print(dp)
print(max(dp))

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()
dp=[[-1]*3010 for _ in range(3010)]
l=[LI() for _ in range(n)]

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

  res=0
  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:
    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:
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]
res = dp[n-1][w-1]+wv[n-1][1]
tmp = wv[n-1][1]
dp = [[0 for i in range(w+1)] for j in range(n+1)]
wv.sort()
ww = [0] * n
vv = [0] * n
for i in range(n):
    ww[i],vv[i] = wv[i][0], wv[i][1]

if sum(ww[:n-1]) < w:
    print(sum(vv))
    quit()
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(max(res,dp[n-1][w-1]+tmp))

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([-10**10] * 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:
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[1]//x[0], reverse=True)

def dfs(i, sum, time, remain):
    global answer
    if i == min(N, 100):
        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:
n, t = [int(x) for x in input().split()]
xs   = []
ys   = []
dp   = []

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

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

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

for i in range(0, n):
	for w in range(0, t + 1):
		if xs[i] <= w or w == t - 1:
			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:
x,y=map(int,input().split())
l=[]
l0=[]
for __ in range(x):
    a,b=map(int,input().split())
    l.append(a)
    l0.append(b)
lol=[]    
for __ in range(x+1):
    lol.append([0]*(y+1))
for i in range(1,x+1):
    for j in range(1,y):
        if l[i-1]<=j:
            lol[i][j]=max(lol[i-1][j],l0[i-1]+lol[i-1][j-l[i-1]+1])
        else:
            lol[i][j]=max(lol[i-1][j],l0[i-1])
print(lol[x][y-1])

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+1)] for i in range(N+1)] # i番目までの料理でj分後（<=T分後）までに得られる満足度の最大値（単純dp）

# 単純dp
for i in range(N):
    for j in range(T+1):
        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])

ans1=dp[N-1][T-1]+x[-1][1]
ans2=dp[N][T]

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [[0, 0, 0] for i in range(N)]
maxA, maxB = 0, 0
for i in range(N):
  A, B = map(int, input().split())
  x = B / A
  if B >= maxB:
    maxB = B
    maxA = A
  AB[i][0], AB[i][1], AB[i][2] = A, B, x
  
AB = sorted(AB, key = lambda x:x[2], reverse = True)

#print(AB)
ans = maxB
x = []
for i in range(N):
  A, B = AB[i][0], AB[i][1]
  if (maxA == A) and (maxB == B):
    continue
  else:
    if T > A:
      ans += B
      T -= A
      x.append([A, B])
      
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
weight = []
value = []

dp = [[0] * (6010) for i in range(3010)]

for i in range(N):
    w, v = map(int, input().split())
    weight.append(w)
    value.append(v)
#配る
for i, (ww, vv) in enumerate(zip(weight, value)):
    for w in range(T):            
        dp[i+1][w] = max(dp[i+1][w], dp[i][w])
        dp[i+1][w+ww] = max(dp[i][w+ww], dp[i][w] + vv)
        #else: dp[i+1][w] = dp[i][w]


print(max(dp[N]))
"""
for i, v in enumerate(dp[N][:200]):
    print("{}:{}, ".format(i, v), end="")
"""

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
d=[None]*N
for i in range(N):
  a,b=map(int,input().split())
  d[i]=[a,b]

dp=[-1]*(3000*2)
dp[0]=0
for i in range(len(d)):
  for j in range(T-1,-1,-1):
    if dp[j]!=-1:
      if dp[j]+d[i][1]>dp[j+d[i][0]]:
        dp[j+d[i][0]]=dp[j]+d[i][1]
      
print(max(dp))

Submission status: Wrong Answer

Solution code:
N,T=[int(s) for s in input().split()]
ls=[[int(s) for s in input().split()] for i in range(N)]
M=max(ls, key=lambda x: x[0])
M=M[0]
dp=[[0,0,i] for i in range(T+M)]
for i in range(N):
  a,b=ls[i]
  for t in range(T):
    s=T-t-1
    x=dp[s][0]+b
    if dp[s+a][0]<x:
      dp[s+a][0]=x
      dp[s+a][1]=max([a,dp[s][1]])
result=[e[0] for e in dp if e[2]-e[1]<T]
#print(dp)
print(max(result))

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l=sorted(l, key=lambda x: x[1])
l=l[::-1]
#print(l)
dp=[[0]*(t+1) for i in range(n+1)]
q=[0]
for i in range(1,n+1):
    for j in range(t+1):
        if j-l[i-1][0]>=0:
            dp[i][j]=max(dp[i-1][j-l[i-1][0]]+l[i-1][1],dp[i-1][j])
        else:
            dp[i][j]=dp[i-1][j]
    if dp[i][-1]==dp[i-1][-1]:
        #print(l[i])
        q.append(l[i-1][1])
q.sort()
#print(dp)
#print(q)
print(dp[-1][-1]+q[-1])

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
l=[]
l0=[]
for __ in range(x):
    a,b=map(int,input().split())
    l.append(a)
    l0.append(b)
lol=[]    
for __ in range(x+1):
    lol.append([0]*(y+1))
for i in range(1,x+1):
    for j in range(1,y+1):
        if l[i-1]<=j:
            lol[i][j]=max(lol[i-1][j],l0[i-1]+lol[i-1][j-l[i-1]+1])
        else:
            lol[i][j]=max(lol[i-1][j],l0[i-1])
print(lol[x][y])

Submission status: Wrong Answer

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

d = {0: 0}
for a, b in ab:
    da = {}
    for k in d:
        pb = d[k]
        if t - k > 0:
            oldval = d.get(k + a)
            if oldval is None:
                da[k + a] = pb + b
            else:
                da[k + a] = max(oldval, pb + b)
    d.update(da)
print(max(d.values()))

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

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