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: Runtime Error

Solution code:
from numpy import*
(N,T),*L=loadtxt(open(0)).tolist()
d=zeros(1e4,int)
for w,v in sorted(L):t=d[w:T+w];s=d[:T]+v;t[t<s]=s
print(max(d))

Submission status: Runtime Error

Solution code:
from numpy import*
N,T,*L=int64(open(0).read().split())
d=zeros(T*2,int)
for w,v in sorted(zip(*[iter(L)]*2)):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)
print(max(d))

Submission status: Runtime Error

Solution code:
n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
sorted(ab)
dp = [[-1 for i in range(t)] for j in range(n+1)]

def dfs(i,j):
    if dp[i][j] >= 0:
        return dp[i][j]
      
    if i == n:
        res = 0
    elif j == 0:
        res = 0
    elif ab[i][0] >= j:
        res = max(ab[i][1],dfs(i+1,j))
    elif ab[i][0] < j:
        res = max(dfs(i+1,j),dfs(i+1,j-ab[i][0])+ab[i][1])
    dp[i][j] = res
    return res

print(dfs(0,t-1))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
D = []
for i in range(N):
    t, v = map(int, input().split())
    D.append((t, v))

D.sort(key=lambda x:x[0])

DP1 = [[0 for j in range(M)] for i in range(N + 2)]
DP2 = [[0 for j in range(M)] for i in range(N + 2)]

