Problem:


Score : 500 points

Problem Statement

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

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

The restaurant has the following rules:

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

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

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

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

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

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

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

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

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

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

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

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

Sample Input 4

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

Sample Output 4

145



Sample input:
2 60
10 10
100 100


Sample output:
110

Submission status: Wrong Answer

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

dp = {0:0}
for a,b in AB:
    dp2 = {0:0}
    for t,m in dp.items():
        if t >= T:
            continue
        if t in dp2:
            dp2[t] = max(dp2[t], m)
        else:
            dp2[t] = m
        if t+a in dp2:
            dp2[t+a] = max(dp2[t+a], m+b)
        else:
            dp2[t+a] = m+b
    dp = dp2
print(max(dp.values()))

Submission status: Wrong Answer

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)]
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
            dp[i][t] = max(p,q)
        else:
            dp[i][t] = p
M = 0
for i in range(1,N+1):
    M = max(M,B[i-1]+dp[i-1][T-1])
print(M)

Submission status: Wrong Answer

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)]
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
            dp[i][t] = max(p,q)
        else:
            dp[i][t] = p
M = 0
for i in range(1,N+1):
    M = max(M,B[i-1]+dp[i-1][T-1])
print(M)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)


def solve(N: int, T: int, A: "List[int]", B: "List[int]"):
    max_t = T + max(A)
    dp = [-1] * (max_t + 1)
    dp[0] = 0
    for i in range(N):
        for t in range(T - 1, -1, -1):
            if dp[t] >= 0:
                dp[t + A[i]] = max(dp[t + A[i]], dp[t] + B[i])
    ret = 0
    for v in dp:
        ret = max(ret, v)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, T, A, B)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
dp = [[0] * 3000 for i in range(N+1)]
answer = 0
for i in range(1, N-1):
    a, b = AB[i]
    for j in range(a, 3000):
        dp[i][j] = max(dp[i-1][j], dp[i-1][j-a] + b)
    result = dp[i][T-1] + sorted(AB[i+1:], key=lambda x:x[1])[-1][1]
    if result > answer:
        answer = result
print(answer)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, t = map(int, input().split())
a = []
b = []
for _ in range(n):
  a_tmp, b_tmp = map(int, input().split())
  a.append(a_tmp)
  b.append(b_tmp)
dp1 = np.zeros((n, t), dtype=np.int)
dp2 = np.zeros((n, t), dtype=np.int)
for i in range(n - 1):
  dp1[i+1, :a[i]] = dp1[i, :a[i]]
  dp1[i+1, a[i]:] = np.maximum(dp1[i, a[i]:], dp1[i, :-a[i]] + b[i])
  dp2[-i-2, :a[-i-1]] = dp2[-i-1, :a[-i-1]]
  dp2[-i-2, a[-i-1]:] = np.maximum(
    dp2[-i-1, a[-i-1]:], dp2[-i-1, :-a[-i-1]] + b[-i-1])
  
print(np.max(dp1[:, -1] + dp2[:, -1]) + max(b))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC145 E

import sys
import math
import copy
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

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

Submission status: Wrong Answer

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,T),int)
for i,(A,B) in enumerate(AB[:-1]):
    for j in range(A,T):
      dp[i+1,j] = max(dp[i,j], dp[i,j-A]+B)
print(dp[-1,-1]+AB[-1][1])

