Problem:


Score : 500 points

Problem Statement

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

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

The restaurant has the following rules:

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

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

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

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

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

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

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

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

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

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

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

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

Sample Input 4

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

Sample Output 4

145



Sample input:
2 60
10 10
100 100


Sample output:
110

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())

def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
      else: #入る可能性はない
        dp[i+1][w]=dp[i][w]
  return dp[N][W]


def main():
	n, t = map(int, input().split())
	AB=[]
	A=[]
	B=[]
	for _ in range(n):
		a,b = map(int, input().split())
		AB.append((b,a))
	
	AB.sort(key=lambda x:x[1])
	AB.sort()
	for i in range(n-1):
		A.append(AB[i][1])
		B.append(AB[i][0])
	kknapsack = knapsack(n-1,t,A,B)
	ans = kknapsack  + AB[-1][0]
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys


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


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
import copy
def knapsack(N,W,weight,value):
  dp=[[999999999999 for i in range(W+1)] for j in range(N+1)]
  dp2=[[0 for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0
 
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w:
        if dp[i][w-weight[i]]+value[i]>dp[i][w]:
          dp[i+1][w]=dp[i][w-weight[i]]+value[i]
          dp2[i+1][w]=w-weight[i]
        else:
          dp[i+1][w]=dp[i][w]
          dp2[i+1][w]=w
        
      else:
        dp[i+1][w]=dp[i][w]
        dp2[i+1][w]=w
  
  l=[]
  x=W
  for i in range(n-1,-1,-1):
    if dp2[i+1][x]==x-weight[i]:
      l.append(i)
    x=dp2[i+1][x]
  return dp[N][W],l
 
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
a=[];b=[]
for i in range(n):
  a.append(l[i][0])
  b.append(l[i][1])
z=knapsack(n,t-1,a,b)
L=[0]*n
for i in range(len(z[1])):
  L[z[1][i]]=1
M=[]
for i in range(n):
  if L[i]==0:
    M.append(l[i][1])
if len(M)==0:
  print(z[0])
else:
  print(z[0]+max(M))

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())

def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
      else: #入る可能性はない
        dp[i+1][w]=dp[i][w]
  return dp[N][W]


def main():
	n, t = map(int, input().split())
	AB=[]
	A=[]
	B=[]
	for _ in range(n):
		a,b = map(int, input().split())
		AB.append((b,a))
	
	AB.sort(key=lambda x:x[1])
	AB.sort()
	for i in range(n-1):
		A.append(AB[i][1])
		B.append(AB[i][0])
	kknapsack = knapsack(n-1,t-1,A,B)
	ans = kknapsack  + AB[-1][0]
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
N,T = map(int, input().split())
L = [[int(l) for l in input().split()] for _ in range(N)]
 
dp = [[0]*T for _ in range(N+1)]
 
for i in range(N):
    for j in range(T):
        if j < L[i][0]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j], dp[i][j-L[i][0]]+L[i][1])
            
used = []
B = dp[N][T-1]
j = T-1
for i in range(N-1, -1, -1):
    if B == dp[i][j]:
        continue
    B -= L[i][1]
    used.append(i)
    j -= L[i][0]
        
ans = dp[N][T-1]
c = 0
for i in range(N):
    if i in used:
        continue
    c = max(c, L[i][1])
    
print(ans+c)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())

def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
      else: #入る可能性はない
        dp[i+1][w]=dp[i][w]
  return dp[N][W]


def main():
	n, t = map(int, input().split())
	AB=[]
	A=[]
	B=[]
	for _ in range(n):
		a,b = map(int, input().split())
		AB.append((b,a))
	
	AB.sort(key=lambda x:x[1],reverse=True)
	AB.sort()
	for i in range(n-1):
		A.append(AB[i][1])
		B.append(AB[i][0])
	kknapsack = knapsack(n-1,t-1,A,B)
	ans = kknapsack  + AB[-1][0]
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())

