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(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,T=map(int,input().split())
    AB=[tuple(map(int,input().split())) for _ in range(n)]
    AB.sort(lambda x:(x[1],x[0]))
    la,lb=AB.pop()

    dp=[0]*T
    for i in range(n-1):
        a,b=AB[i]
        ndp=dp[:]
        for t in range(T):
            if(t+a<T):
                ndp[t+a]=max(ndp[t+a],dp[t]+b)
        dp=ndp
    print(max(dp)+lb)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, T = map(int, input().split())
d = []
for _ in range(N):
  d.append(list(map(int, input().split())))
dp = [[0] * (T + 1) for _ in range(N + 1)]
res = 0
for i in range(N):
  for j in range(T):
    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
    if j + d[i][0] <= T: dp[i + 1][j + d[i][0]] = max(dp[i + 1][j + d[i][0]], dp[i][j] + d[i][1])
    else: dp[i + 1][T] = max(dp[i + 1][T], dp[i][j] + d[i][1])
  #print(dp[i])
for i in range(N + 1):
  res = max(max(dp[i]), res)
print(res)
#print(dp)

Submission status: Wrong Answer

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

dp = [[0 for _ in range(t)] for _ in range(n+1)]

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

#for x in dp: print(x)
ans = max(max(dp[n]), 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)]

AB.sort(key=lambda x:(x[1],x[0]),reverse=True)
ans1,time=0,0
for a,b in AB:
  ans1+=b
  time+=a
  if time>t-0.5:
    break
    
AB.sort(key=lambda x:(x[0],x[1]))
ans2,time=0,0
for a,b in AB:
  ans2+=b
  time+=a
  if time>t-0.5:
    break
print(max(ans1,ans2))

Submission status: Wrong Answer

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

dp = [[0]*3000 for i in range(N)]
for n in range(N-1):
    for w in range(3000):
        if w-AB[n][0]>=0:
            dp[n+1][w] = max(dp[n][w],dp[n][w-AB[n][0]]+AB[n][1])
        else:
            dp[n+1][w] = dp[n][w]
ans = 0
for w in range(T):
    ans = max(ans,dp[N-1][w]+AB[N-1][1])
print(ans)

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
ab = []
for i in range(n):
    ab.append(list(map(int, input().split())))
score = [-float("inf")] * (t + 1)
score[0] = 0
for i in range(n):
    change = []
    for j in range(t):
        if score[j] != -float("inf"):
            change.append(j)
    change.reverse()
    for j in change:
        score[min(j + ab[i][0], t)] = max(score[min(j + ab[i][0], t)], score[j] + ab[i][1])
print(max(score))

Submission status: Wrong Answer

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

dp = [[0 for t_sum in range(T+3003)] for i in range(N+1)]

for i in range(N):
    for t_sum in range(T):

        # これは配るDP

        # i-1番目の品物を選ぶ場合
        # 重量的に入れられるなら入れてみる
        dp[i+1][t_sum+items[i][0]] = max(dp[i+1][t_sum+items[i][0]],
                                             dp[i][t_sum]+items[i][1])

        # i-1番目の品物を選ばない場合
        dp[i+1][t_sum] = max(dp[i+1][t_sum], dp[i][t_sum])

ans = 0
for t in range(T+3003):
    ans = max(ans, dp[N][t])
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
ABs = []
for i in range(N):
  A, B = map(int, input().split())
  ABs.append((A, B))
T = T-1
I = max(range(N), key=lambda i: ABs[i][1])
A0, B0 = ABs.pop(I)
#print(T, ABs)
dp = [{ 0: 0 }]
for i in range(N-1):
  d = dp[i]
  A, B = ABs[i]
  d2 = {}
  for t in d:
    d2[t] = d[t]
  for t in d:
    if t+A > T:
      continue
    if t+A not in d2:
      d2[t+A] = d[t]+B
    else:
      d2[t+A] = max(d2[t+A], d[t]+B)
  dp.append(d2)