Submission status: Wrong Answer

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(t):
        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: Wrong Answer

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(t):
        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][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: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC145 E

import sys
import math
import copy
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,t = LI()
ab = [LI() for _ in range(n)]
ab.sort(key = lambda x:x[0])
dp = [0]*(t)
dplist = [defaultdict(int) for _ in range(t)]
for i in range(n):
    a,b = ab[i]
    for j in range(t)[::-1]:
        if j - a < 0:
            continue
        if dp[j] < dp[j-a]+b:
            dplist[j] = dplist[j-a].copy()
            dplist[j][i] = 1
        dp[j] = max(dp[j],dp[j-a]+b)
f = dplist[-1]
ans = dp[-1]
for i in range(n):
    if not f[i]:
        ans += ab[i][1]
        break
print(ans)

Submission status: Wrong Answer

Solution code:
dishes = input().split()
dish_utility = []
dish_utility_all = []
times = []
deliciousness = []
t = 0
total_utility = 0
j = 0

for i in range(int(dishes[0])):
    dish = input().split()
    dish_utility.append(int(dish[1]) / int(dish[0]))
    dish_utility.append(int(dish[0]))
    times.append(int(dish[0]))
    dish_utility.append(int(dish[1]))
    deliciousness.append(int(dish[1]))
    dish_utility_all.append(dish_utility)
    dish_utility = []

dish_utility_all.sort(reverse=True)
time = int(dishes[1]) - 5
best = dish_utility_all.pop(0)

times.sort(reverse=True)
longest = times.pop(0)
if sum(times) < time:
    print(sum(deliciousness))
else:
    while (t + dish_utility_all[j][1]) < time:
        total_utility += dish_utility_all[j][2]
        t += dish_utility_all[j][1]
        j += 1

    total_utility += best[2]
    print(total_utility)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC145 E

import sys
import math
import copy
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,t = LI()
ab = [LI() for _ in range(n)]
ab.sort(key = lambda x:x[1],reverse=True)
dp = [0]*(t)
dplist = [defaultdict(int) for _ in range(t)]
for i in range(n):
    a,b = ab[i]
    for j in range(t)[::-1]:
        if j - a < 0:
            continue
        if dp[j] < dp[j-a]+b:
            dplist[j] = dplist[j-a].copy()
            dplist[j][i] = 1
        dp[j] = max(dp[j],dp[j-a]+b)
f = dplist[-1]
ans = dp[-1]
for i in range(n):
    if not f[i]:
        ans += ab[i][1]
        break
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#入力
N, T = [int(x)for x in input().split()]
ab=[[0]*2 for _ in range(N)]
for i in range(N): #N=3000
    ab[i]=[int(x) for x in input().split()]

ab.sort(key=lambda j:j[1]) #長さでソートする
T=T-1 #最後の注文は別枠にする

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

for i in range(N-1): #N=3000 9*10^6 なんでTLEするんだ？
    ai=ab[i][0]
    bi=ab[i][1]
    dpi=dp[i]
    dpi1=dp[i-1]
    for j in range(T):#T=3000 内部計算量は最大でも定数で3
        if (not(j < ai) and dp[i - 1][j]<  dp[i - 1][j - ab[i][0]] + ab[i][1]):
            dpi[j] = dpi1[j - ai] + bi
        else:
            dpi[j] = dpi1[j]

print(dp[N-2][T-1] + ab[N-1][1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC145 E

import sys
import math
import copy
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,t = LI()
ab = [LI() for _ in range(n)]
dp = [0]*(t)
dplist = [defaultdict(int) for _ in range(t)]
for i in range(n):
    a,b = ab[i]
    for j in range(t)[::-1]:
        if j - a < 0:
            continue
        if dp[j] < dp[j-a]+b:
            dplist[j] = dplist[j-a].copy()
            dplist[j][i] = 1
        dp[j] = max(dp[j],dp[j-a]+b)
f = dplist[-1]
tmp = 0
for i in range(n):
    if not f[i]:
        tmp = max(tmp,ab[i][1])
ans = dp[-1] + tmp
print(ans)

Submission status: Wrong Answer

Solution code:
#入力
N, T = [int(x)for x in input().split()]
ab=[[0]*2 for _ in range(N)]
for i in range(N): #N=3000
    ab[i]=[int(x) for x in input().split()]

ab.sort(key=lambda j:j[1]) #長さでソートする
T=T-1 #最後の注文は別枠にする

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

for i in range(N-1): #N=3000 9*10^6 なんでTLEするんだ？
    ai=ab[i][0]
    bi=ab[i][1]
    dpi=dp[i]
    dpi1=dp[i-1]
    for j in range(T):#T=3000 内部計算量は最大でも定数で3
        if j < ai:
            dpi[j] = dpi1[j]
        else:
            if(dp[i - 1][j]<  dp[i - 1][j - ab[i][0]] + ab[i][1]):
                dpi[j] =  dpi1[j - ai] + bi
            else:
                dpi[j] = dpi1[j]

print(dp[N-2][T-1] + ab[N-1][1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC145 E

import sys
import math
import copy
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,t = LI()
a,b = [],[]
for _ in range(n):
    x,y = LI()
    a.append(x)
    b.append(y)
dp = [[0]*(t+1) for _ in range(n+1)]
dplist = [[defaultdict(int) for _ in range(t+1)] for _ in range(2)]
for i in range(n):
    for j in range(t+1):
        if j < a[i]:
            dp[i+1][j] = dp[i][j]
            dplist[(i+1)%2][j] = dplist[i%2][j].copy()
        else:
            if dp[i][j] < dp[i][j-a[i]] + b[i]:
                dplist[(i+1)%2][j] = dplist[i%2][j-a[i]].copy()
                dplist[(i+1)%2][j][i] = 1
            else:
                dplist[(i+1)%2][j] = dplist[i%2][j].copy()
            dp[i+1][j] = max(dp[i][j],dp[i][j-a[i]]+b[i])
f = dplist[(i+1)%2][-1]
tmp = -1
for i in range(n):
    if f[i] == 0:
        tmp = max(tmp,b[i])
if tmp < 0:
    print(dp[-1][t])
    quit()
time = 0
for i,j in f.items():
    if j:
        time += a[i]
if time == t:
    m = inf
    for i,j in f.items():
        if j:
            m = min(m,a[i])
    if m > tmp:
        print(dp[-1][t])
    else:
        print(dp[-1][t]-m + tmp)
else:
    ans = dp[-1][t] + tmp
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC145 E

import sys
import math
import copy
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,t = LI()
a,b = [],[]
for _ in range(n):
    x,y = LI()
    a.append(x)
    b.append(y)
dp = [[0]*(t+1) for _ in range(n+1)]
dplist = [[defaultdict(int) for _ in range(t+1)] for _ in range(2)]
for i in range(n):
    for j in range(t+1):
        if j < a[i]:
            dp[i+1][j] = dp[i][j]
            dplist[(i+1)%2][j] = dplist[i%2][j].copy()
        else:
            if dp[i][j] < dp[i][j-a[i]] + b[i]:
                dplist[(i+1)%2][j] = copy.deepcopy(dplist[i%2][j-a[i]])
                dplist[(i+1)%2][j][i] = 1
            else:
                dplist[(i+1)%2][j] = dplist[i%2][j].copy()
            dp[i+1][j] = max(dp[i][j],dp[i][j-a[i]]+b[i])
#print(dp[-1][t])
f = dplist[(i+1)%2][-1]
#print(f)
tmp = -1
for i in range(n):
    if f[i] == 0:
        tmp = max(tmp,b[i])
if tmp < 0:
    print(dp[-1][t])
    quit()
time = 0
for i,j in f.items():
    if j:
        time += a[i]
if time == t:
    print(dp[-1][t])
else:
    ans = dp[-1][t] + tmp
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n, t = ril()
ls = rils(n)

dp = [[0] * t for _ in range(n + 1)]
for i in range(n + 1):
    for t0 in range(t):
        a, b = ls[i - 1]
        if i == 0 or t0 == 0:
            continue
        elif a <= t0:
            dp[i][t0] = max(dp[i - 1][t0], dp[i - 1][t0 - a] + b)
        else:
            dp[i][t0] = dp[i - 1][t0]

res = set()
val = dp[n][t - 1]
t0 = t - 1
for i in reversed(range(n)):
    a, b = ls[i]
    if val <= 0:
        break
    if val == dp[i][t0]:
        continue
    else:
        res.add(i)
        val -= b
        t0 -= a

m = 0
for i, (_, v) in enumerate(ls):
    if i not in res:
        m = max(m, v)
print(dp[n][t - 1] + m)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC145 E

import sys
import math
import copy
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,t = LI()
a,b = [],[]
for _ in range(n):
    x,y = LI()
    a.append(x)
    b.append(y)
dp = [[0]*(t+1) for _ in range(n+1)]
dplist = [[defaultdict(int) for _ in range(t+1)] for _ in range(n+1)]
for i in range(n):
    for j in range(t+1):
        if j < a[i]:
            dp[i+1][j] = dp[i][j]
            dplist[i+1][j] = copy.deepcopy(dplist[i][j])
        else:
            if dp[i][j] < dp[i][j-a[i]] + b[i]:
                dplist[i+1][j] = copy.deepcopy(dplist[i][j-a[i]])
                dplist[i+1][j][i] = 1
            else:
                dplist[i+1][j] = copy.deepcopy(dplist[i][j])
            dp[i+1][j] = max(dp[i][j],dp[i][j-a[i]]+b[i])

f = dplist[-1][-1]
tmp = -1
for i in range(n):
    if f[i] == 0:
        tmp = max(tmp,b[i])
if tmp < 0:
    print(dp[-1][t])
    quit()
time = 0
for i,j in f.items():
    if j:
        time += a[i]
if time == t:
    print(dp[-1][t])
else:
    ans = dp[-1][t] + tmp
    print(ans)

Submission status: Wrong Answer

Solution code:
N,T=list(map(int,input().split()))
AB=[list(map(int,input().split())) for i in range(N)]
AB.sort()
A=[AB[i][0] for i in range(N)]
B=[AB[i][1] for i in range(N)]
Amax=A.pop()
Bmax=B.pop()

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

Submission status: Wrong Answer

Solution code:
# dp[i][t_sum]: i番目までの品物で時間がt_sumを超えないように選んだときの、満足度の総和の最大値
N, T = map(int, input().split())
items = [tuple(map(int, input().split())) for i in range(N)]

dp1 = [[0 for t_sum in range(T+1)] for i in range(N+2)]
dp2 = [[0 for t_sum in range(T+1)] for i in range(N+2)]

for i in range(N):
    for t_sum in range(items[i][0], T+1):

        dp1[i+1][t_sum] = max(dp1[i+1][t_sum], dp1[i]
                              [t_sum-items[i][0]]+items[i][1])
        dp1[i+1][t_sum] = max(dp1[i+1][t_sum], dp1[i][t_sum])

    for t_sum in range(items[N-1-i][0], T+1):

        dp2[N-1-i][t_sum] = max(dp2[N-1-i][t_sum], dp2[N-i]
                                [t_sum-items[N-i-1][0]]+items[N-i-1][1])
        dp2[N-1-i][t_sum] = max(dp2[N-1-i][t_sum], dp2[N-i][t_sum])


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

Submission status: Wrong Answer

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 or j - AB[i][0] < 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: 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.sort()
    ma = AB[-1]
    AB.remove(ma)
#    print(AB)
    dp = [[0]*T for _ in range(N)]
    for i, (a, b) in enumerate(AB):
        for t in range(T):
            if t - a >= 0:
                dp[i+1][t] = max(dp[i][t], dp[i][t-a] + b)
            else:
                dp[i+1][t] = dp[i][t]

#    [print(d) for d in dp]
    print(dp[-1][-1] + ma[1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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=sorted(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: 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.sort(key=lambda x: x[1])
    ma = AB[-1]
    AB.remove(AB[-1])
#    print(AB)
    dp = [[0]*T for _ in range(N)]
    for i, (a, b) in enumerate(AB):
        for t in range(T):
            if t - a >= 0:
                dp[i+1][t] = max(dp[i][t], dp[i][t-a] + b)
            else:
                dp[i+1][t] = dp[i][t]

#    [print(d) for d in dp]
    print(dp[-1][-1] + ma[1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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()
for i in range(n):
  s2+=d[i][2]
  s1+=d[i][1]
  if s1>=t-0.5:
    print(s2)
    exit()

Submission status: Wrong Answer

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()
for i in range(n):
  s2+=d[i][2]
  s1+=d[i][1]
  if s1>t-0.5:
    print(s2)
    exit()

Submission status: Wrong Answer

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][1],c[i][0]] for i in range(n)]
d.sort()
for i in range(n):
  s2+=d[i][1]
  s1+=d[i][2]
  if s1>t-0.5:
    print(s2)
    exit()

Submission status: Wrong Answer

Solution code:
import numpy as np

N,T = map(int, input().split())
item = [None]*N
for i in range(N):
  w,v = map(int,input().split())
  item[i] = [v,w]
item.sort(key=lambda x:x[0])

dp = np.zeros(T, dtype='int64')
ans=0
for i in range(N):
    v,w=item[i]
    ans=max(ans,dp.max()+v)
    dp[w:] = np.maximum(dp[w:], dp[:-w] + v)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, t = list(map(int, readline().split()))
    ab = []
    for i in range(n):
        a, b = list(map(int, readline().split()))
        ab.append([a, b])
    ab.sort(key=lambda x: x[1])
    ls = [0] * (t + 1)
    at = 0
    bb = False
    for i in range(n-1):
        a, b = ab[i]
        for j in range(t - a, -1, -1):
            bb = True
            ls[a + j] = max(ls[a + j], ls[j] + b)
    if bb:
        print(ls[-1] + ab[-1][1])
    else:
        if ab[-1][0] >= t:
            print(ab[-1][1])
        else:
            print(ab[-1][1] + ab[-2][1])


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#入力
N, T = [int(x)for x in input().split()]
ab=[[0]*2 for _ in range(N)]
for i in range(N): #N=3000
    ab[i]=[int(x) for x in input().split()]

ab.sort(key=lambda j:j[1]) #長さでソートする
T=T-1 #最後の注文は別枠にする

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

for i in range(N-1): #N=3000 9*10^6 なんでTLEするんだ？
    ai=ab[i][0]
    bi=ab[i][1]
    for j in range(T):#T=3000 内部計算量は最大でも定数で3
        if j < ai:
            dp[i][j] = dp[i - 1][j]
        else:
            if(dp[i - 1][j]<  dp[i - 1][j - ab[i][0]] + ab[i][1]):
                dp[i][j] =  dp[i - 1][j - ai] + bi
            else:
                dp[i][j] = dp[i - 1][j]

print(dp[N-2][T-1] + ab[N-1][1])

Submission status: Wrong Answer

Solution code:
import numpy as np

N, T = map(int, input().split())
item=[None]*N
for i in range(N):
  w,v=map(int,input().split())
  item[i]=[v,w]
item.sort(key=lambda x:x[0])
def knapsack(n, w):
    # dp[i][j] : i番目の品物で重さj以下の価値の最大値
    dp = np.zeros(T, dtype='int64')
    # forは関数の中で回す。
    ans=0
    for i in range(N):
        v,w=item[i]
        # i+1番目でwより小さい場合は、i番目と変更なし
        ans=max(ans,dp.max()+v)
        dp[w:] = np.maximum(dp[w:], dp[:-w] + v)
        # print(dp)
    return ans
print(knapsack(N, T-1))

Submission status: Wrong Answer

Solution code:
import numpy as np

N, T = map(int, input().split())
item=[None]*N
for i in range(N):
  w,v=map(int,input().split())
  item[i]=[v,w]
item.sort(key=lambda x:x[0])
def knapsack(n, w):
    # dp[i][j] : i番目の品物で重さj以下の価値の最大値
    dp = np.zeros([N+1, T], dtype='int64')
    # forは関数の中で回す。
    ans=0
    for i in range(N):
        v,w=item[i]
        # i+1番目でwより小さい場合は、i番目と変更なし
        dp[i+1][:w] = dp[i][:w]
        ans=max(ans,dp[i].max()+v)
        dp[i+1][w:] = np.maximum(dp[i][w:], dp[i][:-w] + v)
        # print(dp)
    return ans
print(knapsack(N, T-1))

Submission status: Wrong Answer

Solution code:
import numpy as np

N, T = map(int, input().split())
item=[]
for i in range(N):
  w,v=map(int,input().split())
  item.append([v,w])
item.sort(key=lambda x:x[0])
def knapsack(n, w):
    # dp[i][j] : i番目の品物で重さj以下の価値の最大値
    dp = np.zeros([N+1, T+1], dtype='int64')
    # forは関数の中で回す。
    ans=0
    for i in range(N):
        v,w=item[i]
        # i+1番目でwより小さい場合は、i番目と変更なし
        dp[i+1][:w] = dp[i][:w]
        ans=max(ans,dp[i].max()+v)
        dp[i+1][w:] = np.maximum(dp[i][w:], dp[i][:-w] + v)
        # print(dp)
    return ans
print(knapsack(N, T-1))

Submission status: Wrong Answer

Solution code:
import numpy as np

N, T = map(int, input().split())
def knapsack(n, w):
    # dp[i][j] : i番目の品物で重さj以下の価値の最大値
    dp = np.zeros([N+1, T+1], dtype='int64')
    # forは関数の中で回す。
    ans=0
    for i in range(N):
        w, v = map(int, input().split())
        # i+1番目でwより小さい場合は、i番目と変更なし
        dp[i+1][:w] = dp[i][:w]
        ans=max(ans,dp[i].max()+v)
        dp[i+1][w:] = np.maximum(dp[i][w:], dp[i][:-w] + v)
        # print(dp)
    return ans
print(knapsack(N, T-1))

Submission status: Wrong Answer

Solution code:
n,t = [int(_) for _ in input().split()]
a = []
for i in range(n):
    a.append([int(_) for _ in input().split()])
dp = [[0]*(t+1) for i in range(n)]
for j in range(t+1):
    c = a[0][0]
    v = a[0][1]
    if j >= c:
        dp[0][j] = v
dp[0][-1] = v
for i in range(1, n):
    for j in range(1, t+1):
        if j == t:
            c = 1
        else:
            c = a[i][0]
        v = a[i][1]
        if j - c < 0:
            dp[i][j] = dp[i - 1][j]
        else:
            dp[i][j] = max(dp[i - 1][j - c] + v, dp[i-1][j])

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

Submission status: Wrong Answer

Solution code:
n,t = [int(_) for _ in input().split()]
a = []
for i in range(n):
    a.append([int(_) for _ in input().split()])
dp = [[0]*(t+1) for i in range(n)]
for j in range(t+1):
    c = a[0][0]
    v = a[0][1]
    if j >= c:
        dp[0][j] = v
dp[0][-1] = v
for i in range(1, n):
    for j in range(1, t+1):
        if j == t:
            c = 1
        else:
            c = a[i][0]
        v = a[i][1]
        if j - c < 0:
            dp[i][j] = dp[i - 1][j]
        else:
            dp[i][j] = max(dp[i - 1][j - c] + v, dp[i-1][j])
print(dp)
print(dp[n-1][t])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500000)
n, m = map(int, input().split())
time_point = [0]*n
for i in range(n):
    time, point = map(int, input().split())
    time_point[i] = [point, time]
time_point.sort()
baglist = [0]*(m+1)
for i in range(n-1):
    weight, value = time_point[i][1], time_point[i][0]
    for j in range(m,-1,-1):
        if (baglist[j] or j == 0) and j+weight <= m-1:
            baglist[j+weight] = max([baglist[j+weight], baglist[j]+value])
print(max(baglist)+time_point[-1][0])

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, t = list(map(int, readline().split()))
    ab = []
    for i in range(n):
        a, b = list(map(int, readline().split()))
        ab.append([a, b])
    ab.sort(key=lambda x: x[1])
    ls = [0] * t
    at = 0
    for i in range(n-1):
        a, b = ab[i]
        done = False
        for j in range(t, -1, -1):
            if a + j <= t - 1:
                ls[a + j] = max(ls[a + j], ls[j] + b)
    print(ls[-1] + ab[-1][1])


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
time_point = [0]*n
for i in range(n):
    time, point = map(int, input().split())
    time_point[i] = [point, time]
time_point.sort()
baglist = [0]*(m+1)
for i in range(n-1):
    weight, value = time_point[i][1], time_point[i][0]
    for j in range(m,-1,-1):
        if (baglist[j] or j == 0) and j+weight <= m:
            baglist[j+weight] = max([baglist[j+weight], baglist[j]+value])
print(max(baglist)+time_point[-1][0])