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 sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()

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


    P.sort()
    dp = [[0] * 6010 for _ in range(N+1)]

    for i in range(N):
        for j in range(T):
            a,b = P[i]
            dp[i+1][j+a] = max(dp[i][j+a],dp[i][j]+b)

    ans = max(dp[N])

    # print(dp[N])
    print(ans)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
list=[]
for i in range(N):
  a,b=map(int,input().split())
  list.append((a,b))
list.sort()
A=[]
B=[]
for i in range(len(list)):
  A.append(list[i][0])
  B.append(list[i][1])
c=max(B)
d=B.index(c)
B.pop(d)
A.pop(d)
DP=[[0 for i in range(T)]for j in range(N-1)]
for i in range(A[0],T):
  DP[0][i]=B[0]

for i in range(N-1):
  for j in range(T):
    if i+1<N-1 and j+A[i+1]<T:
      DP[i+1][j+A[i+1]]=DP[i][j]+B[i+1]
                                    

print(DP[N-2][T-1]+c)

Submission status: Wrong Answer

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,t = map(int,ipt().split())
    dp = [[0,0] for i in range(t)]
    for _ in range(n):
        a,b = map(int,ipt().split())
        for i in dp:
            if i[1] < i[0]+b:
                i[1] = i[0]+b
        for i in range(t-1,a-1,-1):
            di = dp[i][0]
            dpi = dp[i-a][0]+b
            if di < dpi:
                dp[i][0] = dpi

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

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

dp = [[-1] * (T + 1) for i in range(N + 1)]
dp[0][0] = 0

for i in range(N):
    for j in range(T + 1):
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
    
        if dp[i][j] >= 0 and j < T:
            dp[i + 1][min(T, j + X[i][0])] = max(dp[i + 1][min(T, j + X[i][0])],
                                                 dp[i][j] + X[i][1])

print(max(dp[-1]))

Submission status: Wrong Answer

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

dp = [[-1] * (T + 1) for i in range(N + 1)]
dp[0][0] = 0

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

print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)