#print(dp)
r = max(dp[N-1].values())+B0
print(r)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
food1 = []
for _ in range(N):
    a, b = map(int, input().split())
    food1.append((a, b))
dp1 = [[0]*T for _ in range(N+1)]
dp2 = [[0]*T for _ in range(N+1)]
food2 = food1[::-1]
for i in range(N):
    a1, b1 = food1[i]
    a2, b2 = food2[i]
    for t in range(T-a1):
        dp1[i+1][t] = max(dp1[i+1][t], dp1[i][t])
        dp1[i+1][t+a1] = max(dp1[i+1][t+a1], dp1[i][t]+b1)
    for t in range(T-a2):
        dp2[i+1][t] = max(dp2[i+1][t], dp2[i][t])
        dp2[i+1][t+a2] = max(dp2[i+1][t+a2], dp2[i][t]+b2)
ans = 0
for i in range(N):
    res = max(food1[i][1]+dp1[i][t]+dp2[N-i-1][T-t-1] for t in range(T))
    ans = max(ans, res)
print(ans)

Submission status: Wrong Answer

Solution code:
n, t = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    ab.append([int(i) for i in input().split()])
c = [0] * (t*3)
for i in ab:
    for j in range(t-1, -1, -1):
        k = c[j]
        l = i[0] + j
        if l >= 3*t:
            continue
        c[l] = max(c[l], c[j] + i[1])
print(max(c))

Submission status: Wrong Answer

Solution code:

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 


#x,y = [int(i) for i in readline().split()]
#n = int(input())

n,t = [int(i) for i in readline().split()]
ab = [[int(i) for i in readline().split()] for i in range(n)]


dp = [0]*(t+1)

for a,b in ab:
    c = max(dp[max(0,t-a):t])
    dp[-1] = max(dp[-1],c+b)
    
    for i in range(t-1,-1,-1):
        if i-a >= 0:
            dp[i] = max(dp[i], dp[i-a] + b)
        else:
            break
    
    #print(dp)

print(max(dp))

Submission status: Wrong Answer

Solution code:
n, t = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    ab.append([int(i) for i in input().split()])
c = [0] * (t*2+1)
for i in ab:
    for j in range(t-1, -1, -1):
        k = c[j]
        l = i[0] + j+1
        if l >= 2*t+1:
            continue
        c[l] = max(c[l], c[j] + i[1])
print(max(c))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,T=map(int,input().split())
    A,B=[0]*n,[0]*n
    for i in range(n):
        A[i],B[i]=map(int,input().split())

    dp=[0]*T
    ans=-INF
    for i in range(n):
        a=A[i]; b=B[i]
        ndp=dp[:]
        for t in range(T):
            if(t+a<T):
                ndp[t+a]=max(ndp[t+a],dp[t]+b)
            else:
                ans=max(ans,dp[t]+b)
        dp=ndp
    ans=max(ans,max(dp))
    print(ans)
resolve()

Submission status: Wrong Answer

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

dp = [[0 for t_sum in range(T+3003)] for i in range(N+1)]

for i in range(N):
    for t_sum in range(T+1):

        # これは配るDP

        # i-1番目の品物を選ぶ場合
        # 重量的に入れられるなら入れてみる
        if t_sum < T:
            dp[i+1][t_sum+items[i][0]] = max(dp[i+1][t_sum+items[i][0]],
                                             dp[i][t_sum]+items[i][1])

        # i-1番目の品物を選ばない場合
        dp[i+1][t_sum] = max(dp[i+1][t_sum], dp[i][t_sum])

ans = 0
for t in range(T+3003):
    ans = max(ans, dp[N][t])
print(ans)

Submission status: Wrong Answer

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

n,t = map(int,input().split())
dg = 10**5

ab = []

for i in range(n):
    a,b = map(int,input().split())
    ab.append(b*dg+a)
ab.sort()

