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**6)

def main():
    def gourmet(A,B,T):
        if 0>=T:
            return 0
        elif len(A)==1:
            return B[0]
        else:
            return max(B[0]+gourmet([A[i] for i in range(1,len(A))],[B[i] for i in range(1,len(B))],T-A[0]),gourmet([A[i] for i in range(1,len(A))],[B[i] for i in range(1,len(B))],T))


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

    print(gourmet(A,B,T))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

N,T = list(map(int, input().split()))
R = []
for i in range(N):
  li = list(map(int, input().split()))
  R.append(li)
  
R.sort(key=itemgetter(1,0), reverse=True) 
# DP1[i][j] = 1～i 番目の料理で j 分以内に完食できる美味しさの合計の最大値
# 0番目はT-1分に食べる、としてよい.美味しさ最大でかつ食べるのに一番時間がかかるもの

DP1 = [[0 for i in range(T+1)] for j in range(N)]

for i in range(1,N):
  tim, val = R[i]
  for j in range(T+1):
    if j > tim:
      DP1[i][j] = max(DP1[i-1][j-tim] + val, DP1[i-1][j])
    else:
      DP1[i][j] = DP1[i-1][j]
      
print(DP1[N-1][T-1] + R[0][1])
#for i in range(N):
#  print(DP1[i])

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
A = [0]
B = [0]
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)

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

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
dish = []
for _ in range(n):
  a,b = map(int,input().split())
  dish.append([a,b])
dish.sort()
now = [0 for _ in range(t+1)]
for i in range(n):
  last = now
  now = [0 for _ in range(t+1)]
  a,b = dish[i]
  for d in range(t):
    if d + a < t:
      now[d+a] = max(last[d+a],last[d]+b)
    else:
      now[-1] = max(last[-1],last[d]+b)
  #print(now)
print(now[-1])

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
dish = []
for _ in range(n):
  a,b = map(int,input().split())
  dish.append([b,a])
dish.sort()
now = [0 for _ in range(t+1)]
for i in range(n):
  last = now
  now = [0 for _ in range(t+1)]
  b,a = dish[i]
  for d in range(t):
    if d + a < t:
      now[d+a] = max(last[d+a],last[d]+b)
    else:
      now[-1] = max(last[-1],last[d]+b)
  #print(now)
print(now[-1])

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
dish = []
for _ in range(n):
  a,b = map(int,input().split())
  dish.append([b,a])
dish.sort()
now = [0 for _ in range(t+1)]
for i in range(n):
  b,a = dish[i]
  for d in range(t)[::-1]:
    if d + a < t:
      now[d+a] = max(now[d+a],now[d]+b)
    else:
      now[-1] = max(now[-1],now[d]+b)
  #print(now)
print(now[-1])

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N,T = MI()
	max_T = -1
	dish = []
	for i in range(N):
		A,B = MI()
		dish.append([A,B])
		max_T = max(max_T, A)

	dp=np.array([-1 for _ in range(T+max_T)])
	dp[0] = 0
	upper = 1
	for i in range(N):
		ch = (dp>=0)
		ch[T:] = False
		ch2 = np.roll(ch, dish[i][0])
		ch2[:dish[i][0]]=False
		dp[ch2] = np.maximum(dp[ch2] , dp[ch] + dish[i][1])
	print(max(dp))
main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N,T = MI()
	max_T = -1
	dish = []
	for i in range(N):
		A,B = MI()
		dish.append([A,B])
		max_T = max(max_T, A)

	dp=np.array([-1 for _ in range(T+max_T)])
	dp[0] = 0
	upper = 1
	for i in range(N):
		ch = (dp>=0)
		ch[T:] = False
		ch2 = np.roll(ch, dish[i][0])
		dp[ch2] = np.maximum(dp[ch2] , dp[ch] + dish[i][1])
	print(max(dp))
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort()
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]
    dp1 = [[0]*(T+1) for i in range(N+1)]
    for i in range(1, N+1):
        for j in range(T):
            if j + A[i-1] < T:
                dp1[i][j + A[i-1]] = \
                    max(dp1[i][j + A[i-1]], dp1[i-1][j] + B[i-1])
            else:
                dp1[i][j] = max(dp1[i][j], dp1[i-1][j])
    dp2 = [[0]*(T+1) for i in range(N+1)]
    for j in range(T):
        if j + A[N-1] < T:
            dp2[N][j + A[N-1]] = B[N-1]
    for i in range(1, N)[::-1]:
        for j in range(T):
            if j + A[i-1] < T:
                dp2[i][j + A[i-1]] = \
                    max(dp2[i][j + A[i-1]], dp2[i+1][j] + B[i-1])
            else:
                dp2[i][j] = max(dp2[i][j], dp2[i+1][j])
    ans = 0
    for i in range(1, N):
        for j in range(T):
            ans = max(ans, dp1[i-1][j] + dp2[i+1][T-1-j] + B[i-1])
    for j in range(T):
        ans = max(ans, dp1[N-1][j] + B[N-1])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]
    dp = [[0]*(T+1) for i in range(N+1)]
    for i in range(N):
        for j in range(T):
            dp[i][j] = max(dp[i][j], dp[i-1][j])
            dp[i+1][min(j + A[i], T)] = \
                max(dp[i+1][min(j + A[i], T)], dp[i][j] + B[i])
    # for i in range(N+1):
    #    print(dp[i])
    print(dp[N][T])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())

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

