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:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,boundary=1,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

def gcj(t,*x):
    print("Case #"+str(t)+":",*x)
    return

n,t=LI()
N=6001
N=31
dp=[[0]*t for i in range(n+1)]
c=[]
for i in range(n):
    a,b=LI()
    c+=[(a,b)]
c.sort(key=lambda x:x[0])
for i in range(n):
    a,b=c[i]
    ans=max(ans,dp[i][-1]+b)
    for j in range(t+N):
        if j+a<t:
            dp[i+1][j+a]=max(dp[i][j]+b,dp[i][j+a])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,T = IL()
ab = [IL() for i in range(N)]

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

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = [inpl() for _ in range(n)]
rwv = wv[::-1]
udp = [[0] * t for _ in range(n+5)]
ddp = [[0] * t for _ in range(n+5)]

for i in range(n-1):
    w,v = wv[i]
    for j in range(t):
        if j < w:
            udp[i+1][j] = udp[i][j]
        else:
            udp[i+1][j] = max(udp[i][j], udp[i][j-w] + v)
res = udp[n-1][t-1] + wv[n-1][1]

for i in range(n-1):
    w,v = rwv[i]
    for j in range(t):
        if j < w:
            ddp[i+1][j] = ddp[i][j]
        else:
            ddp[i+1][j] = max(ddp[i][j], ddp[i][j-w] + v)
    res = max(res,ddp[n-1][t-1] + wv[0][1])

for i in range(1,n-1):
    u = i; d = n-i-1
    mx = 0
    for j in range(t):
        tmp = udp[u][j] + ddp[d][t-1-j]
        mx = max(mx, tmp)
    res = max(res, mx)
print(res)

Submission status: Wrong Answer

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

a.sort(key=lambda x:x[1])

# 一番美味しいものを避けておく
ans=a[-1][1]
b=a[0:n-1]

# dp[i][j]: i番目の料理まで見て時間がjの時の満足度のMAX
dp=[[0]*t for i in range(n)]

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

ans=dp[-1][-1]+ans

a.sort(reverse=True)
x=0
y=0
for i in range(n):
    if a[i][0]>t-1:
        if x<a[i][1]:
            x=a[i][1]
            y=i
    else:
        break

if x==0:
    print(ans)


else:
    ans1=x
    del a[y]
    dp = [[0] * t for i in range(n)]

    for i in range(n - 1):
        for j in range(t):
            if a[i][0] > j:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i][j - a[i][0]] + a[i][1], dp[i][j])
    print(max(ans,ans1+dp[-1][-1]))

Submission status: Wrong Answer

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

a.sort(key=lambda x:x[1])

# 一番美味しいものを避けておく
ans=a[-1][1]
b=a[0:n-1]

# dp[i][j]: i番目の料理まで見て時間がjの時の満足度のMAX
dp=[[0]*t for i in range(n)]

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

ans=dp[-1][-1]+ans

a.sort(reverse=True)
x=0
y=0
for i in range(n):
    if a[i][0]>=t-1:
        if x<a[i][1]:
            x=a[i][1]
            y=i
    else:
        break

if x==0:
    print(ans)


else:
    ans1=a[y][1]
    del a[y]
    dp = [[0] * t for i in range(n)]

    for i in range(n - 1):
        for j in range(t):
            if a[i][0] > j:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i][j - a[i][0]] + a[i][1], dp[i][j])
    print(max(ans,ans1+dp[-1][-1]))

Submission status: Wrong Answer

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

a.sort(key=lambda x:x[1])

# 一番美味しいものを避けておく
ans=a[-1][1]
del a[-1]

# dp[i][j]: i番目の料理まで見て時間がjの時の満足度のMAX
dp=[[0]*t for i in range(n)]