dp = [[0]*(t+1) for i in range(n)]

for i in range(n):
    a,b = ab[i]%dg,ab[i]//dg
    for j in range(min(a,t+1)):
        dp[i][j] = dp[i-1][j]
    for j in range(a,t+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-a]+b)


res = dp[n-1][t]
p = 0
q = 0
for k in range(n-1,0,-1):
    a,b = ab[k]%dg,ab[k]//dg
    q += b
    res = max(res,q+dp[k-1][t-p-1])
    p += a
    if p >= t:
        break
print(res)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

n, t = map(int, input().split())
knapsack = defaultdict(lambda: 0)
knapsack[0] = 0
tmp_ans = 0
for line in sys.stdin:
    a, b = map(int, line.split())
    for time, satisfied in list(knapsack.items()):
        if time + a >= t:
            tmp_ans = max(tmp_ans, satisfied + b)
            continue
        knapsack[time + a] = max(knapsack[time + a], satisfied + b)

print(max(tmp_ans, max(knapsack.values())))

Submission status: Wrong Answer

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

D = [[0] * (t+1) for i in range(2)]

for i in range(n):
    for j in range(t, -1, -1):
        D[1][j] = max(D[1][j], D[0][j] + A[i][1])
        if j-A[i][0] >= 0:
            
            D[0][j] = max(D[0][j], D[0][j-A[i][0]] + A[i][1])
        # else:
        #     # print(D)
        #     break

# print(D)
print(D[1][-2])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n, t = LI()
    ab = LIR(n)
    dp = [0] * (t+1)
    for i in range(n):
        a, b = ab[i]
        for ti in range(t-1,-1,-1):
            dp[min(ti + a, t)] = max(dp[min(ti + a,t)], dp[ti] + b)
    print(dp[t])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

D = [[0] * (t+1) for i in range(2)]

for i in range(n):
    for j in range(t, -1, -1):
        D[1][j] = max(D[1][j], D[0][j] + A[i][1])
        if j-A[i][0] >= 0:
            
            D[0][j] = max(D[0][j], D[0][j-A[i][0]] + A[i][1])
        else:
            # print(D)
            break

# print(D)
print(D[1][-2])

Submission status: Wrong Answer

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

N, T = map(int, input().split())
food = []
for i in range(N):
    a, b = map(int, input().split())
    food.append((a, b))

dp = [[0 for _ in range(6001)] for _ in range(N+1)]
for i in range(N):
    for t in range(T):
        a, b = food[i]
        dp[i+1][t] = max(dp[i+1][t], dp[i][t])
        dp[i+1][t+a] = max(dp[i+1][t+a], dp[i][t] + b)
    for t in range(T, 6001):
        dp[i + 1][t] = max(dp[i + 1][t], dp[i][t])