for i in range(1, N + 1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP1[i][j] = max(DP1[i - 1][j], DP1[i - 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP1[i][j] = DP1[i - 1][j]

for i in range(N, 0, -1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP2[i][j] = max(DP2[i + 1][j], DP2[i + 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP2[i][j] = DP2[i + 1][j]

mv = 0
for l in range(N):
    mv = max(DP1[l][M - 1] + DP2[l + 2][l + 1] + D[l][1], mv)

print(mv)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
D = []
for i in range(N):
    t, v = map(int, input().split())
    D.append((t, v))

D.sort(key=lambda x:x[0])

DP1 = [[0 for j in range(M)] for i in range(N + 2)]
DP2 = [[0 for j in range(M)] for i in range(N + 2)]

for i in range(1, N + 1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP1[i][j] = max(DP1[i - 1][j], DP1[i - 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP1[i][j] = DP1[i - 1][j]

for i in range(N, 0, -1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP2[i][j] = max(DP2[i + 1][j], DP2[i + 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP2[i][j] = DP2[i + 1][j]

mv = 0
for l in range(N):
    mv = max(DP1[l][M - 1] + DP2[l][l + 1] + D[l][1], mv)

print(mv)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
D = []
for i in range(N):
    t, v = map(int, input().split())
    D.append((t, v))

D.sort(key=lambda x:x[0])

DP1 = [[0 for j in range(M)] for i in range(N + 2)]
DP2 = [[0 for j in range(M)] for i in range(N + 2)]

for i in range(1, N + 1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP1[i][j] = max(DP1[i - 1][j], DP1[i - 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP1[i][j] = DP1[i - 1][j]

for i in range(N - 1, 0, -1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP2[i][j] = max(DP2[i + 1][j], DP2[i + 1][j - D[i][0]] + D[i][1])
        else:
            DP2[i][j] = DP2[i + 1][j]

mv = 0
for l in range(N):
    mv = max(DP1[l][M - 1] + DP2[l][l + 1] + D[l][1], mv)

print(mv)

Submission status: Runtime Error

Solution code:
n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a_max = max([y[1] for y in x])

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(ai):
        dp[j] = dp_pre[j]
    for j in range(ai, t + ai):
        dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi)
    for j in range(t + ai, t + a_max):
        dp[j] = dp_pre[j]
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))

Submission status: Runtime Error

Solution code:
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N):
AB.sort(key = lambda x:x[0])
dp = [0]*(T+1)
for A,B in AB:  #それぞれの食事について
    for i in range(T-1,-1,-1):
        if A+i>=T:  #時間オーバー
            dp[T] = max(dp[T], dp[i]+B)
        else:   #時間内
            dp[i+A] = max(dp[i+A],dp[i]+B)
#これが成り立つのはAB.sortしているから
print(max(dp))

Submission status: Runtime Error

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

import sys
N,T=map(int, sys.stdin.readline().split())
AB=[ map(int, sys.stdin.readline().split()) for _ in range(N) ]

AB.sort()

N=2005
dp=[ None for _ in range(N) ]
dp[0]=0

for a,b in AB:
    for i in range(N-1,-1,-1):
        if dp[i] is not None and i<T:
                dp[i+a]=max(dp[i+a],dp[i]+b)

print max(dp)

Submission status: Runtime Error

Solution code:
def main():
    N, T = map(int, input().split())
    l = []
    for _ in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    l.sort(key=lambda x: x[0])
    m = [[0] * T for _ in range(N)]
    m[0] = [0 if t < l[0][0] else l[0][1] for t in range(T)]
    for i in range(1, N):
        t, v = l[i]
        for j in range(t):
            m[i][j] = m[i-1][j]
        for j in range(t, T):
            m[i][j] = max(l[i][1] + m[i-1][j-t], m[i-1][j])
    return max(l[i][1] + m[i-1][-1] if i > 0 else l[i][1] for i in range(N))

print(main())

Submission status: Runtime Error

Solution code:
N, T=map(int, input().split())
lst=[list(map(int, input().split())) for _ in range(N)]
MAXN, MAXT=3001, 3001
dp1=[[0 for _ in range(MAXT)] for _ in range(MAXN)]
dp2=[[0 for _ in range(MAXT)] for _ in range(MAXN)]
for i in range(1, N+1):
  A, B=lst[i-1]
  for j in range(1, MAXT):
    dp1[i][j]=max(dp1[i-1][j], dp1[i-1][j-A]+B) if j>=A else dp1[i-1][j]
for i in range(N, 0, -1):
  A, B=lst[i-1]
  for j in range(1, MAXT):
    dp2[i][j]=max(dp2[i+1][j], dp2[i+1][j-A]+B) if j>=A else dp2[i+1][j]
ans=dp1[N-1][T-1]+lst[-1][1]
for i in range(1, N):
  A, B=lst[i-1]
  for j in range(T):
    ans=max(ans, dp1[i-1][j]+dp2[i+1][T-1-j]+B)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n,t=map(int,input().split())
s=[list(map(int,input().split()))for i in range(n)]
a,b=np.zeros((n+1,t+1)),np.zeros((n+1,t+1))
for i in range(n):
    a[i+1]=a[i]
    a[i+1][s[i][0]:]=np.maximum(a[i+1][s[i][0]:],a[i][:t+1-s[i][0]]+s[i][1])
    b[n-i-1]=b[n-i]
    b[n-i-1][s[n-i-1][0]:]=np.maximum(b[n-i-1][s[n-i-1][0]:],b[n-i][:t-s[n-i-1][0]+1]+s[n-i-1][1])
p=0
print(a,b)
for i in range(n):
    for j in range(t):
        p=max(p,a[i][j]+b[i+1][t-1-j]+s[i][1])
print(int(p))

Submission status: Runtime Error

Solution code:
import numpy as np
n,t=map(int,input().split())
s=[list(map(int,input().split()))for i in range(n)]
a,b=np.zeros((n+1,t+1)),np.zeros((n+1,t+1))
for i in range(n):
    a[i+1]=a[i]
    a[i+1][s[i][1]:]=np.maximum(a[i+1][s[i][1]:],a[i][:t+1-s[i][1]]+s[i][0])
    b[n-i-1]=b[n-i]
    b[n-i-1][s[n-i-1][1]:]=np.maximum(b[n-i-1][s[n-i-1][1]:],b[n-i][:t-s[n-i-1][1]+1]+s[n-i-1][0])
p=0
for i in range(n):
    for j in range(t+1):
        p=max(p,a[i][j]+b[i+1][t-j]+s[i][0])
print(int(p))

Submission status: Runtime Error

Solution code:
N,T = input().split()
N,T = int(N),int(T)
import numpy as np



for i in range(N):
    a,b = input().split()
    a,b = int(a),int(b)
    A.append(a)
    B.append(b)
    
ind = np.argsort(np.array(B))

data = np.zeros((N-1,T-1))

for i in range(N-1):
    for t in range(T-1):
        if(i==0):
            num = ind[0]
            if(t>=A[num]):
                data[i][t] = B[num]
        else:
            if(t<A[num]):
                num = ind[i]
                data[i][t] = data[i-1][t]
            else:
                num = ind[i]
                p1 = data[i-1][t]
                p2 = data[i-1][t-A[num]]+B[num]

                max_p = max([p1,p2])

                data[i][t] = max_p
            
print(int(data[N-2][T-2])+B[ind[N-1]])

Submission status: Runtime Error

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

dp=[[0]*(t) for i in range(n)]
x=[[0]*(t) for j in range(n)]
s.sort()


p=s[-1]
del(s[-1])


for i in range(n-1):
    for j in range(t):

        if j-s[i][0]>=0:
            dp[i+1][j]=max(dp[i][j],dp[i][j-s[i][0]]+s[i][1])
            if dp[i+1][j]>dp[i][j]:
                x[i+1][j]=str(x[i][j-s[i][0]])+str(i)
            else:
                x[i+1][j]=x[i][j]

        else:
            dp[i+1][j]=dp[i][j]
            x[i+1][j]=x[i][j]
count=0

v=[]
for i in x[n-1][-1]:
    count+=1
    if count==1:
        continue
    v.append(int(i))


ans=p[1]
k=0
l=len(v)
for i in range(n-1):
    if v[k]==i:
        if k<l-1:
            k+=1
        continue


    ans=max(ans,s[i][1])



print(dp[n-1][-1]+ans)

Submission status: Runtime Error

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

dp=[[0]*(t) for i in range(n)]
x=[[0]*(t) for j in range(n)]
s.sort()


p=s[-1]
del(s[-1])


for i in range(n-1):
    for j in range(t):

        if j-s[i][0]>=0:
            dp[i+1][j]=max(dp[i][j],dp[i][j-s[i][0]]+s[i][1])
            if dp[i+1][j]>dp[i][j]:
                x[i+1][j]=str(x[i][j-s[i][0]])+str(i)
            else:
                x[i+1][j]=x[i][j]

        else:
            dp[i+1][j]=dp[i][j]
            x[i+1][j]=x[i][j]
count=0

v=[]
for i in x[n-1][-1]:
    count+=1
    if count==1:
        continue
    v.append(int(i))


ans=p[1]
for i in range(n-1):
    if len(v)!=0 and v[0]==i:
        del(v[0])
        continue

    ans=max(ans,s[i][1])



print(dp[n-1][-1]+ans)

Submission status: Runtime Error

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

dp=[[0]*(t) for i in range(n)]
x=[[0]*(t) for j in range(n)]
s.sort()


p=s[-1]
del(s[-1])


for i in range(n-1):
    for j in range(t):

        if j-s[i][0]>=0:
            dp[i+1][j]=max(dp[i][j],dp[i][j-s[i][0]]+s[i][1])
            if dp[i+1][j]>dp[i][j]:
                x[i+1][j]=str(x[i][j-s[i][0]])+str(i)
            else:
                x[i+1][j]=x[i][j]

        else:
            dp[i+1][j]=dp[i][j]
            x[i+1][j]=x[i][j]
count=0

v=[]
for i in x[n-1][-1]:
    count+=1
    if count==1:
        continue
    v.append(int(i))


ans=p[1]
for i in range(n-1):
    if v.count(i)==1:
        continue

    ans=max(ans,s[i][1])



print(dp[n-1][-1]+ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n,t=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(n)]

dp=[[0]*(t) for i in range(n)]
x=[[0]*(t) for j in range(n)]
s.sort()


p=s[-1]
del(s[-1])


for i in range(n-1):
    for j in range(t):

        if j-s[i][0]>=0:
            dp[i+1][j]=max(dp[i][j],dp[i][j-s[i][0]]+s[i][1])
            if dp[i+1][j]>dp[i][j]:
                x[i+1][j]=str(x[i][j-s[i][0]])+str(i)
            else:
                x[i+1][j]=x[i][j]

        else:
            dp[i+1][j]=dp[i][j]
            x[i+1][j]=x[i][j]
count=0

v=[]
for i in x[n-1][-1]:
    count+=1
    if count==1:
        continue
    v.append(int(i))


ans=p[1]
for i in range(n-1):
    if v.count(i)==1:
        continue

    ans=max(ans,s[i][1])



print(dp[n-1][-1]+ans)

Submission status: Runtime Error

Solution code:
N,T = input().split()
N,T = int(N),int(T)
import numpy as np
A = []
B = []
#A = [15,20,13,24,18,19,23,18,27,22]
#B = [23,18,17,12,29,27,21,20,15,25]


for i in range(N):
    a,b = input().split()
    a,b = int(a),int(b)
    A.append(a)
    B.append(b)
    
ind = np.argsort(np.array(B))#[::-1]
#print(ind)
def dp(t,i):
    if(i==0):
        if(t-0.5<0):
            return 0
        else:
            return B[ind[0]]
    else:
        if(t-0.5<0):
            return 0#dp(t,i-1)
        else:
            num = ind[i-1]
            p1 = dp(t,i-1)
            p2 = dp(t-A[num],i-1)+B[num]

            return max([p1,p2])
            
print(dp(T,N))

Submission status: Runtime Error

Solution code:
N,T = input().split()
N,T = int(N),int(T)
A = []
B = []
#A = [15,20,13,24,18,19,23,18,27,22]
#B = [23,18,17,12,29,27,21,20,15,25]


for i in range(N):
    a,b = input().split()
    a,b = int(a),int(b)
    A.append(a)
    B.append(b)
    
ind = np.argsort(np.array(B))#[::-1]
def dp(t,i):
    if(i==0):
        if(t-0.5<0):
            return 0
        else:
            return B[ind[0]]
    else:
        if(t-0.5<0):
            return 0#dp(t,i-1)
        else:
            num = ind[i-1]
            p1 = dp(t,i-1)
            p2 = dp(t-A[num],i-1)+B[num]

            return max([p1,p2])
            
print(dp(T,N))

Submission status: Runtime Error

Solution code:
N,T = input().split()
N,T = int(N),int(T)
import numpy as np
A = []
B = []


for i in range(N):
    a,b = input().split()
    a,b = int(a),int(b)
    A.append(a)
    B.append(b)
    
ind = np.argsort(np.array(B))#[::-1]
print(ind)
def dp(t,i):
    if(i==0):
        if(t-0.5<0):
            return 0
        else:
            return B[ind[0]]
    else:
        if(t-0.5<0):
            return dp(t,i-1)
        else:
            num = ind[i-1]
            p1 = dp(t,i-1)
            p2 = dp(t-A[num],i-1)+B[num]

            return max([p1,p2])
            
print(dp(T,N))

Submission status: Runtime Error

Solution code:
N,T = map(int,input().split())
import numpy as np
A = []
B = []
C = []
for i in range(N):
    a,b = map(int,input().split())
    C.append([a,b])
C.sort()
dp = [[0 for i in range(T)]for j in range(N+1)]
for i in range(1,N+1):
    a,b = C[i-1][0],C[i-1][1]
    for t in range(1,T):
        p = dp[i-1][t]
        if t >= a:
            q = dp[i-1][t-a] + b
            dp[i][t] = max(p,q)
        else:
            dp[i][t] = p
M = 0
for i in range(1,N+1):
    M = max(M,C[i-1][1]+dp[i-1][T-1])
print(M)

Submission status: Runtime Error

Solution code:
N,T = map(int,input().split())
import copy
A = []
B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
C = zip(B,A)
C = sorted(C)
B,A = zip(*C)
A = list(A)
B = list(B)
dp = [[0 for i in range(T)]for j in range(N+1)]
dp1 = [[[]for i in range(T)]for j in range(N+1)]
for i in range(1,N+1):
    a,b = A[i-1],B[i-1]
    for t in range(1,T):
        p = dp[i-1][t]
        if t >= a:
            q = dp[i-1][t-a] + b
            if p > q:
                dp[i][t] = p
                dp1[i][t] = copy.deepcopy(dp1[i-1][t])
            elif p < q:
                dp[i][t] = q
                dp1[i][t] = copy.deepcopy(dp1[i-1][t-a])
                dp1[i][t].append(b)
            else:
                dp[i][t] = p
                mp = max(dp1[i-1][t])
                mq = max(dp1[i-1][t-a])
                mq = max(mq,b)
                if mp >= mq:
                    dp1[i][t] = copy.deepcopy(dp1[i-1][t-a])
                    dp1[i][t].append(b)
                else:
                    dp1[i][t] = copy.deepcopy(dp1[i-1][t])
        else:
            dp[i][t] = p
            dp1[i][t] = copy.deepcopy(dp1[i-1][t])
M = 0
for i in range(N):
    b = B[N-1-i]
    if b not in dp1[N][T-1]:
        M = b
        break
print(dp[N][T-1]+M)

Submission status: Runtime Error

Solution code:
import numpy as np
N, T = map(int, input().split())
AB = sorted([list(map(int, input().split())) for _ in range(N)], key=lambda x:x[1])

dp = np.zeros((N+1,T),int)
for i,(A,B) in enumerate(AB): dp[i+1,A:] = np.maximum(dp[i,A:], dp[i,:T-A]+B)
print(dp[-1,-1])

Submission status: Runtime Error

Solution code:
n = int(input())
events = input().split()
sub = []
result = []
a = True
for i in range(n):
        if int(events[i]) not in sub:
                sub.append(int(events[i]))
        else:
                if sum(sub) == 0:
                        result.append(len(sub))
                        sub = []
                        sub.append(events[i])
                else:
                        print(-1)
                        a = False
                        break

if sum(sub) == 0:
    result.append(len(sub))

if sum(result) == n and a == True:
    print(len(result))
    for k in result:
            print(k, end= " ")
else:
    print(-1)

Submission status: Runtime Error

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

n, t = map(int, input().split())
dish = [tuple(map(int, input().split())) for _ in range(n)]

dp = [[0] * (t + 1) for _ in range(n + 1)]
for i in range(n):
    for t0 in range(dish[i][0]):
        dp[i + 1][t0] = dp[i][t0]
    for t0 in range(t):
        t1 = min(t0 + dish[i][0], t)
        dp[i + 1][t1] = max(dp[i + 1][t1], dp[i][t1], dp[i][t0] + dish[i][1])

ans = max(dp[n])
# print(*dp, sep='\n')
# print([dp[i][-1] for i in range(n + 1)])
print(ans)

Submission status: Runtime Error

Solution code:

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

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


AB.sort()

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




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


# 使ってないものを探す

used = [False] * N
j = T-1
#print("j =", j)
for i in reversed(range(N)):
    if dp[i+1][j] == 0:
        break
    #for j in reversed(range(T)):
    # i を使う場合
    #print(i, dp[i][j - AB[i][0]] + AB[i][1] , dp[i][j])
    #print("i, j, dp(i+1,j), dp[i+1][j - AB[i][0]] + AB[i][1] ", i, j, dp[i+1][j], dp[i+1][j - AB[i][0]] + AB[i][1] )
    #print(i,j, AB[i][0])
    if dp[i][j - AB[i][0]] + AB[i][1] == dp[i+1][j]:
        used[i] = True
        j -= AB[i][0]
        #print("j =", j)
    
#print(used)

last = 0
for i in range(N):
    if not used[i]:
        last = max(last, AB[i][1])

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

#print(dp)

Submission status: Runtime Error

Solution code:

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

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


AB.sort()

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




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


# 使ってないものを探す

used = [False] * N
j = T-1
#print("j =", j)
for i in reversed(range(N)):
    if dp[i+1][j] == 0 or dp[i][j] == 0:
        break
    #for j in reversed(range(T)):
    # i を使う場合
    #print(i, dp[i][j - AB[i][0]] + AB[i][1] , dp[i][j])
    #print("i, j, dp(i+1,j), dp[i+1][j - AB[i][0]] + AB[i][1] ", i, j, dp[i+1][j], dp[i+1][j - AB[i][0]] + AB[i][1] )
    #print(i,j, AB[i][0])
    if dp[i][j - AB[i][0]] + AB[i][1] == dp[i+1][j]:
        used[i] = True
        j -= AB[i][0]
        #print("j =", j)
    
#print(used)

last = 0
for i in range(N):
    if not used[i]:
        last = max(last, AB[i][1])

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

#print(dp)

Submission status: Runtime Error

Solution code:

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

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


AB.sort()

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




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


# 使ってないものを探す

used = [False] * N
j = T-1
#print("j =", j)
for i in reversed(range(N)):
    if j < 0 or dp[i+1][j] == 0:
        break
    #for j in reversed(range(T)):
    # i を使う場合
    #print(i, dp[i][j - AB[i][0]] + AB[i][1] , dp[i][j])
    #print("i, j, dp(i+1,j), dp[i+1][j - AB[i][0]] + AB[i][1] ", i, j, dp[i+1][j], dp[i+1][j - AB[i][0]] + AB[i][1] )
    if dp[i][j - AB[i][0]] + AB[i][1] == dp[i+1][j]:
        used[i] = True
        j -= AB[i][0]
        #print("j =", j)
    
#print(used)

last = 0
for i in range(N):
    if not used[i]:
        last = max(last, AB[i][1])

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

#print(dp)

Submission status: Runtime Error

Solution code:

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

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


AB.sort()

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




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


# 使ってないものを探す

used = [False] * N
j = T-1
#print("j =", j)
for i in reversed(range(N)):
    if j < 0 or dp[i+1][j] == 0:
        break
    #for j in reversed(range(T)):
    # i を使う場合
    #print(i, dp[i][j - AB[i][0]] + AB[i][1] , dp[i][j])
    #print("i, j, dp(i+1,j), dp[i+1][j - AB[i][0]] + AB[i][1] ", i, j, dp[i+1][j], dp[i+1][j - AB[i][0]] + AB[i][1] )
    if dp[i][j - AB[i][0]] + AB[i][1] == dp[i+1][j]:
        used[i] = True
        j -= AB[i][0]
        #print("j =", j)
    
#print(used)

last = 0
for i in range(N):
    if not used[i]:
        last = max(last, AB[i][1])

print(dp[N][-1] + last)

#print(dp)

Submission status: Runtime Error

Solution code:
n,t=map(int,input().split())
c=[list(map(int,input().split())) for i in range(n)]
d=list()
s1=0
s2=0
d=[[c[i][1]/c[i][0],c[i][0],c[i][1]] for i in range(n)]
d=sort(d,key=lambda x:(x[0],x[1]))
for i in range(n):
  s2+=d[i][2]
  s1+=d[i][1]
  if s1>=t-0.5:
    print(s2)
    exit()

Submission status: Runtime Error

Solution code:
n,t=map(int,input().split())
c=[list(map(int,input().split())) for i in range(n)]
d=list()
s1=0
s2=0
d=[[c[i][1]/c[i][0],c[i][0],c[i][1]] for i in range(n)]
d=sort(d,key=lamda x:(x[0],x[1]))
for i in range(n):
  s2+=d[i][2]
  s1+=d[i][1]
  if s1>=t-0.5:
    print(s2)
    exit()

Submission status: Runtime Error

Solution code:

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

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


AB.sort()

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




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


# 使ってないものを探す

used = [False] * N
j = T-1
#print("j =", j)
for i in reversed(range(N)):
    if dp[i+1][j] == 0:
        break
    #for j in reversed(range(T)):
    # i を使う場合
    #print(i, dp[i][j - AB[i][0]] + AB[i][1] , dp[i][j])
    #print("i, j, dp(i+1,j), dp[i+1][j - AB[i][0]] + AB[i][1] ", i, j, dp[i+1][j], dp[i+1][j - AB[i][0]] + AB[i][1] )
    if dp[i][j - AB[i][0]] + AB[i][1] == dp[i+1][j]:
        used[i] = True
        j -= AB[i][0]
        #print("j =", j)
    
#print(used)

last = 0
for i in range(N):
    if not used[i]:
        last = max(last, AB[i][1])

print(dp[N][-1] + last)

#print(dp)

Submission status: Runtime Error

Solution code:
n,t=map(int,input().split())
times=[]
values=[]
for i in range(n):
  time,value=map(int,input().split())
  times.append(time)
  values.append(value)
  
def knapsack(i,w):
  if i >= n:
    return 0
  elif w < 0:
    return 0
  else:
    p1 = knapsack(i+1, w)
    p2 = knapsack(i+1, w - times[i]) + values[i]
    return max(p1,p2)

  
print(knapsack(0,t-1))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
//constexpr ll MOD = (998244353);
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
void dump(){cout<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cout<<h<<", ";dump(forward<Ts>(t)...);}
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}

void solve1();void solve2();
int main(){
  solve1();
  return 0;
}

void solve1(){

  ll n,t;
  cin>>n>>t;
  using pll = pair<ll,ll>;
  vector<pll> a(n);
  cin>>a;

  sort(a.begin(),a.end());
  auto dp = make_vector<ll>(6010,6010);
  rep(i,n){
    rep(j,t){
      if(i>0)chmax(dp[i][j],dp[i-1][j]);
      if(j>0)chmax(dp[i][j],dp[i][j-1]);
      if(i>0&&j>0)chmax(dp[i][j],dp[i-1][j-1]);
      chmax(dp[i+1][j+a[i].first],dp[i][j]+a[i].second);
    }
  }

  ll ans = 0;
  for(auto y:dp)for(auto x:y)chmax(ans,x);

  cout<<(ans)<<endl;

}

Submission status: Runtime Error

Solution code:
n,t=map(int,input().split())
times=[]
values=[]
for i in range(n):
  time,value=map(int,input().split())
  times.append(time)
  values.append(value)
  
def knapsack(i,w):
  if i >= n:
    return 0
  elif w < 0:
    return 0
  else:
    p1 = knapsack(i+1, w)
    p2 = knapsack(i+1, w - times[i]) + values[i]
    if p1>p2:
        return p1
    else:
        return p2

  
print(knapsack(0,t))

Submission status: Runtime Error

Solution code:
n,t=map(int,input().split())
c=[list(map(int,input().split())) for i in range(n)]
d=list()
s1=0
s2=0
for i in range(n):
  d.append([c[i][1]/c[i][0],c[i][0]])
while s1=<t-0.5:
  s2+=c[d.index(d.pop(max(d)))][1]
  s1+=c[d.index(d.pop(max(d)))][0]
print(s2)

Submission status: Runtime Error

Solution code:
#
import sys
import math
import numpy as np
import itertools
# いくつか入力
n,t = (int(i) for i in input().split())

# n行の複数列ある数値を二次元配列へ。x[n][?]
ab = [[int(i) for i in input().split()] for i in range(n)]
# 0:weight 1:value

ab = sorted(ab,key=lambda x: x[1])

dp = np.zeros((n+1,t),int)
dp2 = np.zeros((n+1,t),int)

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

answer =0
for i in range(n+1):
    for w in range(t):
        answer = np.max(answer, dp2[i][w])
print(answer)

#print(dp2)

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


# 枝刈り探索（分枝限定法）

class Knapsack:
	def __init__(self, VW):
		self.VW = VW
		self.VW.sort(key=lambda vw: vw[0] / vw[1], reverse=True)
		self.n = len(VW)
	
	def solve(self, capacity, ok=0):
		self.ok = ok
		self.capacity = capacity
		return self._dfs(0, 0, 0)
	
	def _dfs(self, i, v_now, w_now):
		if i == self.n:
			self.ok = max(self.ok, v_now)
			return v_now
		ng, f = self._solve_relaxation(i, self.capacity - w_now)
		ng += v_now
		if f:
			self.ok = max(self.ok, ng)
			return ng
		if ng < self.ok:
			return -float("inf")
		res = -float("inf")
		v, w = self.VW[i]
		if w_now + w <= self.capacity:
			res = max(res, self._dfs(i + 1, v_now + v, w_now + w))
		res = max(res, self._dfs(i + 1, v_now, w_now))
		return res
	
	def _solve_relaxation(self, i, capacity):
		res = 0
		f = True
		for v, w in self.VW[i:]:
			if capacity == 0:
				break
			if w <= capacity:
				capacity -= w
				res += v
			else:
				f = False
				res += v * (capacity / w)
				break
		return res, f


def main():
	n, t = map(int, input().split())
	AB=[]
	for _ in range(n):
		A,B = map(int, input().split())
		AB.append((B,A))
	AB.sort()
	knapsack = Knapsack(AB[:-1])
	ans = knapsack.solve(t-1) + AB[-1][0]
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
import copy
def knapsack(N,W,weight,value):
  dp=[[999999999999 for i in range(W+1)] for j in range(N+1)]
  dp2=[[0 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:
        if dp[i][w-weight[i]]+value[i]>dp[i][w]:
          dp[i+1][w]=dp[i][w-weight[i]]+value[i]
          dp2[i+1][w]=w-weight[i]
        else:
          dp[i+1][w]=dp[i][w]
          dp2[i+1][w]=w
        
      else:
        dp[i+1][w]=dp[i][w]
        dp2[i+1][w]=w
  
  l=[]
  x=W
  for i in range(n-1,-1,-1):
    if dp2[i+1][x]==x-weight[i]:
      l.append(i)
    x=dp2[i+1][x]
  return dp[N][W],l
 
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
a=[];b=[]
for i in range(n):
  a.append(l[i][0])
  b.append(l[i][1])
z=knapsack(n,t-1,a,b)
L=[0]*n
for i in range(len(z[1])):
  L[z[1][i]]=1
M=[]
for i in range(n):
  if L[i]==0:
    M.append(l[i][1])
print(z[0]+max(M))