for i in range(n-1):
    for j in range(t):
        if a[i][0]>j:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j-a[i][0]]+a[i][1],dp[i][j])

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

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort(key=lambda p: p[1])
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]
    dp = [[0]*(6005) for _ in range(N+1)]
    for i in range(N):
        for t in range(T):
            dp[i+1][t+A[i]] = max(dp[i+1][t+A[i]], dp[i][t] + B[i])

    ans1 = max(dp[N][T:])
    AB.sort()
    dp = [[0]*(6005) for _ in range(N+1)]
    for i in range(N):
        for t in range(T):
            dp[i+1][t+A[i]] = max(dp[i+1][t+A[i]], dp[i][t] + B[i])
    ans2 = max(dp[N][T:])
    print(max(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N,W = list(map(int,input().split()))
vw = [list(map(int,input().split())) for i in range(N)]
value = [i[1] for i in vw]
weight = [i[0] for i in vw]
#valueが最大の要素を事前に抽出
v_max_id = 0
v_max_v = 0
v_max_w = 0
for i,v in enumerate(value):
    if v > v_max_v:
        v_max_id = i
        v_max_v = v
        v_max_w = weight[i]
    else:
        if v==v_max_v and weight[i] > v_max_w:
            v_max_id = i
            v_max_v = v
            v_max_w = weight[i]
N-=1
W-=1
del value[v_max_id]
del weight[v_max_id]

inf=float("inf")
dp = [[-inf for _ in range(W+1)] for _ in range(N+1)]

dp[0] = [0]*(W+1)
def nap():
    for i in range(N):
        w = 0
        while w <= W:
            if weight[i] <= w:
                dp[i+1][w]=max(dp[i][w-weight[i]]+value[i],dp[i][w])
            else:
                dp[i+1][w]=dp[i][w]
            w+=1
nap()
print(v_max_v+dp[N][W])

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort(key=lambda p: p[1])
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]
    dp = [[0]*(6005) for _ in range(N+1)]
    for i in range(N):
        for t in range(T):
            dp[i+1][t+A[i]] = max(dp[i+1][t+A[i]], dp[i][t] + B[i])
    print(max(dp[N][T:]))
    # print(*dp, sep="\n")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a.sort(key=lambda x:x[1])

# 一番美味しいものを避けておく
ans=a[n-1][1]
del a[n-1]

# dp[i][j]: i番目の料理まで見て時間がtの時の満足度のMAX
dp=[[0]*t for i in range(n)]

for i in range(n-1):
    for j in range(t):
        if a[i][0]>j:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j-a[i][0]]+a[i][1],dp[i][j])

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

Submission status: Wrong Answer

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

a.sort(key=lambda x:x[1])

# 一番美味しいものを避けておく
ans=a[n-1][1]
del a[n-1]

# dp[i][j]: i番目の料理まで見て時間がtの時の満足度のMAX
dp=[[0]*(t) for i in range(n)]

for i in range(n-1):
    for j in range(t):
        if a[i][0]>=j:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j-a[i][0]]+a[i][1],dp[i][j])

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

Submission status: Wrong Answer

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

a.sort(key=lambda x:x[1])

# 一番美味しいものを避けておく
ans=a[n-1][1]
del a[n-1]

# dp[i][j]: i番目の料理まで見て時間がtの時の満足度のMAX
dp=[[0]*(t+1) for i in range(n)]

for i in range(n-1):
    for j in range(t+1):
        if a[i][0]>=j:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j-a[i][0]]+a[i][1],dp[i][j])

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

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = [inpl() for _ in range(n)]
wv.sort(reverse = True)
res = 0
for i in range(min(n,10)):
    dp = [[0] * (t+5) for _ in range(n+5)]
    for j in range(n):
        w,v = wv[j]
        for k in range(t):
            if k < w or i == j:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-w] + v)
    # print(dp[n][t-1])
    res = max(dp[n][t-1] + wv[i][1], res)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
read = sys.stdin.read
from operator import itemgetter

n, t = map(int, input().split())
m = map(int, read().split())
AB = sorted(zip(m, m), key=itemgetter(1))
A, B = zip(*AB)
dp = [[0]*(t+1) for _ in range(n)]
for i, a in enumerate(A[:-1]):
  for j in range(t+1):
    if j < a:
      dp[i+1][j] = dp[i][j]
    else:
      dp[i+1][j] = max(dp[i][j-a]+B[i], dp[i][j])