print(max(dp[-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
input = sys.stdin.readline
MOD = 10**9 + 7

ab = []
n, t = [int(item) for item in input().split()]
for i in range(n):
    a, b = [int(item) for item in input().split()]
    ab.append((a, b))
ab.sort()

dp = [[-1] * (t+1) for _ in range(n+1)]
dp[0][0] = 0
ans = 0
for i, (a, b) in enumerate(ab):
    for j in range(t-1, -1, -1):
        if dp[i][j] == -1:
            continue
        if a + j >= t:
            ans = max(ans, dp[i][j] + b)
            continue
        dp[i+1][j + a] = max(dp[i][j + a], dp[i][j] + b)
        ans = max(ans, dp[i+1][j + a])
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,T=map(int,input().split())
    A,B=[0]*n,[0]*n
    for i in range(n):
        A[i],B[i]=map(int,input().split())

    dp=[0]*T
    ans=-INF
    for i in range(n):
        a=A[i]; b=B[i]
        ndp=dp[:]
        for t in range(T):
            if(t+a<T):
                ndp[t+a]=max(ndp[t+a],dp[t]+b)
            else:
                ans=max(ans,dp[t]+b)
        dp=ndp
    print(ans)
resolve()

Submission status: Wrong Answer

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

N, T = map(int, input().split())
food = []
for i in range(N):
    a, b = map(int, input().split())
    food.append((a, b))

dp = [[0 for _ in range(6001)] for _ in range(N+1)]
for i in range(N):
    for t in range(T):
        a, b = food[i]
        dp[i+1][t] = max(dp[i+1][t], dp[i][t])
        dp[i+1][t+a] = max(dp[i+1][t+a], dp[i][t] + b)

print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500000)

# 枝刈り探索（分枝限定法）
class Knapsack:
    def __init__(self, VW):
        self.VW = VW
        self.VW.sort(key=lambda vw: vw[0] / vw[1], reverse=True)
        self.n = len(VW)

    def solve(self, capacity, ok=0):
        self.ok = ok
        self.capacity = capacity
        return self._dfs(0, 0, 0)

    def _dfs(self, i, v_now, w_now):
        if i == self.n:
            self.ok = max(self.ok, v_now)
            return v_now
        ng, f = self._solve_relaxation(i, self.capacity - w_now)
        ng += v_now
        if f:
            self.ok = max(self.ok, ng)
            return ng
        if ng < self.ok:
            return -float("inf")
        res = -float("inf")
        v, w = self.VW[i]
        if w_now + w <= self.capacity:
            res = max(res, self._dfs(i + 1, v_now + v, w_now + w))
        res = max(res, self._dfs(i + 1, v_now, w_now))
        return res

    def _solve_relaxation(self, i, capacity):
        res = 0
        f = True
        for v, w in self.VW[i:]:
            if capacity == 0:
                break
            if w <= capacity:
                capacity -= w
                res += v
            else:
                f = False
                res += v * (capacity / w)
                break
        return res, f


def main():
    N, T = map(int, input().split())
    VW = []
    ma = 0
    ama = -1
    for i in range(N):
        w, v = map(int, input().split())
        if ma < v:
            ma = v
            ama = i
        VW.append([v, w])
    del VW[ama]
    if N==1:
        print(ma)
        exit()
    knapsack = Knapsack(VW)
    print(knapsack.solve(T-1) + ma)


main()

Submission status: Wrong Answer

Solution code:
I = [int(_) for _ in open(0).read().split()]
N, T = I[:2]
A, B = I[2::2], I[3::2]
dp_start = [-1] * T
dp_end = [-1] * 6001
dp_start[0] = 0
for a, b in zip(A, B):
    dp_start_new = dp_start[:]
    dp_end_new = dp_end[:]
    for k in range(T):
        if dp_start[k] == -1:
            continue
        dp_end_new[k + a] = max(dp_end[k + a], dp_start[k] + b)
        if k + a < T:
            dp_start_new[k + a] = max(dp_start[k + a], dp_end_new[k + a])
    dp_start = dp_start_new
    dp_end = dp_end_new
print(max(dp_end))

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
A = [0] * T
B = [0] * T
for _ in range(N):
    a, b = map(int, input().split())
    for i in range(T)[::-1]:
        if i + a < T: A[i+a] = max(A[i+a], A[i] + b)
        B[i] = max(B[i], A[i] + b)
print(max(B))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
input = sys.stdin.readline
MOD = 10**9 + 7

ab = []
n, t = [int(item) for item in input().split()]
for i in range(n):
    a, b = [int(item) for item in input().split()]
    ab.append((a, b))
ab.sort()

dp = [[-1] * (t+1) for _ in range(n+1)]
dp[0][0] = 0
ans = 0
for i, (a, b) in enumerate(ab):
    for j in range(t-1, -1, -1):
        if dp[i][j] == -1:
            continue
        if a + j > t:
            ans = max(ans, dp[i][j] + b)
            continue
        dp[i+1][j + a] = max(dp[i][j + a], dp[i][j] + b)
        dp[i+1][j] = max(dp[i][j], dp[i][j])
        ans = max(ans, dp[i+1][j + a])

print(ans)

Submission status: Wrong Answer

Solution code:
n, t = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    ab.append([int(i) for i in input().split()])
c = [0] * (t*2)
for i in ab:
    for j in range(t-1, -1, -1):
        k = c[j]
        l = i[0] + j
        if l >= 2*t:
            continue
        c[l] = max(c[l], c[j] + i[1])
print(max(c))

Submission status: Wrong Answer

Solution code:
import numpy as np
n,t = map(int, input().split())
DP = np.zeros((t))
ans = 0
for i in range(n):
    a,b = map(int, input().split())
    ans = max(ans, max(DP)+b)
    if a<t:
        DP[a:] = np.maximum(DP[a:], DP[:-a]+b)
ans = int(ans)
print(ans)

Submission status: Wrong Answer

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

def main():
    n,t=map(int,input().split())
    AB=[list(map(int,input().split())) for _ in range(n)]
    DP=[[0]*(6001) for _ in range(n+1)]
    for i,(a,b) in enumerate(AB):
        for j in range(6001):
            if j<a:
                DP[i+1][j]=DP[i][j]
            elif j-a<t:
                DP[i+1][j]=max(DP[i][j],DP[i][j-a]+b)
            else:
                DP[i+1][j]=DP[i][j]
    print(max(DP[-1]))
    
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

#a = list(map(int, input().split()))

n,T = list(map(int, input().split()))
item=[]

for i in range(n):
    item.append(list(map(int, input().split())))
    
dp = [[-1 for i in range(T+1)] for j in range(n+1)]

dp[0][0]=0

for i in range(n):
    nowit=item[i]
    for j in range(T):
        z = dp[i][j]
        y = dp[i+1][min(j+nowit[0],T)]
        down = dp[i+1][j]
        dp[i+1][j] = max(dp[i][j],down)
        if(z >= 0):
            dp[i+1][min(j+nowit[0],T)] = max(z+nowit[1],y)
    dp[i+1][T] = max(dp[i][T],dp[i+1][T])
            
print(max(dp[n]))

Submission status: Time Limit Exceeded

Solution code:
N,T=[int(s) for s in input().split()]
food=[[int(s) for s in input().split()] for i in range(N)]
food.sort()

#i番目までの料理でt秒以内に食べきるときのDP
DP=[[0 for t in range(T)] for i in range(N)]
for i in range(N):
  time,point=food[i]
  for t in range(min([T,time])):
    DP[i][t]=DP[i-1][t]
  for t in range(time,T):
    DP[i][t]=max([DP[i-1][t] , DP[i-1][t-time]+point])

#i番目の料理を最後に注文することを考える
ls=[DP[i-1][T-1]+food[i][1] for i in range(1,N)]
ls.append(food[0][1])
print(max(ls))

Submission status: Time Limit Exceeded

Solution code:
#all you can eat

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

dp = [[0]*(3010) for i in range(3010)]

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

rev_dp = [[0]*(3010) for i in range(3010)]
food.reverse()

for i in range(N):
    time, value = food[i]
    for j in range(T):
        if j >= time:
            rev_dp[i+1][j] = max(rev_dp[i][j], rev_dp[i][j-time] + value)
        else:
            rev_dp[i+1][j] = rev_dp[i][j]
    dp[i][j] = max(dp[i][j], dp[i][j-1])


ans = 0
for x in range(N):
    ans = max(ans, max(dp[N-1-x][t] + rev_dp[x][T-1-t]
                       for t in range(T))+food[x][1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
for i in range(3000):
  for j in range(3000):
    print(i + j)

Submission status: Time Limit Exceeded

Solution code:
N, T = map(int, input().split())
food = [0] * N
for i in range(N):
  food[i] = list(map(int, input().split()))

food = sorted(food)  
#i番目までの商品でj分いないに完食できる美味しさの最大値  
dp = [[0] * (T + 1) for i in range(N + 1)]  

for i in range(N):
  for j in range(1, T + 1):
    #i番目の商品を使わない場合
    now = max(dp[i + 1][j - 1], dp[i][j])
    #使う場合
    if (j - food[i][0]) >= 0:
      now2 = dp[i][j - food[i][0]] + food[i][1]
    else:
      now2 = 0
    dp[i + 1][j] = max(now, now2)
    
#print(dp)
#T - 1分に i + 1番目の商品を注文
ans = 0
for i in range(N):
  now3 = dp[i][T - 1] + food[i][1]
  #print(dp[i][T - 1], food[i][1])
  ans = max(ans, now3)
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = 10**6#float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
#from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, T = MAP()
AB = [LIST() for _ in range(N)]
AB.sort(key = lambda x:x[0])
t = [0]*T
ans = 0

for A, B in AB:
	for i in range(T-1, 0, -1):
		if t[i]:
			if T <= i+A:
				ans = max(ans, t[i]+B)
			else:
				t[i+A] = max(t[i+A], t[i]+B)
	if T <= A:
		ans = max(ans, B)
	else:
		t[A] = max(t[A], B)

ans = max(ans, max(t))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *AB = map(int, read().split())
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]

    D.sort()

    dp = [0] * T
    ans = 0

    for i in range(N - 1):
        for t in range(T - 1, -1, -1):
            if t - D[i][0] >= 0 and dp[t] < dp[t - D[i][0]] + D[i][1]:
                dp[t] = dp[t - D[i][0]] + D[i][1]
        if ans < dp[T - 1] + D[i + 1][1]:
            ans = dp[T - 1] + D[i + 1][1]

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *AB = map(int, read().split())
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]

    D.sort()

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

    for i in range(N):
        for t in range(T):
            if t - D[i][0] >= 0:
                dp[i + 1][t] = dp[i][t - D[i][0]] + D[i][1]
            if dp[i + 1][t] < dp[i][t]:
                dp[i + 1][t] = dp[i][t]

    ans = 0
    for i in range(N):
        if ans < dp[i][T - 1] + D[i][1]:
            ans = dp[i][T - 1] + D[i][1]

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys 
sys.setrecursionlimit(10**7)
N, T = map(int, input().split())
A = []
for _ in range(N):
  a, b = map(int,input().split())
  A.append((a, b))
A.sort(key=lambda x: x[1], reverse=True)
A.sort(key=lambda x: x[0])
ans = []
max_point = [-1 for _ in range(T)]
def solve(point, x, unvisit_list):
  if x >= T or len(unvisit_list) == 0:
    ans.append(point)
    return 0
  elif max_point[x] >= point:
    return 0
  else:
    max_point[x] = point
    for k in range(len(unvisit_list)):
      new_unvisit = unvisit_list[:k] + unvisit_list[k+1:]
      if x + unvisit_list[k][0] >= T:
        ans.append(point+max(unvisit_list, key=lambda x: x[1])[1])
        return 0
      solve(point+unvisit_list[k][1], x + unvisit_list[k][0], new_unvisit)
    return 0

while A:
  item = A[0]
  del A[0]
  solve(item[1], item[0], A)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys 
sys.setrecursionlimit(10**7)
N, T = map(int, input().split())
A = []
for _ in range(N):
  a, b = map(int,input().split())
  A.append((a, b))
A.sort(key=lambda x: x[1], reverse=True)
A.sort(key=lambda x: x[0])
ans = []
max_point = [-1 for _ in range(T)]
def solve(point, x, unvisit_list):
  if x >= T or len(unvisit_list) == 0:
    ans.append(point)
    return 0
  elif max_point[x] > point:
    return 0
  else:
    max_point[x] = point
    for k in range(len(unvisit_list)):
      new_unvisit = unvisit_list[:k] + unvisit_list[k+1:]
      solve(point+unvisit_list[k][1], x + unvisit_list[k][0], new_unvisit)
    return 0

while A:
  item = A[0]
  del A[0]
  solve(item[1], item[0], A)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys 
sys.setrecursionlimit(10**7)
N, T = map(int, input().split())
A = []
for _ in range(N):
  a, b = map(int,input().split())
  A.append((a, b))
A.sort(key=lambda x: x[1], reverse=True)
A.sort(key=lambda x: x[0])
ans = []
max_point = [-1 for _ in range(T)]
def solve(point, x, unvisit_list):
  if x >= T or len(unvisit_list) == 0:
    ans.append(point)
    return 0
  elif max_point[x] > point:
    return 0
  else:
    max_point[x] = point
    for k in range(len(unvisit_list)):
      new_unvisit = unvisit_list[:k] + unvisit_list[k+1:]
      solve(point+unvisit_list[k][1], x + unvisit_list[k][0], new_unvisit)

while A:
  item = A[0]
  del A[0]
  solve(item[1], item[0], A)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n, t = map(int, input().split())
dp1 = [[-float("inf") for i in range(t)] for i in range(n + 1)]
dp1[0][0] = 0
dp2 = [[-float("inf") for i in range(t)] for i in range(n + 1)]
dp2[n][0] = 0
ab = []
for i in range(n):
    ab.append(list(map(int, input().split())))
for i in range(n):
    a, b = ab[i]
    for j in range(t - 1, -1, -1):
        if j >= a:
            dp1[i + 1][j] = max(dp1[i][j], dp1[i][j - a] + b)
        else:
            dp1[i + 1][j] = dp1[i][j]
for i in range(n - 1, -1, -1):
    a, b = ab[i]
    for j in range(t - 1, -1, -1):
        if j >= a:
            dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - a] + b)
        else:
            dp2[i][j] = dp2[i + 1][j]