def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
      else: #入る可能性はない
        dp[i+1][w]=dp[i][w]
  return dp[N][W]


def main():
	n, t = map(int, input().split())
	AB=[]
	A=[]
	B=[]
	for _ in range(n):
		a,b = map(int, input().split())
		AB.append((b,a))
	
	AB.sort(key=lambda x:x[1],reverse=True)
	AB.sort()
	print(AB)
	for i in range(n-1):
		A.append(AB[i][1])
		B.append(AB[i][0])
	kknapsack = knapsack(n-1,t-1,A,B)
	ans = kknapsack  + AB[-1][0]
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
# from bisect import *
# from collections import *
# from heapq import *
# import functools
# import itertools
# import math
import operator
INF = float('inf')
MOD = 10**9+7

class SegmentTree:

    def __init__(self, seq, default, op = operator.add):
        self.size = 2**len(seq).bit_length()
        self.dat = [default]*(self.size*2)
        self.op = op
        for i in range(len(seq)):
            self.update(i, seq[i])

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.op(self.dat[i+i], self.dat[i+i+1])

    def add(self, i, x):
        i += self.size
        self.dat[i] = self.op(self.dat[i], x)
        while i > 0:
            i >>= 1
            self.dat[i] = self.op(self.dat[i], x)

    def get(self, a, b = None):
        if b is None:
            b = a+1
        l, r = a+self.size, b+self.size
        res = None
        while l < r:
            if l & 1:
                if res is None:
                    res = self.dat[l]
                else:
                    res = self.op(res, self.dat[l])
                l += 1
            if r & 1:
                r -= 1
                if res is None:
                    res = self.dat[r]
                else:
                    res = self.op(res, self.dat[r])
            l >>= 1
            r >>= 1
        return res

N, T = map(int, input().split())
AB = [None] * N
for i in range(N):
    AB[i] = list(map(int, input().split()))