max_idx = -1
max_val = -1
ans = 0

for i, j in enumerate(l):
    if max_val < j[0]:
        max_val = j[0]
        max_idx = i
        ans = j[1]
l.pop(max_idx)

A, B = zip(*l)
dp = [[0]*(T) for _ in range(N)]

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

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

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())

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

max_idx = -1
max_val = -1

for i, j in enumerate(l):
    if max_val < j[1]:
        max_val = j[1]
        max_idx = i
l.pop(max_idx)

A, B = zip(*l)
dp = [[0]*(T) for _ in range(N)]

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

print(max(dp[N-1])+max_val)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10 ** 7)

# ・最適解をとってみる
# ・最適解に仮定できる構造を探す
# ・探索空間が小さくなったら、既知の手法に当てはまる（dpなど）

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

FOOD.sort()
dp = np.array([np.zeros(T + 1, dtype=np.int64) for _ in range(N + 1)])
for i in range(N):
    a, b = FOOD[i]
    dp[i + 1][a:] = np.maximum(dp[i][:-a] + b, dp[i][a:])
    #dp[i + 1][a:T+a] = np.maximum(dp[i][:T] + b, dp[i][a:T+a])
    if len(dp[i][-a+1:T]) > 0:
        dp[i + 1][T] = max(dp[i + 1][T], max(dp[i][-a+1:T]) + b)

    # print(dp[i+1])

print(max(dp[N]))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10 ** 7)

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

FOOD.sort(reverse=True, key=lambda x: x[1])
dp = np.array([np.zeros(T+1, dtype=np.int64) for _ in range(N + 1)])
for i in range(N):
    a, b = FOOD[i]
    dp[i + 1][a:] = np.maximum(dp[i][:-a] + b, dp[i][a:])

    if len(dp[i][-a+1:T]) > 0:
        dp[i + 1][T] = max(dp[i + 1][T], max(dp[i][-a+1:T]) + b)


print(dp[N][T])

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()
DP = [[0 for i in range(t+3001)] for j in range(n+1)]
for i in range(1,n+1):
  for j in range(1,t+3001):
    if j-a[i-1][0] >= 0:
      DP[i][j] = max(DP[i-1][j],DP[i-1][j-a[i-1][0]]+a[i-1][1])
    else:
      DP[i][j] = DP[i-1][j]
ans = 0
for i in range(n):
  ans = max(ans,DP[i][t-1]+a[i][1])
  print(ans)

Submission status: Wrong Answer

Solution code:
N,T = list(map(int,input().split()))

A =[list(map(int,input().split())) for i in range(N)]

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

INF = float("inf")

DP = [[-INF]*(T+3001) 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])
        DP[i+1][j+A[i][0]] = max(DP[i+1][j+A[i][0]],DP[i][j+A[i][0]],DP[i][j]+A[i][1])
score = 0
for i in range(N):
    for j in range(T+A[i][0]):
        score = max(DP[i+1][j],score)

print(score)

Submission status: Wrong Answer

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

dp1 = [[0 for _ in range(t)] for _ in range(n)]
ab.sort()
for i in range(n):
    for j in range(t):
        if i == 0:
            if j >= ab[i][0]:
                dp1[i][j] = ab[i][1]
            else:
                dp1[i][j] = dp1[i][j - 1]
        else:
            if j - ab[i][0] >= 0:
                dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - ab[i][0]] + ab[i][1])
            else:
                dp1[i][j] = dp1[i - 1][j]