ans = 0
for i in range(n):
  ans = max(ans, dp[i][-1] + B[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
read = sys.stdin.read
from operator import itemgetter

n, t = map(int, input().split())
m = map(int, read().split())
AB = sorted(zip(m, m), key=itemgetter(1))
A, B = zip(*AB)
dp = [[0]*t for _ in range(n)]
for i, a in enumerate(A[:-1]):
  for j in range(t):
    if j < a:
      dp[i+1][j] = dp[i][j]
    else:
      dp[i+1][j] = max(dp[i][j-a]+B[i], dp[i][j])
ans = 0
for i in range(n):
  ans = max(ans, dp[i][-1] + B[i])
print(ans)

Submission status: Wrong Answer

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

ans = 0
DP = [0] * T

for i in range(N):
  a, b = AB[i]
  for j in range(T - 1, -1, -1):
    if j + a >= T:
      ans = max(ans, DP[j] + b)
    else:
      DP[j + a] = max(DP[j + a], DP[j] + b)

for i in range(T):
  ans = max(ans, DP[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, T = map(int, input().split())
a = []
for _ in range(N):
  w, v = map(int, input().split())
  a.append((w, v))

dp = [[0] * (T + 1) for _ in range(N + 1)]
for i in range(N):
  w, v = a[i]
  for j in range(T):
    if j + w >= T:
      dp[i + 1][T] = max(dp[i + 1][T], dp[i][j] + v)
    else:
      dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)
    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
  dp[i + 1][T] = max(dp[i + 1][T], dp[i][T])
print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, T = iip()

    dp1 = [[0 for _ in range(T)] for _ in range(N + 1)]
    dp2 = [[0 for _ in range(T)] for _ in range(N + 1)]
    AB = [iip() for i in range(N)]


    for i, ab in enumerate(AB, 1):
        a, b = ab
        for j in range(T):
            dp1[i][j] = dp1[i-1][j]
        for j in reversed(list(range(T))):
            if j+a >= T:
                continue
            dp1[i][j + a] = max(dp1[i][j + a], dp1[i][j] + b)


    for i, ab in enumerate(reversed(AB), 1):
        a, b = ab
        for j in range(T):
            dp2[i][j] = dp2[i-1][j]
        for j in reversed(list(range(T))):
            if j + a >= T:
                continue
            dp2[i][j + a] = max(dp2[i][j + a], dp2[i][j] + b)

    #dp2.reverse()

    mm = 0
    for i in range(N-1):
        for j in range(T):
            ii1 = i
            ii2 = N-i-1
            ii3 = i

            ij1 = j
            ij2 = T-j-1

            a = dp1[ii1][ij1]
            b = dp2[ii2][ij2]
            c = AB[ii3][1]
            #print(ii1, ii2, ij1, ij2, ii3, a, b, c, a+b+c)
            #print(a, b, c)
            mm = max(a+b+c, mm)


            #print(i, j, cur, a, b)


    #print(dp1)
    #print(dp2)

    print(mm)


#####################################################ライブラリ集ここから

def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, T = lr()
AB = [lr() for _ in range(N)]
AB.sort()
# 1つ除いたDP, DP[T-1]に除いたおいしさを足す
dp_left = np.array([np.zeros(T, np.int64) for _ in range(N+1)])
for i in range(N):
    time, sweet = AB[i]
    dp_left[i+1][time:] = np.maximum(dp_left[i][time:], dp_left[i][:-time] + sweet)

dp_right = np.array([np.zeros(T, np.int64) for _ in range(N+1)])
for i in range(N):
    time, sweet = AB[N-1-i]
    dp_right[i+1][time:] = np.maximum(dp_right[i][time:], dp_right[i][:-time] + sweet)

dp_right = dp_right[::-1]
answer = 0
for i in range(N):
    result = (dp_left[i] + dp_right[i+1][::-1]).max() + AB[i][1]
    if result > answer:
        answer = result

print(answer)

Submission status: Wrong Answer

Solution code:
ans=0
n,t=map(int,input().split())
food=[]
for i in range(n):
    f=list(map(int,input().split()))
    food.append(f)
f=sorted(f)
dp=[0,0]+[-1]*(t-1)

for a,b in food:
    tmp=[dp[i] for i in range(t)]
    for i in range(t):
        if dp[i]==-1: continue
        j=i+a
        if j<t: tmp[j]=max(tmp[j],dp[i]+b)
        else: ans=max(ans,dp[i]+b)
    
    dp=[tmp[i] for i in range(t)]
print(max(ans,max(dp)))

Submission status: Wrong Answer

Solution code:
ans=0
n,t=map(int,input().split())
dp=[0]+[-1]*(t-1)
for _ in range(n):
    a,b=map(int,input().split())
    tmp=[dp[i] for i in range(t)]
    for i in range(t):
        if dp[i]==-1: continue
        j=i+a
        if j<t: tmp[j]=max(tmp[j],dp[i]+b)
        else: ans=max(ans,dp[i]+b)
    
    dp=[tmp[i] for i in range(t)]
print(max(ans,max(dp)))

Submission status: Wrong Answer

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

def main():
  N,T=map(int,input().split())
  L=[[int(x) for x in input().split()] for _ in range(N)]
  L.sort(key=lambda x:x[1])
  dp=[[0]*T for _ in range(2)]

  for i in range(N-1):
    for j in range(T):
      if L[i][0]>j:
        dp[-~i%2][j]=dp[i%2][j]
      else:
        dp[-~i%2][j]=max(dp[i%2][j],dp[i%2][j-L[i][0]]+L[i][1])
  print(max(dp[(N-1)%2])+L[N-1][1])

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def main():
  N,T=map(int,input().split())
  L=[[int(x) for x in input().split()] for _ in range(N)]
  L.sort(key=lambda x:x[1])
  dp=[[0]*T for _ in range(2)]

  for i in range(N-1):
    for j in range(T):
      if L[i][0]>j:
        dp[-~i%2][j]=dp[i%2][j]
      else:
        dp[-~i%2][j]=max(dp[i%2][j],dp[i%2][j-L[i][0]]+L[i][1])
  print(max(dp[(N-1)%2])+L[N-1][1])

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def main():
  N,T=map(int,input().split())
  L=[[int(x) for x in input().split()] for _ in range(N)]
  L.sort(key=lambda x:x[1])
  dp=[[0]*T for _ in range(2)]

  for i in range(N-1):
    for j in range(T):
      if L[i][0]>j:
        dp[-~i%2][j]=dp[i%2][j]
      else:
        dp[-~i%2][j]=max(dp[i%2][j],dp[i%2][j-L[i][0]]+L[i][1])
  print(max(dp[(N-1)%2])+L[N-1][1])

if __name__ == 'main':
  main()

Submission status: Wrong Answer

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

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

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

print(max(dp[(N-1)%2])+L[N-1][1])

Submission status: Wrong Answer

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

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

for i in range(N-1):
  for j in range(T):
    dp[-~i%2][j]=dp[i%2][j]
    if j>=L[i][0]:
      t=dp[i%2][j-L[i][0]]+L[i][1]
      if dp[i%2][j]<t:
        dp[-~i%2][j]=t

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
L=[[int(x) for x in input().split()] for _ in range(N)]
L.sort(key=lambda x:x[0])

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

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

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

Submission status: Wrong Answer

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

#dp1[i][j] ... 1~iでj分以内の最大値
#dp2[i][j] ... i~nでj分以内の最大値
dp1 = [[0 for _ in range(t)] for _ in range(n+1)]
dp2 = [[0 for _ in range(t)] for _ in range(n+2)]

for i in range(1, n+1):
    a, b = ab[i-1]
    for j in range(1, t):
        if j - a >= 0:
            dp1[i][j] = max(dp1[i-1][j], dp1[i-1][j-a] + b)

for i in range(n, 0, -1):
    a, b = ab[i-1]
    for j in range(1, t):
        if j - a >= 0:
            dp2[i][j] = max(dp2[i+1][j], dp2[i+1][j-a] + b)

ans = 0
for i in range(1, n+1):
    b = ab[i-1][1]
    for j in range(0, t):
        x = dp1[i-1][j] + dp2[i+1][t-j-1] + b
        ans = max(ans, x)
        
print(ans)

Submission status: Wrong Answer

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

#dp1[i][j] ... 1~iでj分以内の最大値
#dp2[i][j] ... i~nでj分以内の最大値
dp1 = [[0 for _ in range(t+1)] for _ in range(n+1)]
dp2 = [[0 for _ in range(t+1)] for _ in range(n+1)]

for i in range(1, n+1):
    a, b = ab[i-1]
    for j in range(1, t+1):
        if j - a > 0:
            dp1[i][j] = max(dp1[i-1][j], dp1[i-1][j-a] + b)

for i in range(n, 0, -1):
    a, b = ab[i-1]
    for j in range(1, t+1):
        if j - a > 0:
            dp2[i][j] = max(dp2[i-1][j], dp2[i-1][j-a] + b)

ans = 0
for i in range(1, n+1):
    b = ab[i-1][1]
    for j in range(0, t+1):
        if i == 1:
            x = dp2[i+1][t-j] + b
        elif i == n:
            x = dp1[i-1][j] + b
        else:
            x = dp1[i-1][j] + dp2[i+1][t-j] + b
        
        ans = max(ans, x)
        
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
def iip(listed = False):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def inv(n, mod):
    return power(n, mod-2)

def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0

    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    #if test:
    #    print(f"{n}C{r} = {ret}")
    return ret

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


#########################入力受付部
N, T = iip()
AB = []
A = []
B = []
for i in range(N):
    a, b = iip()
    AB.append((a, b))
    AB = sorted(AB, key=lambda x:x[1])

#########################入力受付部

def main():
    manzoku_list = [0 for i in range(T)]
    tabeta_list = [[] for i in range(T)]

    for i, ab in enumerate(AB):
        a = ab[0]
        b = ab[1]
        #print(a, b)
        manzoku_list2 = [i for i in manzoku_list]
        tabeta_list2 = [i for i in tabeta_list]
        for j, manzoku in enumerate(manzoku_list2):
            if not j+a >= T:
                if manzoku_list2[j+a] < manzoku+b:
                    manzoku_list[j+a] = manzoku+b
                    tt = [k for k in tabeta_list2[j]]
                    tt.append(i)
                    tabeta_list[j+a] = tt
        #print(tabeta_list)

    maxx = 0
    for i, ab in enumerate(AB):
        if not i in tabeta_list[T-1]:
            maxx = max(ab[1], maxx)


    ans = manzoku_list[T-1] + maxx

    #print(tabeta_list)
    #print(manzoku_list)
    print(ans)


main()

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
def iip(listed = False):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def inv(n, mod):
    return power(n, mod-2)

def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0

    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    #if test:
    #    print(f"{n}C{r} = {ret}")
    return ret

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


#########################入力受付部
N, T = iip()
AB = []
A = []
B = []
for i in range(N):
    a, b = iip()
    AB.append((a, b))
    AB = sorted(AB, key=lambda x:x[1])

#########################入力受付部

def main():
    manzoku_list = [0 for i in range(T)]
    tabeta_list = [[] for i in range(T)]

    for i, ab in enumerate(AB):
        a = ab[0]
        b = ab[1]
        #print(a, b)
        manzoku_list2 = [i for i in manzoku_list]
        tabeta_list2 = [i for i in tabeta_list]

        for j, manzoku in enumerate(manzoku_list2):
            if not j+a >= T:
                if manzoku_list2[j+a] < manzoku+b:
                    manzoku_list[j+a] = manzoku+b
                    tt = [i for i in tabeta_list2[j]]
                    tt.append(i)
                    tabeta_list[j+a] = tt
        #print(tabeta_list)

    maxx = 0
    for i, ab in enumerate(AB):
        if not i in tabeta_list[T-1]:
            maxx = max(ab[1], maxx)


    ans = manzoku_list[T-1] + maxx

    #print(tabeta_list)
    #print(manzoku_list)
    print(ans)


main()