dp1 = [[-INF]*(T+1) for i in range(N+1)]
dp1[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j and dp1[i][j-A]+B > dp1[i][j]:
            dp1[i+1][j] = dp1[i][j-A]+B
        else:
            dp1[i+1][j] = dp1[i][j]

AB = AB[::-1]
dp2 = [[-INF]*(T+1) for i in range(N+1)]
dp2[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j and dp2[i][j-A]+B > dp2[i][j]:
            dp2[i+1][j] = dp2[i][j-A]+B
        else:
            dp2[i+1][j] = dp2[i][j]

ans = max(dp1[N])

_ans = 0
B = AB[-1][1]
_ans = max(dp2[N-1][:T])+B
if _ans > ans:
    ans = _ans

_ans = 0
B = AB[0][1]
_ans = max(dp1[N-1][:T])+B
if _ans > ans:
    ans = _ans

for i in range(1, N-1):
    A, B = AB[i]
    seg = SegmentTree(dp2[N-i-2],  -INF, max)
    for j in range(T):
        tmp = dp1[i-1][j]+seg.get(0, T-j)+B
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())

def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
      else: #入る可能性はない
        dp[i+1][w]=dp[i][w]
  return dp[N][W]


def main():
	n, t = map(int, input().split())
	AB=[]
	A=[]
	B=[]
	for _ in range(n):
		a,b = map(int, input().split())
		AB.append((b,a))
	AB.sort()
	for i in range(n-1):
		A.append(AB[i][1])
		B.append(AB[i][0])
	kknapsack = knapsack(n-1,t-1,A,B)
	ans = kknapsack  + AB[-1][0]
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
V = []
W = []
for n in range(N):
    w, v = map(int, input().split())
    V.append(v)
    W.append(w)

max_V = max(V)
argmax_V = V.index(max(V))

V.pop(argmax_V)
W.pop(argmax_V)

N = N - 1
T = T - 1

# 以下、Wの合計がTを超えない範囲でVの合計を最大化する問題を考える

dp = [[0 for t in range(T + 1)] for n in range(N + 1)]
# dp[n][t] = (N=n, T=tとしたときのこの問題の答え)

for n in range(N):
    for t in range(T+1):
        if W[n] > t:
            dp[n+1][t] = dp[n][t]
        else:
            dp[n+1][t] = max(dp[n][t], dp[n][t-W[n]]+V[n])

print(dp[N][T]+max_V)

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
food = list(list(map(int,input().split()))for i in range(N))
DP = [[0]*(T) for i in range(N+1)]
for i in range(N):
    a,b = food[i]
    for j in range(min(a,T)):
        DP[i+1][j] = DP[i][j]
    for j in range(a,T):
        DP[i+1][j] = max(DP[i][j],DP[i][j-a]+b)
ans = 0
i = N
j = T-1
while i > 0 and j > 0:
    if DP[i][j] == DP[i][j-1]:
        j -= 1
    elif DP[i][j] == DP[i-1][j]:
        i -= 1
    else:
        food[i-1][1] = 0
        j -= food[i-1][0]
        i -= 1
food.sort(reverse=True,key= lambda x:x[1])
print(DP[-1][-1]+food[0][1])

Submission status: Wrong Answer

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

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

dp = [[0 for _ in range(N+1)] for _ in range(T+1)]
# 重さwまで品物nまででで最大になる価値

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

for n in range(N): # 品物nまでの世界で決めていく
    a, b = AB[n]
    for w in range(T):
        if a > w: # カバンに入りきらないなら入れられない
            dp[w][n+1] = dp[w][n]
            prev[w][n+1] = w
        else: #カバンに入れられる可能性があるなら
            if b+dp[w-a][n] >= dp[w][n]:
                dp[w][n+1] = dp[w-a][n] + b
                prev[w][n+1] = w - a
            else:
                dp[w][n+1] = dp[w][n]
                prev[w][n+1] = w

ans = dp[T-1][N]
used = [False]*N
c = T-1
for i in reversed(range(N)):
    if prev[c][i+1] == c - AB[i][0]:
        used[i] = True
    c = prev[c][i+1]

p = 0
for i in range(N):
    if not used[i] and AB[i][1] > p:
        p = AB[i][1]

print(ans+p)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
# from bisect import *
# from collections import *
# from heapq import *
# import functools
# import itertools
# import math
import operator
INF = float('inf')
MOD = 10**9+7

class SegmentTree:

    def __init__(self, seq, default, op = operator.add):
        self.size = 2**len(seq).bit_length()
        self.dat = [default]*(self.size*2)
        self.op = op
        for i in range(len(seq)):
            self.update(i, seq[i])

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.op(self.dat[i+i], self.dat[i+i+1])

    def add(self, i, x):
        i += self.size
        self.dat[i] = self.op(self.dat[i], x)
        while i > 0:
            i >>= 1
            self.dat[i] = self.op(self.dat[i], x)

    def get(self, a, b = None):
        if b is None:
            b = a+1
        l, r = a+self.size, b+self.size
        res = None
        while l < r:
            if l & 1:
                if res is None:
                    res = self.dat[i]
                else:
                    res = self.op(res, self.dat[i])
                i += 1
            if r & 1:
                r -= 1
                if res is None:
                    res = self.dat[r]
                else:
                    res = self.op(res, self.dat[r])
            l >>= 1
            r >>= 1
        return res

N, T = map(int, input().split())
AB = [None] * N
for i in range(N):
    AB[i] = list(map(int, input().split()))
dp1 = [[-INF]*(T+1) for i in range(N+1)]
dp1[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j and dp1[i][j-A]+B > dp1[i][j]:
            dp1[i+1][j] = dp1[i][j-A]+B
        else:
            dp1[i+1][j] = dp1[i][j]

AB = AB[::-1]
dp2 = [[-INF]*(T+1) for i in range(N+1)]
dp2[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j and dp2[i][j-A]+B > dp2[i][j]:
            dp2[i+1][j] = dp2[i][j-A]+B
        else:
            dp2[i+1][j] = dp2[i][j]

ans = max(dp1[N])

_ans = 0
B = AB[-1][1]
_ans = max(dp2[N-1][:T])+B
if _ans > ans:
    ans = _ans

_ans = 0
B = AB[0][1]
_ans = max(dp1[N-1][:T])+B
if _ans > ans:
    ans = _ans

for i in range(1, N-1):
    A, B = AB[i]
    seg = SegmentTree(dp2[N-i-2],  -INF, max)
    for j in range(T):
        tmp = dp1[i-1][j]+seg.get(0, T-j)+B
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
#
import sys
import math
import numpy as np
import itertools
# いくつか入力
n,t = (int(i) for i in input().split())

# n行の複数列ある数値を二次元配列へ。x[n][?]
ab = [[int(i) for i in input().split()] for i in range(n)]
# 0:weight 1:value

ab = sorted(ab,key=lambda x: x[1])

dp = np.zeros((n+1,t),int)
dp2 = np.zeros((n+1,t),int)

for i in range(n):
    for w in range(t):
        if w >= ab[i][0] :
            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]
        if w >= ab[i][0] :
            dp2[i+1][w] = max(dp2[i][w-ab[i][0]] + ab[i][1],dp[i][w] + ab[i][1], dp[i][w])
        else:
            dp2[i+1][w] = max(dp[i][w] + ab[i][1], dp[i][w])

answer =0
#for i in range(n+1):
#    for w in range(t):
#        answer = np.max(answer, dp2[i][w])
print(answer)

#print(dp2)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


# 枝刈り探索（分枝限定法）

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())
	AB=[]
	for _ in range(n):
		A,B = map(int, input().split())
		AB.append((B,A))
	AB.sort()
	knapsack = Knapsack(AB[:-1])
	ans = knapsack.solve(t-1) + AB[-1][0]
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys


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


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
import itertools
INF = float('inf')
MOD = 10**9+7

N, T = map(int, input().split())
AB = [None] * N
for i in range(N):
    AB[i] = list(map(int, input().split()))

dp1 = [[-INF]*(T+1) for i in range(N+1)]
dp1[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j:
            tmp = dp1[i][j-A]+B
            if dp1[i][j] < tmp:
                dp1[i+1][j] = tmp
            else:
                dp1[i+1][j] = dp1[i][j]
        else:
            dp1[i+1][j] = dp1[i][j]

AB = AB[::-1]
dp2 = [[-INF]*(T+1) for i in range(N+1)]
dp2[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j:
            tmp = dp2[i][j-A]+B
            if dp2[i][j] < tmp:
                dp2[i+1][j] = tmp
            else:
                dp2[i+1][j] = dp2[i][j]
        else:
            dp2[i+1][j] = dp2[i][j]

for i in range(N+1):
    for j in range(1, T+1):
        if dp1[i][j] < dp1[i][j-1]:
            dp1[i][j] = dp1[i][j-1]

for i in range(N+1):
    for j in range(1, T+1):
        if dp2[i][j] < dp2[i][j-1]:
            dp2[i][j] = dp2[i][j-1]

AB = AB[::-1]
ans = max(dp1[N][T], dp1[N-1][T-1]+AB[-1][1], dp2[N-1][T-1]+AB[0][1])
for i in range(1, N):
    B = AB[i][1]
    for j in range(T):
        tmp = dp1[i-1][j]+B+dp2[N-i-1][T-j-1]
        if ans < tmp:
            ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


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


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
import itertools
INF = float('inf')
MOD = 10**9+7

N, T = map(int, input().split())
AB = [None] * N
for i in range(N):
    AB[i] = list(map(int, input().split()))

dp1 = [[-INF]*(T+1) for i in range(N+1)]
dp1[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j:
            tmp = dp1[i][j-A]+B
            if dp1[i][j] < tmp:
                dp1[i+1][j] = tmp
            else:
                dp1[i+1][j] = dp1[i][j]
        else:
            dp1[i+1][j] = dp1[i][j]

AB = AB[::-1]
dp2 = [[-INF]*(T+1) for i in range(N+1)]
dp2[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j:
            tmp = dp2[i][j-A]+B
            if dp2[i][j] < tmp:
                dp2[i+1][j] = tmp
            else:
                dp2[i+1][j] = dp2[i][j]
        else:
            dp2[i+1][j] = dp2[i][j]

for i in range(N+1):
    for j in range(1, T+1):
        if dp1[i][j] < dp1[i][j-1]:
            dp1[i][j] = dp1[i][j-1]

for i in range(N+1):
    for j in range(1, T+1):
        if dp2[i][j] < dp2[i][j-1]:
            dp2[i][j] = dp2[i][j-1]

AB = AB[::-1]
ans = max(dp1[N][T], dp1[N-1][T-1]+AB[-1][1], dp2[N-1][T-1]+AB[0][1])
for i in range(1, N-1):
    B = AB[i][1]
    for j in range(T):
        tmp = dp1[i-1][j]+B+dp2[N-i-1][T-j-1]
        if ans < tmp:
            ans = tmp
print(ans)

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

Submission status: Wrong Answer

Solution code:
import sys


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


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

D.sort()

dp = [[0] * (T-1) for _ in range(N)]
for i in range(1, N):
    a, b = D[i-1]
    _ , nb = D[i]
    b = -b
    nb = -nb
    for t in range(T-1):
        dp[i][t] = dp[i-1][t]
        if t >= a:
            dp[i][t] = max(dp[i][t], dp[i-1][t-a]+b)
    # dp[i][-1]

print(max(-D[i][1] + dp[i][-1] for i in range(1,N)))

Submission status: Wrong Answer

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

dp = [-1 for _ in range(6000)]
dp[0] = 0
dp2 = dp[:]

for i in range(n):
    dp_trans = ab[i][0]
    dp_delicious = ab[i][1]
    for j in range(t):
        if dp[j] != -1:
            dp2[j+dp_trans] = max(dp[j+dp_trans], dp[j] + dp_delicious)
    dp = dp2[:]

print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
import itertools
INF = float('inf')
MOD = 10**9+7

N, T = map(int, input().split())
AB = [None] * N
for i in range(N):
    AB[i] = list(map(int, input().split()))

dp1 = [[-INF]*(T+1) for i in range(N+1)]
dp1[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j:
            tmp = dp1[i][j-A]+B
            if dp1[i][j] < tmp:
                dp1[i+1][j] = tmp
            else:
                dp1[i+1][j] = dp1[i][j]
        else:
            dp1[i+1][j] = dp1[i][j]

AB = AB[::-1]
dp2 = [[-INF]*(T+1) for i in range(N+1)]
dp2[0][0] = 0
for i in range(N):
    A, B = AB[i]
    for j in range(T+1):
        if A <= j:
            tmp = dp2[i][j-A]+B
            if dp2[i][j] < tmp:
                dp2[i+1][j] = tmp
            else:
                dp2[i+1][j] = dp2[i][j]
        else:
            dp2[i+1][j] = dp2[i][j]

for i in range(N):
    for j in range(1, T+1):
        if dp1[i][j] < dp1[i][j-1]:
            dp1[i][j] = dp1[i][j-1]

for i in range(N):
    for j in range(1, T+1):
        if dp2[i][j] < dp2[i][j-1]:
            dp2[i][j] = dp2[i][j-1]


AB = AB[::-1]
ans = max(dp1[N][T], dp1[N-1][T-1]+AB[-1][1], dp2[N-1][T-1]+AB[0][1])
for i in range(1, N-1):
    B = AB[i][1]
    for j in range(T):
        tmp = dp1[i-1][j]+B+dp2[N-i-2][T-j-1]
        if ans < tmp:
            ans = tmp
print(ans)

Submission status: Wrong Answer

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

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

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

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

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