ans = 0
print(ab[-1][1] + dp1[-2][-1])

Submission status: Wrong Answer

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

dp1 = [[0 for _ in range(t - 1)] for _ in range(n)]
ab.sort()
for i in range(n):
    for j in range(t - 1):
        if i == 0:
            if j == ab[i][0]:
                dp1[i][j] = ab[i][1]
            else:
                dp1[i][j] = dp1[i][j - 1]
        else:
            if j - ab[i][0] >= 0:
                dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - ab[i][0]] + ab[i][1])
            else:
                dp1[i][j] = dp1[i - 1][j]

ab2 = ab[::-1]
dp2 = [[0 for _ in range(t - 1)] for _ in range(n)]
for i in range(n):
    for j in range(t - 1):
        if i == 0:
            if j == ab2[i][0]:
                dp2[i][j] = ab2[i][1]
            else:
                dp2[i][j] = dp2[i][j - 1]
        else:
            if j - ab2[i][0] >= 0:
                dp2[i][j] = max(dp2[i - 1][j], dp2[i - 1][j - ab2[i][0]] + ab2[i][1])
            else:
                dp2[i][j] = dp2[i - 1][j]
dp2 = dp2[::-1]
ans = 0
for i in range(n):
    max_tmp = 0
    tmp = 0
    for j in range(t - 1):
        tmp = 0
        if i > 0:
            tmp = dp1[i - 1][j]
        if i < n - 1:
            tmp += dp2[i + 1][t - 2 - j]

        max_tmp = max(max_tmp, tmp + ab[i][1])
    ans = max(max_tmp, ans)
print(ans)

Submission status: Wrong Answer

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

dp1 = [[0 for _ in range(t - 1)] for _ in range(n)]
ab.sort()
for i in range(n):
    for j in range(t - 1):
        if i == 0:
            if j == ab[i][0]:
                dp1[i][j] = ab[i][1]
            else:
                dp1[i][j] = dp1[i][j - 1]
        else:
            if j - ab[i][0] >= 0:
                dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - ab[i][0]] + ab[i][1])
            else:
                dp1[i][j] = dp1[i - 1][j]

ab2 = ab[::-1]
dp2 = [[0 for _ in range(t - 1)] for _ in range(n)]
for i in range(n):
    for j in range(t - 1):
        if i == 0:
            if j == ab2[i][0]:
                dp2[i][j] = ab2[i][1]
            else:
                dp2[i][j] = dp2[i][j - 1]
        else:
            if j - ab2[i][0] >= 0:
                dp2[i][j] = max(dp2[i - 1][j], dp2[i - 1][j - ab2[i][0]] + ab2[i][1])
            else:
                dp2[i][j] = dp2[i - 1][j]
dp2 = dp2[::-1]
ans = 0
for i in range(n):
    max_tmp = 0
    tmp = 0
    for j in range(t - 1):
        tmp = 0
        if i > 0:
            tmp = dp1[i - 1][j]
        if i < n - 1:
            tmp += dp2[i + 1][t - 2 - j]

    max_tmp = max(max_tmp, tmp + ab[i][1])
    ans = max(max_tmp, ans)
print(ans)

Submission status: Wrong Answer

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

dp1 = [[0 for _ in range(t - 1)] for _ in range(n)]
ab.sort()
for i in range(n):
    for j in range(t - 1):
        if i == 0:
            if j == ab[i][0]:
                dp1[i][j] = ab[i][1]
            else:
                dp1[i][j] = dp1[i][j - 1]
        else:
            if j - ab[i][0] >= 0:
                dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - ab[i][0]] + ab[i][1])
            else:
                dp1[i][j] = dp1[i - 1][j]

ab2 = ab[::-1]
dp2 = [[0 for _ in range(t - 1)] for _ in range(n)]
for i in range(n):
    for j in range(t - 1):
        if i == 0:
            if j == ab2[i][0]:
                dp2[i][j] = ab2[i][1]
            else:
                dp2[i][j] = dp2[i][j - 1]
        else:
            if j - ab2[i][0] >= 0:
                dp2[i][j] = max(dp2[i - 1][j], dp2[i - 1][j - ab2[i][0]] + ab2[i][1])
            else:
                dp2[i][j] = dp2[i - 1][j]