class Bisect:
    def __init__(self, func):
        self.__func = func

    def bisect_left(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if self.__func(mid) < x:
                lo = mid+1
            else:
                hi = mid
        return lo

    def bisect_right(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if x < self.__func(mid):
                hi = mid
            else:
                lo = mid+1
        return lo


@mt
def slv(N, T, AB):
    memo = [-1] * (T + max(a for a, b in AB))

    memo[0] = 0
    for a, b in AB:
        nm = memo[:]
        for t in range(T):
            if memo[t] == -1:
                continue
            nm[t+a] = max(nm[t+a], memo[t]+b)
        memo = nm
    # print(memo)
    return max(memo)


def main():
    N, T = read_int_n()
    AB = [read_int_n() for _ in range(N)]
    print(slv(N, T, AB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)


class Bisect:
    def __init__(self, func):
        self.__func = func

    def bisect_left(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if self.__func(mid) < x:
                lo = mid+1
            else:
                hi = mid
        return lo

    def bisect_right(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if x < self.__func(mid):
                hi = mid
            else:
                lo = mid+1
        return lo


@mt
def slv(N, T, AB):
    memo = [-1] * (T + max(a for a, b in AB))

    memo[1] = 0
    for a, b in AB:
        nm = memo[:]
        for t in range(T):
            if memo[t] == -1:
                continue
            nm[t+a] = max(nm[t+a], memo[t]+b)
        memo = nm
    # print(memo)
    return max(memo)


def main():
    N, T = read_int_n()
    AB = [read_int_n() for _ in range(N)]
    print(slv(N, T, AB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)


class Bisect:
    def __init__(self, func):
        self.__func = func

    def bisect_left(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if self.__func(mid) < x:
                lo = mid+1
            else:
                hi = mid
        return lo

    def bisect_right(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if x < self.__func(mid):
                hi = mid
            else:
                lo = mid+1
        return lo


@mt
def slv(N, T, AB):
    memo = [-1] * (T + max(a for a, b in AB)+1)

    memo[0] = 0
    for a, b in AB:
        nm = memo[:]
        for t in range(min(T, len(memo)-a)):
            if memo[t] == -1:
                continue
            nm[t+a] = max(nm[t+a], memo[t]+b)
        memo = nm
    return max(memo)


def main():
    N, T = read_int_n()
    AB = [read_int_n() for _ in range(N)]
    print(slv(N, T, AB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

dp = np.zeros((N + 1, T), dtype=int)
for i, (a, b) in enumerate(C):
    dp[i + 1] = dp[i]
    dp[i + 1, a:] = np.maximum(dp[i + 1, a:], dp[i, :-a] + b)

cur = T - 1
selected = set()
for i in range(N - 1, -1, -1):
    if dp[i + 1, cur] != dp[i, cur]:
        selected.add(i)
        cur -= C[i][0]
last = max((C[i][1] for i in range(N) if i not in selected), default=0)
print(dp[N, T - 1] + last)

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(n)]
dp=[0]*(t+max(l)[0])
for a,b in l:
  for i in range(0,t+a)[::-1]: 
    if i-a>=0:
      dp[i]=max(dp[i],dp[i-a]+b)
print(max(dp[t:]))

Submission status: Wrong Answer

Solution code:
import sys
import math
import bisect
def input():
    return sys.stdin.readline()[:-1]
n,t=map(int,input().split())
ab = []
for i in range(n):
    ab.append(list(map(int,input().split())))

dp = [[0 for i in range(t)]for i in range(n+1)]
ab = sorted(ab,key=lambda x:x[1])
for i in range(1,n+1):
    for j in range(t):
        if j-ab[i-1][0]>=0:
            dp[i][j] += max(dp[i-1][j-ab[i-1][0]] + ab[i-1][1],dp[i-1][j])
        else:
            dp[i][j] += dp[i-1][j]

cho = [False for i in range(n)]
now = dp[-1][-1]
#print(*dp,sep="\n")
while now!=0:
    if dp[i][j]!= dp[i-1][j]:
        j-=ab[i-1][0]
        cho[i-1]=True
        i-=1
        now = dp[i][j]
    else:
        i-=1
#print(ab)
ama = [ab[i][1] for i in range(n) if cho[i]==False]
#print(ama)
if len(ama)==0:
    print(dp[-1][-1])
else:
    print(dp[-1][-1]+ama[-1])

Submission status: Wrong Answer

Solution code:
N,T,*ABf = map(int, open(0).read().split())
AB = [ABf[i:i+2] for i in range(0, len(ABf), 2)]
dp = [[0] * 2 for _ in range(6000)]
filled = set([0])
for x in AB:
    temp = set()
    change = []
    for y in filled:
        if y + x[0] <= (T-1) + max(dp[y][1],x[0]):
            if dp[y+x[0]][0] <= dp[y][0] + x[1]:
                change.append((y,y+x[0],dp[y][0]+x[1],x[0]))
    for y in change:
        if dp[y[1]][0] == y[2]:
            dp[y[1]][1] = max(dp[y[1]][1],dp[y[0]][1],y[3])
        else:    
            dp[y[1]][0] = y[2]
            dp[y[1]][1] = max(dp[y[0]][1],dp[y[0]][1],y[3])
            temp.add(y[0]+x[0])
    filled = filled | temp
print(max(dp)[0])

Submission status: Wrong Answer

Solution code:
N,T,*ABf = map(int, open(0).read().split())
AB = [ABf[i:i+2] for i in range(0, len(ABf), 2)]
dp = [[0] * 2 for _ in range(6000)]
filled = set([0])
for x in AB:
    temp = set()
    change = []
    for y in filled:
        if y + x[0] <= (T-1) + max(dp[y][1],x[0]):
            if dp[y+x[0]][0] <= dp[y][0] + x[1]:
                change.append((y,y+x[0],dp[y][0]+x[1],x[0]))
                temp.add(y+x[0])
    for y in change:
        if dp[y[1]][0] == y[2]:
            dp[y[1]][1] = max(dp[y[1]][1],max(dp[y[0]][1],y[3]))
        else:    
            dp[y[1]][0] = y[2]
            dp[y[1]][1] = max(dp[y[0]][1],y[3])
    filled = filled | temp
print(max(dp)[0])

Submission status: Wrong Answer

Solution code:
N,T,*ABf = map(int, open(0).read().split())
AB = [ABf[i:i+2] for i in range(0, len(ABf), 2)]
dp = [[0] * 2 for _ in range(6000)]
filled = set([0])
for x in AB:
    temp = set()
    change = []
    for y in filled:
        if y + x[0] <= (T-1) + max(dp[y][1],x[0]):
            if dp[y+x[0]][0] <= dp[y][0]+x[1]:
                change.append((y,y+x[0],dp[y][0]+x[1],x[0]))
                temp.add(y+x[0])
    for y in change:
        if dp[y[1]][0] == y[2]:
            dp[y[1]][1] = min(dp[y[1]][1],max(dp[y[0]][1],y[3]))
        else:    
            dp[y[1]][0] = y[2]
            dp[y[1]][1] = max(dp[y[0]][1],y[3])
    filled = filled | temp
print(max(dp)[0])

Submission status: Wrong Answer

Solution code:
N,T,*ABf = map(int, open(0).read().split())
AB = [ABf[i:i+2] for i in range(0, len(ABf), 2)]
dp = [[0] * 2 for _ in range(6000)]
filled = set([0])
for x in AB:
    temp = set()
    change = []
    for y in filled:
        if y + x[0] <= (T-1) + max(dp[y][1],x[0]):
            if dp[y+x[0]][0] <= dp[y][0]+x[1]:
                change.append((y,y+x[0],dp[y][0]+x[1],x[0]))
                temp.add(y+x[0])
    for y in change:
        dp[y[1]][0] = y[2]
        dp[y[1]][1] = max(dp[y[0]][1],y[3])
    filled = filled | temp
print(max(dp)[0])

Submission status: Wrong Answer

Solution code:
N,T,*ABf = map(int, open(0).read().split())
AB = [ABf[i:i+2] for i in range(0, len(ABf), 2)]
dp = [[0] * 2 for _ in range(6000)]
filled = set([0])
for x in AB:
    temp = set()
    change = []
    for y in filled:
        if y + x[0] <= (T-1) + max(dp[y][1],x[0]):
            if dp[y+x[0]][0] < dp[y][0]+x[1]:
                change.append((y,y+x[0],dp[y][0]+x[1],x[0]))
                temp.add(y+x[0])
    for y in change:
        dp[y[1]][0] = y[2]
        dp[y[1]][1] = max(dp[y[0]][1],y[3])
    filled = filled | temp
print(max(dp)[0])

Submission status: Wrong Answer

Solution code:
import numpy as np

N,T,*ABf = map(int, open(0).read().split())
AB = np.array(ABf).reshape(N,2).tolist()
dp = [[0] * 2 for _ in range(6000)]
filled = set([0])
for x in AB:
    temp = set()
    change = []
    for y in filled:
        if y + x[0] <= (T-1) + max(dp[y][1],x[0]):
            if dp[y+x[0]][0] < dp[y][0]+x[1]:
                change.append((y,y+x[0],dp[y][0]+x[1],x[0]))
                temp.add(y+x[0])
    for y in change:
        dp[y[1]][0] = y[2]
        dp[y[1]][1] = max(dp[y[0]][1],y[3])
    filled = filled | temp
print(max(dp)[0])

Submission status: Wrong Answer

Solution code:
import numpy as np

N,T,*ABf = map(int, open(0).read().split())
AB = np.array(ABf).reshape(N,2).tolist()
dp = [[0] * 2 for _ in range(6000)]
filled = set([0])
for x in AB:
    temp = set()
    change = []
    for y in filled:
        if y <= T - 1:
            if y + x[0] <= (T-1) + max(dp[y][1],x[0]):
                if dp[y+x[0]][0] < dp[y][0]+x[1]:
                    change.append((y,y+x[0],dp[y][0]+x[1],x[0]))
                    temp.add(y+x[0])
    for y in change:
        dp[y[1]][0] = y[2]
        dp[y[1]][1] = max(dp[y[0]][1],y[3])
    filled = filled | temp
print(max(dp)[0])

Submission status: Wrong Answer

Solution code:
INF = 10 ** 7
N,T = map(int,input().split())
foods = []
for _ in range(N):
    foods.append(list(map(int,input().split())))

foods.sort()
dp = [[[-INF, -INF] for i in range(6000)] for j in range(N+1)]
dp[0][0] = [0, 0]
for i in range(N):
    for j in range(T):
        if dp[i][j][0] != -INF:
            if dp[i+1][j][0] < dp[i][j][0] or dp[i+1][j][0] == dp[i][j][0] and dp[i+1][j][1] > dp[i][j][1]:
                dp[i+1][j] = dp[i][j]
            if dp[i+1][j+foods[i][0]][0] < dp[i][j][0] + foods[i][1] or dp[i+1][j+foods[i][0]][0] == dp[i][j][0] + foods[i][1] and dp[i+1][j][1] > dp[i][j][1]:
                dp[i+1][j+foods[i][0]] = [dp[i][j][0] + foods[i][1], j]

ans = 0
for i in range(6000):
    if dp[N][i][1] < T:
        ans = max(ans, dp[N][i][0])
print(ans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
#print(ab)
ab.sort(key=lambda x: x[0])
last = ab.pop()
"""
idx = 0
for i in reversed(range(n)):
    if ab[i][1] == ab[-1][1]:
        idx = i
ab2 = sorted(ab[idx-1:], key=lambda x: x[0])
last = ab2.pop()
ab = ab2 + ab[:idx-1]
ab.sort(key=lambda x: x[0])
"""
#print(ab)
#print(last)
dp = [0] * t
for ai, bi in ab:
    for ti in reversed(range(ai, t)):
        dp[ti] = max(dp[ti], dp[ti - ai] + bi)
#import numpy as np
#print(np.array(dp))
print(dp[t-1] + last[1])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
#print(ab)
ab.sort(key=lambda x: x[1])
last = ab.pop()
"""
idx = 0
for i in reversed(range(n)):
    if ab[i][1] == ab[-1][1]:
        idx = i
ab2 = sorted(ab[idx-1:], key=lambda x: x[0])
last = ab2.pop()
ab = ab2 + ab[:idx-1]
ab.sort(key=lambda x: x[0])
"""
#print(ab)
print(last)
dp = [0] * t
for ai, bi in ab:
    for ti in reversed(range(ai, t)):
        dp[ti] = max(dp[ti], dp[ti - ai] + bi)
#import numpy as np
#print(np.array(dp))
print(dp[t-1] + last[1])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
#print(ab)
ab.sort(key=lambda x: x[1])
idx = 0
for i in reversed(range(n)):
    if ab[i][1] == ab[-1][1]:
        idx = i
ab2 = sorted(ab[idx-1:], key=lambda x: x[0])
last = ab2.pop()
ab = ab2 + ab[:idx-1]
ab.sort(key=lambda x: x[0])
#print(ab)
#print(last)
dp = [0] * t
for ai, bi in ab:
    for ti in reversed(range(ai, t)):
        dp[ti] = max(dp[ti], dp[ti - ai] + bi)
#import numpy as np
#print(np.array(dp))
print(dp[t-1] + last[1])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
#print(ab)
ab.sort(key=lambda x: x[1])
idx = 0
for i in reversed(range(n)):
    if ab[i][1] == ab[-1][1]:
        idx = i
ab2 = sorted(ab[idx-1:], key=lambda x: x[0])
last = ab2.pop()
ab = ab2 + ab[:idx-1]
#ab.sort(key=lambda x: x[0])
#print(ab)
#print(last)
dp = [0] * t
for ai, bi in ab:
    for ti in reversed(range(ai, t)):
        dp[ti] = max(dp[ti], dp[ti - ai] + bi)
#import numpy as np
#print(np.array(dp))
print(dp[t-1] + last[1])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
#print(ab)
ab.sort(key=lambda x: x[0])
ab.sort(key=lambda x: x[1])
last = ab.pop()
dp = [0] * t
for ai, bi in ab:
    for ti in reversed(range(ai, t)):
        dp[ti] = max(dp[ti], dp[ti - ai] + bi)
#import numpy as np
#print(np.array(dp))
print(dp[t-1] + last[1])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
#print(ab)
ab.sort(key=lambda x: x[1])
last = ab.pop()
dp = [0] * t
for ai, bi in ab:
    for ti in reversed(range(ai, t)):
        dp[ti] = max(dp[ti], dp[ti - ai] + bi)
#import numpy as np
#print(np.array(dp))
print(dp[t-1] + last[1])

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc145/tasks/abc145_e
# なんとなくナップサック問題っぽい
# A分以内に食べきる事のできる最大の美味しさは？ならすぐにできる。
# 問題は最後のA分は必要ないこと

# →i番目の料理を最後に食べたときに、それ以外の料理でT分以内に食べることのできる美味しさの最大は？
# O(n^3) ∵ 各iについて(3000) × dp(3000*3000)
# →TLEしてしまいそう...


# 普通にナップサックして解いてから。もしiを最後に食べてたらの処理をする？
# ナップサック復元して、まだ食べてないもののなかから美味しさが最大のものを食べればよい！

'''
dp[i][j] ... 最大の美味しさの総和。[,i)の料理まで考慮したとき。j分以内に食べ終わることができる。
'''

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_a_int(): return int(read())


def read_ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


# default import
from itertools import product, permutations, combinations


# ナップサック復元して、まだ食べてないもののなかから美味しさが最大のものを食べればよい！
# ただし注意として、なるべくA(時間)が小さいものから先に食べて置くことを検討しないとと行けない。∵

N, T = read_ints()
A, B = read_col(N)
tmp = list(zip(A, B))
tmp.sort(reverse=True)
A, B = map(list, zip(*tmp))
# print(A, B)

'''
dp[i][j] ... 最大の美味しさの総和。[,i)の料理まで考慮したとき。j分以内に食べ終わることができる。
'''

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

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

# dp復元しながら食べた料理の美味しさを0に書き換える(あとでmaxを取るときに無視するため)
i = N
j = T - 1
while i > 0:
    if dp[i][j] == dp[i - 1][j]:  # 上から来たものだったらi-1番目のものは使ってない
        pass
    else:  # 斜めから来てたらi-1番目のものを使っている
        B[i - 1] = 0  # 使ったものは美味しさを0にセットしておく
        j -= A[i - 1]
    i -= 1

print(dp[-1][-1] + max(B))

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc145/tasks/abc145_e
# なんとなくナップサック問題っぽい
# A分以内に食べきる事のできる最大の美味しさは？ならすぐにできる。
# 問題は最後のA分は必要ないこと

# →i番目の料理を最後に食べたときに、それ以外の料理でT分以内に食べることのできる美味しさの最大は？
# O(n^3) ∵ 各iについて(3000) × dp(3000*3000)
# →TLEしてしまいそう...


# 普通にナップサックして解いてから。もしiを最後に食べてたらの処理をする？
# ナップサック復元して、まだ食べてないもののなかから美味しさが最大のものを食べればよい！

'''
dp[i][j] ... 最大の美味しさの総和。[,i)の料理まで考慮したとき。j分以内に食べ終わることができる。
'''

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_a_int(): return int(read())


def read_ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


# default import
from itertools import product, permutations, combinations


# ナップサック復元して、まだ食べてないもののなかから美味しさが最大のものを食べればよい！
# ただし注意として、なるべくA(時間)が小さいものから先に食べて置くことを検討しないとと行けない。∵

N, T = read_ints()
A, B = read_col(N)
tmp = list(zip(A, B))
tmp.sort()
A, B = map(list, zip(*tmp))
# print(A, B)

'''
dp[i][j] ... 最大の美味しさの総和。[,i)の料理まで考慮したとき。j分以内に食べ終わることができる。
'''

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

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

# dp復元しながら食べた料理の美味しさを0に書き換える(あとでmaxを取るときに無視するため)
i = N
j = T - 1
while i > 0:
    if dp[i][j] == dp[i - 1][j]:  # 上から来たものだったらi-1番目のものは使ってない
        pass
    else:  # 斜めから来てたらi-1番目のものを使っている
        B[i - 1] = 0  # 使ったものは美味しさを0にセットしておく
        j -= A[i - 1]
    i -= 1

print(dp[-1][-1] + max(B))

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc145/tasks/abc145_e
# なんとなくナップサック問題っぽい
# A分以内に食べきる事のできる最大の美味しさは？ならすぐにできる。
# 問題は最後のA分は必要ないこと

# →i番目の料理を最後に食べたときに、それ以外の料理でT分以内に食べることのできる美味しさの最大は？
# O(n^3) ∵ 各iについて(3000) × dp(3000*3000)
# →TLEしてしまいそう...


# 普通にナップサックして解いてから。もしiを最後に食べてたらの処理をする？
# ナップサック復元して、まだ食べてないもののなかから美味しさが最大のものを食べればよい！

'''
dp[i][j] ... 最大の美味しさの総和。[,i)の料理まで考慮したとき。j分以内に食べ終わることができる。
'''

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_a_int(): return int(read())


def read_ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


# default import
from itertools import product, permutations, combinations


# ナップサック復元して、まだ食べてないもののなかから美味しさが最大のものを食べればよい！

N, T = read_ints()
A, B = read_col(N)
T -= 1
'''
dp[i][j] ... 最大の美味しさの総和。[,i)の料理まで考慮したとき。j分以内に食べ終わることができる。
'''

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

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

# dp復元しながら食べた料理の美味しさを0に書き換える(あとでmaxを取るときに無視するため)
i = N
j = T - 1
while i > 0:
    if dp[i][j] == dp[i - 1][j]:  # 上から来たものだったらi-1番目のものは使ってない
        pass
    else:  # 斜めから来てたらi-1番目のものを使っている
        B[i - 1] = 0  # 使ったものは美味しさを0にセットしておく
        j -= A[i - 1]
    i -= 1

print(dp[-1][-1] + max(B))

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc145/tasks/abc145_e
# なんとなくナップサック問題っぽい
# A分以内に食べきる事のできる最大の美味しさは？ならすぐにできる。
# 問題は最後のA分は必要ないこと

# →i番目の料理を最後に食べたときに、それ以外の料理でT分以内に食べることのできる美味しさの最大は？
# O(n^3) ∵ 各iについて(3000) × dp(3000*3000)
# →TLEしてしまいそう...


# 普通にナップサックして解いてから。もしiを最後に食べてたらの処理をする？
# ナップサック復元して、まだ食べてないもののなかから美味しさが最大のものを食べればよい！

'''
dp[i][j] ... 最大の美味しさの総和。[,i)の料理まで考慮したとき。j分以内に食べ終わることができる。
'''

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_a_int(): return int(read())


def read_ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


# default import
from itertools import product, permutations, combinations


# ナップサック復元して、まだ食べてないもののなかから美味しさが最大のものを食べればよい！

N, T = read_ints()
A, B = read_col(N)
'''
dp[i][j] ... 最大の美味しさの総和。[,i)の料理まで考慮したとき。j分以内に食べ終わることができる。
'''

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

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

# dp復元しながら食べた料理の美味しさを0に書き換える(あとでmaxを取るときに無視するため)
i = N
j = T - 1
while i > 0:
    if dp[i][j] == dp[i - 1][j]:  # 上から来たものだったらi-1番目のものは使ってない
        pass
    else:  # 斜めから来てたらi-1番目のものを使っている
        B[i - 1] = 0  # 使ったものは美味しさを0にセットしておく
        j -= A[i - 1]
    i -= 1

print(dp[-1][-1] + max(B))

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n,t=map(int,readline().split())
    a=[0]*n
    b=[0]*n
    for i in range(n):
        a[i],b[i]=map(int,readline().split())

    #ナップサックDP
    inf=-10**9
    dp=[[inf]*(t+1) for _ in range(n+1)]
    dp[0][0]=0
    for i in range(n):
        for j in range(t+1):
            #i番目を食べる
            if j!=t:
                dp[i+1][min(t,j+a[i])]=max(dp[i+1][min(t,j+a[i])],dp[i][j]+b[i])
            
            #i番目を食べない
            dp[i+1][j]=max(dp[i+1][j],dp[i][j])

    ans=0
    for j in range(t+1):
        ans=max(ans,dp[n][j])

    print(ans)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,T = map(int, raw_input().split(' '))
dishes = [map(int, raw_input().split(' ')) for _ in range(n)]

#print n,T
#print dishes
h = [0 for iii in range(T + 1)]
m = dishes[0][1] if len(dishes) == 1 else 0
for u,v in dishes:
	m = max(m , h[T-1] + v)
	for tt in range(T, -1, -1):
		if tt - u >= 0:
			h[tt] = max(h[tt], v + h[tt - u])
		
print m

Submission status: Wrong Answer

Solution code:
n,T = map(int, raw_input().split(' '))
dishes = [map(int, raw_input().split(' ')) for _ in range(n)]

#print n,T
#print dishes
h = {0:0}
m = dishes[0][1] if len(dishes) == 1 else 0
for u,v in dishes:
	if (T-1) in h:
		m = max(m , h[T-1] + v)
	for tt in range(T, -1, -1):
		if tt - u >= 0:
			h[tt] = max(h[tt] if tt in h else 0, v + (h[tt - u] if (tt - u >= 0 and tt - u in h) else 0))
		
print m

Submission status: Wrong Answer

Solution code:
import numpy as np
n,t=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n)]
dp1=np.zeros((n+1,3001),np.int64)
dp2=np.zeros((n+2,3001),np.int64)
# dp1[i][j]:0<=i<=n:i個目までの料理で考えたとき（i個の料理）、時間j以内で食べられるおいしさの総和
# dp2[i][j]:1<=i<=n+1:i個目以降の料理で考えたとき（n-i+1個の料理）、時間j以内で食べられるおいしさの総和
for i in range(n):
  a,b=ab[i]
  dp1[i+1,a:]=np.maximum(dp1[i,a:],dp1[i,:-a]+b)
  j=n-i
  a,b=ab[j-1]
  dp2[j,a:]=np.maximum(dp2[j+1,a:],dp2[j+1,:-a]+b)
ans=0
for i in range(1,n+1):# i個目の料理を最後に食べるとするとき。
  ai=max([dp1[i-1][j]+dp2[i+1][t-1-j]+ab[i-1][1] for j in range(t)])
  ans=max(ans,ai)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,t=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n)]
dp1=np.zeros((n+1,3001),np.int64)
dp2=np.zeros((n+2,3001),np.int64)
# dp1[i][j]:0<=i<=n:i個目までの料理で考えたとき（i個の料理）、時間j以内で食べられるおいしさの総和
# dp2[i][j]:1<=i<=n+1:i個目以降の料理で考えたとき（n-i+1個の料理）、時間j以内で食べられるおいしさの総和
for i in range(n):
  a,b=ab[i]
  dp1[i+1,a:]=np.maximum(dp1[i,a:],dp1[i,:-a]+b)
  j=n-i
  a,b=ab[j-1]
  dp2[j,a:]=np.maximum(dp2[j+1,a:],dp2[j+1,:-a]+b)
ans=0
for i in range(1,n+1):# i個目の料理を最後に食べるとするとき。
  ai=max([dp1[i-1][j]+dp2[i+1][t-1-j]+ab[i-1][1] for j in range(t)])
  ans=max(ans,ai)
print(ans)
print(dp2[n,:25])
print(dp2[n+1])