ans = [max([max(dp1[i][:j + 1]) + max(dp2[i + 1][:t - j]) + ab[i][1]]) for j in range(t) for i in range(n)]
print(max(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, W = map(int, input().split())
ab = [tuple(map(int, input().split()))for _ in range(n)]
ab.sort(reverse=True)

dp = [0]*W
for w, v in ab:
    for j in reversed(range(W)):
        if j-w < 0:
            break
        if dp[j] < dp[j-w]+v:
            dp[j] = dp[j-w]+v
    if dp[0] < v:
        dp[0] = v
print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

#######################################################################
# ナップサック
# 重さ：Wー10**5くらい
# 価値：N*max_Viー10**11くらい
# dp[w]:wの重さで可能な最大の価値
#   dp[w]とdp[w-wi]+vi(i番目のアイテムをとる場合)との大きいほうで更新
#######################################################################

n, t = map(int, input().split())
dp = [0] * (t+1)
itm = [list(map(int, input().split())) for _ in range(n)]
itm.sort()
for w_ , v_ in itm:
    for i in range(1,t+1):
        dp[max(0,i-w_)] = max(dp[max(0,i-w_)], dp[i] + v_)
print(dp[0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

#@njit((i8,i8,i8[:]), cache=True)
def main(N,T,A):
  dp = np.zeros(T+1, np.int64)
  for i in range(N):
    for t in range(T-1,-1,-1):
      u = min(T,t+A[i,0])
      dp[u] = max(dp[u],dp[t]+A[i,1])
  ans = dp[-1]
  return ans

N, T = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
A = np.array(sorted(A))
print(main(N,T,A))

Submission status: Time Limit Exceeded

Solution code:
n,t=map(int,input().split())
dish=[list(map(int,input().split())) for i in range(n)]
dish.sort()
dp=[0]*6001
for i in range(n):
    a,b=dish[i]
    for j in range(t-1,-1,-1):
        dp[j+a]=max(dp[j+a],dp[j]+b)
print(max(dp))