dp2 = dp2[::-1]
ans = 0
for i in range(n):
    max_tmp = 0
    tmp = 0
    for j in range(t - 1):
        tmp = 0
        if i > 0:
            tmp = dp1[i - 1][j]
        if i < n - 1:
            tmp += dp2[i + 1][t - 2 - j]

    max_tmp = max(max_tmp, tmp) + ab[i][1]
    ans = max(max_tmp, ans)
print(ans)

Submission status: Wrong Answer

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

dp1 = [[0 for _ in range(t - 1)] for _ in range(n)]
ab.sort()
for i in range(n):
    for j in range(t - 1):
        if i == 0:
            if j == ab[i][0]:
                dp1[i][j] = ab[i][1]
            else:
                dp1[i][j] = dp1[i][j - 1]
        else:
            if j - ab[i][0] >= 0:
                dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - ab[i][0]] + ab[i][1])
            else:
                dp1[i][j] = dp1[i - 1][j]

ab2 = ab[::-1]
dp2 = [[0 for _ in range(t - 1)] for _ in range(n)]
for i in range(n):
    for j in range(t - 1):
        if i == 0:
            if j == ab2[i][0]:
                dp2[i][j] = ab2[i][1]
            else:
                dp2[i][j] = dp2[i][j - 1]
        else:
            if j - ab2[i][0] >= 0:
                dp2[i][j] = max(dp2[i - 1][j], dp2[i - 1][j - ab2[i][0]] + ab2[i][1])
            else:
                dp2[i][j] = dp2[i - 1][j]
dp2 = dp2[::-1]
ans = 0
for i in range(n):
    tmp = 0
    if i > 0:
        tmp = dp1[i - 1][-1]
    if i < n - 1:
        tmp = max(tmp, dp2[i + 1][-1])

    tmp += ab[i][1]
    ans = max(tmp, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,W=LI()
  w=[]
  v=[]
  for _ in range(n):
    a,b=LI()
    w.append(a)
    v.append(b)

  dp=[[0 for _ in range(W+3001)]for __ in range(n+1)]

  for i in range(n):
    for j in range(W+1):
      dp[i+1][j]=max(dp[i+1][j],dp[i][j])
      if j<W:
        dp[i+1][j+w[i]]=max(dp[i+1][j+w[i]],dp[i][j]+v[i])

  mx=-inf
  for x in dp:
    _mx=max(x)
    mx=max(mx,_mx)

  return mx

# main()
print(main())

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l.sort()
dp=[[0]*(t+1) for i in range(n+1)]
for i in range(1,n+1):
  for j in range(t):
    if j+l[i-1][0]<t:
      dp[i][j+l[i-1][0]]=max(dp[i][j+l[i-1][0]],dp[i-1][j]+l[i-1][1])
    else:
      dp[i][t]=max(dp[i][t],dp[i-1][j]+l[i-1][1])
    dp[i][j]=max(dp[i-1][j],dp[i][j])
print(max(dp[n]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
AB = []
for _ in range(N):
    AB.append(list(map(int,input().split())))
dp1 = [[0 for _ in range(T+1)] for _ in range(N)]
for i in range(N):
    for j in range(T+1):
        if i == 0:
            if j >= AB[i][0]:
                dp1[i][j] = AB[i][1]
        else:
            if j-AB[i][0] < 0:
                continue
            dp1[i][j] = max(dp1[i-1][j-AB[i][0]]+AB[i][1],dp1[i-1][j])
dp2 = [[0 for _ in range(T+1)] for _ in range(N)]
for i in range(N)[::-1]:
    for j in range(T+1):
        if i == N-1:
            if j >= AB[i][0]:
                dp2[i][j] = AB[i][1]
        else:
            if j-AB[i][0] < 0:
                continue
            dp2[i][j] = max(dp2[i+1][j-AB[i][0]]+AB[i][1],dp2[i+1][j])
ans = 0
for i in range(N):
    for j in range(T):
        if i == 0:
            value = AB[i][1]+dp2[i+1][T-1]
        elif i == N-1:
            value = AB[i][1]+dp1[i-1][T-1]
        else:
            value = AB[i][1]+dp1[i-1][j]+dp2[i+1][T-1-j]
    if ans < value:
        ans = value
print(ans)

Submission status: Wrong Answer

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/abc145/tasks/abc145_e
# E - All-you-can-eat
"""

"""

N = gi()
T = gi()
BA = [] * (N + 5)

for i in range(N):
    a = gi()
    b = gi()
    BA.append((b, a))

BA.sort()

mv = [[0 for x in range(N + 5)] for i in range(T + 5)]

for i in range(N):
    for t_from in range(T + 1):
        if (t_from == T):
            mv[T][i + 1] = max(mv[T - 1][i] + BA[i][0], mv[t_from][i])
            continue

        mv[t_from][i + 1] = max(mv[t_from][i + 1], mv[t_from][i])

        it = BA[i][1]
        id = BA[i][0]

        if t_from >= it:
            mv[t_from][i + 1] = max(mv[t_from][i + 1], mv[t_from - it][i] + id)

print(mv[T][N])

Submission status: Wrong Answer

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/abc145/tasks/abc145_e
# E - All-you-can-eat
"""

"""

N = gi()
T = gi()
BA = [] * (N + 5)

for i in range(N):
    a = gi()
    b = gi()
    BA.append((b, a))

BA.sort()

mv = [[0 for x in range(N + 5)] for i in range(T + 5)]

for n_item in range(1, N):
    for t_from in range(1, T):
        #print(t_from, n_item)
        mv[t_from][n_item] = mv[t_from][n_item - 1]

        it = BA[n_item - 1][1]
        id = BA[n_item - 1][0]

        if t_from >= it:
            mv[t_from][n_item] = max(mv[t_from][n_item],
                                     mv[t_from - it][n_item - 1] + id)

print(mv[T - 1][N - 1] + BA[N - 1][0])

Submission status: Wrong Answer

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

W = T + 1
def dp(AB,W):
    N = len(AB)
    DP = [[0]*W for i in range(N+1)]
    for i in range(N):
        for w in range(W):
            if w != W-1:
                if AB[i][0] <= w:
                    DP[i+1][w] = max(DP[i][w-AB[i][0]]+AB[i][1],DP[i][w])
                else:
                    DP[i+1][w] = DP[i][w]
            else:
                DP[i+1][w] = max(DP[i][w-1]+AB[i][1],DP[i][w])
    return DP

DP1 = dp(AB,W)

ans = DP1[N][T]
print(ans)

Submission status: Wrong Answer

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

W = T + 1
def dp(AB,W):
    N = len(AB)
    DP = [[0]*W for i in range(N+1)]
    for i in range(N):
        for w in range(W):
            if w != W-1:
                if AB[i][0] <= w:
                    DP[i+1][w] = max(DP[i][w-AB[i][0]]+AB[i][1],DP[i][w])
                else:
                    DP[i+1][W-1] = DP[i][w]
            else:
                DP[i+1][w] = max(DP[i][w-1]+AB[i][1],DP[i][w])
    return DP

DP1 = dp(AB,W)

ans = DP1[N][T]
print(ans)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda x: x[1])
AB = AB[::-1]
#print(AB)

W = T + 1
def dp(AB,W,N):
    DP = [[0]*W for i in range(N+1)]
    for i in range(N):
        for w in range(W):
            if AB[i][0] <= w:
                DP[i+1][w] = max(DP[i][w-AB[i][0]]+AB[i][1],DP[i][w])
            else:
                DP[i+1][w] = DP[i][w]
    return DP

DP1 = dp(AB,W,N)
DP2 = dp(AB[1:],W,N-1)

ans = max(DP1[N][T], DP2[N-1][T], DP2[N-1][T-1]+AB[0][1])
print(ans)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
tlist=[0]*N
vlist=[0]*N
tvlist=[[0,0]]*N
dp=[[0]*T for i in range(N+1)]

for i in range(N):
    tvlist[i] = list(map(int,input().split()))
tvlist.sort(key=lambda x: x[0])
for i in range(N):
    tlist[i] = tvlist[i][0]
    vlist[i] = tvlist[i][1]

for i in range(N):
    for t in range(T):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

        print(i+1,t,dp[i+1][t])

ans=0
for i in range(N):
    ans = max(ans,dp[i][T-1]+tvlist[i][1])

print(ans)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
tlist=[0]*N
vlist=[0]*N
dp=[[0]*T for i in range(N+1)]

for i in range(N):
    tlist[i],vlist[i] = map(int,input().split())

tlist.sort()

for i in range(N):
    for t in range(T):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

        print(i+1,t,dp[i+1][t])

ans=0
for i in range(1,N):
    ans = max(ans,dp[i][T-1]+vlist[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
tlist=[0]*N
vlist=[0]*N
dp=[[0]*T for i in range(N+1)]

for i in range(N):
    tlist[i],vlist[i] = map(int,input().split())

tlist.sort()

for i in range(N):
    for t in range(T):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

ans=vlist[0]
for i in range(1,N):
    ans = max(ans,dp[i][T-1]+vlist[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
tlist=[0]*N
vlist=[0]*N
dp=[[0]*T for i in range(N+1)]

"""
for i in range(N):
    t[i+1],v[i+1] = map(int,input().split())

last=[[0]*(T+1) for i in range(N+1)]
for i in range(N):
    for j in range(1,T+1):
        if j>=t[i+1]:
            dp[i+1][j] = max(dp[i][j],dp[i][j-t[i]]+v[i+1],dp[i][j]-last[i][j]+v[i+1])
            if dp[i+1][j]!=dp[i][j] and dp[i+1][j]!=dp[i][j-t[i]]+v[i+1]:
                last[i+1][j]=v[i+1]
            else:
                last[i+1][j]=last[i][j]
        else:
            dp[i+1][j] = max(dp[i][j],dp[i][j]-last[i][j]+v[i+1])
            if dp[i+1][j]!=dp[i][j]:
                last[i+1][j]=v[i+1]
            else:
                last[i+1][j]=last[i][j]
        print(i+1,j,dp[i+1][j])
        print("last=",str(last[i+1][j]))

print(dp[N][T])
"""

for i in range(N):
    tlist[i],vlist[i] = map(int,input().split())

tlist.sort()
for i in range(N):
    for t in range(T-1):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

for i in range(N):
    for t in range(T):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

ans=vlist[0]
for i in range(1,N):
    ans = max(ans,dp[i][T-1]+vlist[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
tlist=[0]*N
vlist=[0]*N
dp=[[0]*T for i in range(N+1)]

for i in range(N):
    tlist[i],vlist[i] = map(int,input().split())

print(dp[1][0])

tlist.sort()
for i in range(N):
    for t in range(T-1):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

for i in range(N):
    for t in range(T):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

ans=vlist[0]
for i in range(1,N):
    ans = max(ans,dp[i][T-1]+vlist[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [[0, 0]] + [list(map(int, input().split())) for _ in range(N)]
ans = 0
for i in range(N+1):
    dp =[[0 for _ in range(T)] for _ in range(N+2)]
    for t in range(T):
        for n in range(1, N+1):
            if t-AB[n-1][0] < 0:
                continue
            else:
                if i == n:
                    dp[n+1][t] = dp[n][t]
                else:
                    if t-AB[n][0] >= 0:
                        dp[n+1][t] = max(dp[n][t], dp[n][t-AB[n][0]] + AB[n][1])
                    else:
                        dp[n+1][t] = dp[n][t]
    ans = max(ans, dp[-1][-1] + AB[i][1])
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [[0, 0]] + [list(map(int, input().split())) for _ in range(N)]
ans = 0
for i in range(N+1):
    dp =[[0 for _ in range(T)] for _ in range(N+2)]
    for t in range(T):
        for n in range(1, N+1):
            if t-AB[n-1][0] < 0:
                continue
            else:
                if i == n:
                    dp[n+1][t] = dp[n][t]
                else:
                    if t-AB[n][0] >= 0:
                        dp[n+1][t] = max(dp[n][t], dp[n][t-AB[n][0]] + AB[n][1])
                    else:
                        dp[n+1][t] = dp[n][t]
    ans = max(ans, max([max(row) for row in dp]) + AB[i][1])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
dp = defaultdict(lambda: 0)
n,t = [int(j) for j in input().split()]
lsa = []
lsb = []
dic = []
for i in range(n):
    a,b = [int(j) for j in input().split()]
    lsa.append(a)
    lsb.append(b)
    dic.append((b,a))    
dp[0,1,1] = lsb[0]
if lsa[0]>1:
    dp[0,1,0] = 0
else:
    dp[0,1,0] = lsb[0]
# dp = dict()
ans = 0
dic.sort()
for i in dic:
    lsa.append(i[1])
    lsb.append(i[0])
for i in range(n):
    for j in range(1,t+1):
        if j-lsa[i]>0:
            dp[i,j,0] = max(dp[i-1, j, 0], dp[i-1, j-lsa[i], 0]+lsb[i])
        else:
            dp[i,j,0] = dp[i-1, j, 0]
        # print(dp[i,j,0], i, j, 0)
        # print(dp[i,j], i, j)
        dp[i,j,1] = max(dp[i-1,j,1], dp[i-1, j, 0]+lsb[i])
print(max(dp[n-1, t, 0], dp[n-1, t, 1]))