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 = map(int,input().split())
now = [0 for _ in range(t+1)]
for _ in range(n):
  w,v = map(int,input().split())
  last = now
  now = [0]
  for f in range(1,t):
    if f - w >= 0:
      if last[f-w] + v > last[f]:
        now.append(last[f-w] + v)
      else:
        now.append(last[f])
    else:
      now.append(last[f])
  now.append(max(last[-2]+v,last[-1]))
print(now[-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import  numpy as np

def solve():
    N, T = map(int, input().split())
    C = [tuple(map(int, input().split())) for _ in range(N)]
    C.sort(key=lambda c: c[0]/c[1])
    dp = np.zeros((N+1,T+1), dtype=int)
    t = np.arange(T)
    for i,(a,b) in enumerate(C, start=1):
        s = np.minimum(T, t+a)
        dp[i,s] = np.maximum(dp[:i,t]+b, dp[:i,s]).max(axis=0)
    return dp[N,T]

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


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


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


def read_str_n():
    return list(map(str, input().split()))


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


def mt(f):
    import time

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

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

    return wrap



@mt
def slv(N, T, AB):
    AB.sort(key=lambda x: x[1], reverse=True)
    # AB.sort(key=lambda x: x[0])
    dp = [[0] * (N+1) for _ in range(T)]

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


    # for t, r in enumerate(dp):
    #     print(t, r)
    
    ans = 0
    B = list(map(lambda x: x[1], AB))
    B.append(0)
    for i, (a, b) in enumerate(AB):
        ans = max(ans, dp[-1][i]+max(B[i+1:]))
    return ans


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a)]
c.sort()
DP=[[0 for j in range(b+1)]for i in range(a)]
for i in range(c[0][0],b+1):
  DP[0][i]+=c[0][1]
for i in range(1,a):
  for j in range(c[i][0],b):
    DP[i][j]=max(DP[i-1][j-c[i][0]]+c[i][1],DP[i-1][j])
  DP[i][b]=max(DP[i-1][b-1]+c[i][1],DP[i-1][b])
ans=DP[-1][-1]

c.sort(reverse=True)
DP=[[0 for j in range(b+1)]for i in range(a)]
for i in range(c[0][0],b+1):
  DP[0][i]+=c[0][1]
for i in range(1,a):
  for j in range(c[i][0],b):
    DP[i][j]=max(DP[i-1][j-c[i][0]]+c[i][1],DP[i-1][j])
  DP[i][b]=max(DP[i-1][b-1]+c[i][1],DP[i-1][b])
print(max(ans,DP[-1][-1]))

Submission status: Wrong Answer

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

n,w = inpl()
wv = [inpl() for i in range(n)]
dp = [[0 for i in range(w+1)] for j in range(n+1)]
wv.sort()
wv.sort(key=lambda x:x[1])
ww = [0] * n
vv = [0] * n
for i in range(n):
    ww[i],vv[i] = wv[i][0], wv[i][1]
for i in range(n-1):
    for j in range(w+1):
        if j >= ww[i]:
            dp[i+1][j] = max(dp[i][j], dp[i][j-ww[i]]+vv[i])
        else:
            dp[i+1][j] = dp[i][j]
res = dp[n-1][w-1]+wv[n-1][1]

dp = [[0 for i in range(w+1)] for j in range(n+1)]
wv.sort()
ww = [0] * n
vv = [0] * n
for i in range(n):
    ww[i],vv[i] = wv[i][0], wv[i][1]

if sum(ww[:n-1]) < w:
    print(sum(vv))
    quit()
for i in range(n-1):
    for j in range(w+1):
        if j >= ww[i]:
            dp[i+1][j] = max(dp[i][j], dp[i][j-ww[i]]+vv[i])
        else:
            dp[i+1][j] = dp[i][j]
print(max(res,dp[n-1][w-1]+wv[n-1][1]))

Submission status: Wrong Answer

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

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

for i in range(n):
    for j in range(t+1):
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        if j + dish[i][0] < t:
            dp[i+1][j+dish[i][0]] = max(dp[i+1]
                                        [j+dish[i][0]], dp[i][j] + dish[i][1])
        if i == n-1:
            dp[i+1][j] = max(dp[i]) + dish[i][1]
print(dp[n][t])

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l=sorted(l, key=lambda x: x[1])
l=l[::-1]
#print(l)
dp=[[0]*(t+1) for i in range(n+1)]
q=[0]
for i in range(1,n+1):
    for j in range(t+1):
        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]
    if dp[i][-1]==dp[i-1][-1]:
        #print(l[i])
        q.append(l[i-1][1])
q.sort()
#print(dp[-1])
#print(q)
print(max(dp[-1][-2]+q[-1],dp[-1][-1]))

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
INF = float('inf')
MOD = 10**9+7

class SegmentTree:

    def __init__(self, seq, _INF = -INF):
        self.N, N = 1, len(seq)
        while self.N < N:
            self.N *= 2
        self.data = [_INF for i in range(self.N*2-1)]
        self.seq = seq
        for k in range(N):
            self.update(k, seq[k])

    def update(self, k, a):
        self.seq[k] = a
        k += self.N-1
        self.data[k] = a
        while k > 0:
            k = (k-1)//2
            self.data[k] = max(self.data[k*2+1], self.data[k*2+2])

    def rmq(self, a, b, k=0, l=0, r=-1):
        if r == -1: r = self.N
        if r <= a or b <= l:
            return -INF
        if a <= l and r <= b:
            return self.data[k]
        vl = self.rmq(a, b, k*2+1, l, (l+r)//2)
        vr = self.rmq(a, b, k*2+2, (l+r)//2, r)
        return max(vl, vr)

N, T = map(int, input().split())
AB = [None] * N
for i in range(N):
    AB[i] = tuple(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]
seg = SegmentTree(dp2[N-1])
_ans = seg.rmq(0, T)+B
if _ans > ans:
    ans = _ans

_ans = 0
B = AB[0][1]
seg = SegmentTree(dp1[N-1])
_ans = seg.rmq(0, T)+B
if _ans > ans:
    ans = _ans

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

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)]

for i in range(T+1):
    dp[0][i]=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]))

#for i in range(len(dp)):
    #print(dp[i])

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())

a = [0]*3333
b = [0]*3333
dp = [0]*3333


for i in range(1, n+1):
	a[i], b[i] = map(int, input().split())

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

#print(dp)
print(max(dp))

Submission status: Wrong Answer

Solution code:
import math
N,T=[int(s) for s in input().split()]
ls=[[int(s) for s in input().split()] for i in range(N)]
M=max(ls, key=lambda x: x[0])
M=M[0]
dp=[[0,0,i] for i in range(T+M)]
for i in range(N):
  a,b=ls[i]
  for t in range(T+M-a):
    s=T+M-a-t-1
    x=dp[s][0]+b
    if dp[s+a][0]<x:
      dp[s+a][0]=x
      dp[s+a][1]=max([a,dp[s][1]])
result=[e[0] for e in dp if e[2]-e[1]<T]
#print(dp)
print(max(result))

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)]
  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]
        else:
          dp[i+1][w]=dp[i][w]
      else:
        dp[i+1][w]=dp[i][w]
  return dp[N][W]

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])
print(knapsack(n,t+max(a)-1,a,b))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

n,t = map(int,input().split())

abl = []
for _ in range(n):
    a,b = map(int,input().split())
    abl.append((a,b))
    
dp = [[0 for i in range(t+1)] for j in range(n+1)]

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

for _ in range(n):
    m = max([i[1] for i in abl])
    rest_food.append(m)
    abl.pop(0)

ans = 0
for i in range(n):
    tmp = rest_food[i] + dp[i][t-1]
    if tmp > ans:
        ans = tmp

if dp[n][t] > ans:
    ans = dp[n][t]

print(ans)

Submission status: Wrong Answer

Solution code:
n, t = [int(x) for x in input().split()]
xs   = []
ys   = []
dp   = []

for _ in range(0, 3001):
	dp.append([0] * 3001)

for i in range(0, t + 1):
	dp[0][t] = 0

for _ in range(0, n):
	x, y = [int(x) for x in input().split()]
	xs.append(x)
	ys.append(y)

for i in range(0, n):
	for w in range(0, t + 1):
		if xs[i] <= w:
			dp[i + 1][w] = max(dp[i][w - xs[i]] + ys[i], dp[i][w])
		else:
			dp[i + 1][w] = dp[i][w]
			
print(dp[n][t])

Submission status: Wrong Answer

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

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

M = [0] * N
for i in range(N - 1, -1, -1) :
    M[i] = max(M[i], vw[i][1])
        
dp = [-float('inf')] * T
dp[0] = 0
ret = 0

for n, (w, v) in enumerate(vw) :
    for i in range(T-1, w-1, -1) :
        dp[i] = max(dp[i], dp[i-w]+v)
    
    if n + 1 < N :
        ret = max(ret, max(dp) + M[n + 1])
    else :
        ret = max(ret, max(dp))
    
print(ret)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
# N, T = 3000, 14
# AB = [(i**2, i) for i in range(N)]

# dp[i+1][j] = i番目までの料理で、j分経った時の、最高満足度
# jがT以上のときはj == T とする(T以上における差はないので)

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

for i, (a, b) in enumerate(AB):
    # print(a, b)
    for j in range(T-1):
        dp[i+1][j] = max(
            dp[i+1][j],
            dp[i][j]
        )
        if j+a < T:
            dp[i+1][j+a] = max(
                dp[i+1][j+a],
                dp[i][j] + b
            )
        else:
            dp[i+1][T] = max(
                dp[i+1][T],
                dp[i][j] + b
            )

# for dp_i in dp:
#     print(dp_i)

print(dp[N][T])

Submission status: Wrong Answer

Solution code:
import collections

n, t = map(int, input().split())
a = [None] * n
b = [None] * n
for i in range(n):
    a[i], b[i] = map(int, input().split())

dp = [collections.defaultdict(int) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
    for cur in dp[i]:
        dp[i + 1][cur] = max(dp[i + 1][cur], dp[i][cur])
        if cur <= t:
            dp[i + 1][cur + a[i]] = max(dp[i + 1][cur + a[i]], dp[i][cur] + b[i])
print(max(dp[n].values()))

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
ab = [list(map(int,input().split())) for _ in [0]*n]
#満足度の高い順にソートする
ab.sort(key = lambda x:-x[1])
#print(ab)

sum = [[ab[0][0],ab[0][1]]] + [[0,0] for _ in [0]*(n-1)] 

#累積和を計算
for i in range(1,n):
  sum[i][0] = sum[i-1][0] + ab[i][0]
  sum[i][1] = sum[i-1][1] + ab[i][1]
  if sum[i][0] >= t:
    print(sum[i][1])
    exit()
    
#print(sum)

if sum[-1][0] < t:
  print(sum[-1][1])
  
# めちゃくちゃ時間かかるけどめちゃくちゃ満足度のあるやつを時間ぎりぎりで注文したい。
# 周りと比べて価値が低いがかかる分数が少ないからコスパが良い、みたいなやつも考慮すべき？

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inp():
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl():
    return list(map(int, input().split()))
def inpls():
    return list(input().split())
def inplm(n):
    return list(int(input()) for _ in range(n))
def inpll(n):
    return sorted([list(map(int, input().split())) for _ in range(n)])

def Knapsack2(W,w,v):
    n=len(w)
    v_max=W+max(w)+10
    dp=[0]+[10**8 for _ in range(v_max+1)]
    over=[0 for _ in range(v_max+2)]
    for i in range(n):
        for j in range(v_max,v[i]-1,-1):
            a=dp[j]
            dp[j]=min(dp[j],dp[j-v[i]]+w[i])
            if a!=dp[j]:
                over[j]=max(over[j],w[i])
    ans=0
    for i in range(v_max+2):
        if dp[i]-over[i]<=(W-0.5):
            ans=i
    print(ans)

def main():
    n,t=inpm()
    a=[]
    b=[]
    for _ in range(n):
        x,y=inpm()
        a.append(x)
        b.append(y)
    Knapsack2(t,a,b)

if __name__ == "__main__":
    main()

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 i in range(N)]
ab2 = [ab[i] for i in range(N) if ab[i][0] >= T]
ab2.append([0,0])
ab.sort(key=lambda x: x[1])
ab.sort(key=lambda x: x[0])
ab2.sort(key=lambda x: x[1])

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


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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())
c = [tuple(map(int,input().split())) for _ in range(n)]
dp1 = [[-1]*t for _ in range(n+1)]
dp2 = [[-1]*t for _ in range(n+1)]
dp1[0][0] = 0
dp2[-1][0] = 0
for r in range(n):
    a,b = c[r]
    for i in range(t):
        if dp1[r][i] >= 0:
            dp1[r+1][i] = dp1[r][i]
            if i+a < t and dp1[r+1][i+a] < dp1[r][i] + b:
                dp1[r+1][i+a] = dp1[r][i] + b
    a,b = c[-r-1]
    for i in range(t):
        if dp2[-r-1][i] >= 0:
            dp2[-r-2][i] = dp2[-r-1][i]
            if i+a < t and dp2[-r-2][i+a] < dp2[-r-1][i] + b:
                dp2[-r-2][i+a] = dp2[-r-1][i] + b
for r in range(n+1):
    for i in range(t-1):
        if dp1[r][i+1] < dp1[r][i]:
            dp1[r][i+1] = dp1[r][i]
        if dp2[r][i+1] < dp2[r][i]:
            dp2[r][i+1] = dp2[r][i]

ans = 0
for i in range(n):
    a,b = c[i]
    r1 = dp1[i][:]
    r2 = dp2[i+1][::-1] 
    cur = b + max(r1[j]+r2[j] for j in range(t))
    if ans < cur:
        ans = cur
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

N, T = map(int, input().split())

tabemono = []

for _ in range(N):
    a, b = map(int, input().split())
    heikin = b / a
    heapq.heappush(tabemono, (-heikin, a, b))


zangai = []
jikan = 0
manzoku = 0
while jikan < T:
    if len(tabemono) == 0:
        break

    tabe = heapq.heappop(tabemono)
    if tabe[1] < T - jikan:
        manzoku += tabe[2]
        jikan += tabe[1]
    else:
        zangai.append(-tabe[2])

zangai.sort()

if jikan <= T and len(zangai) > 0:
    manzoku -= zangai[0]

print(manzoku)

Submission status: Wrong Answer

Solution code:
def get():
    return map(int,input().split())

N,W=get()
w=[0]*N
v=[0]*N
for i in range(N):
    w[i],v[i]=get()
if W>=sum(w):
    print(sum(v))
else:
    dp=[0]*(W+1)
    dp2 = 0
    for i in range(N):
        for j in range(w[i]+W-1,w[i]-1,-1):
            if j >= W:
                dp2=max(dp2,dp[W-1]+v[i])
            else:
                dp[j]=max(dp[j],dp[j-w[i]]+v[i])
    print(dp2)

Submission status: Wrong Answer

Solution code:
import collections

n, t = map(int, input().split())
a = [None] * n
b = [None] * n
for i in range(n):
    a[i], b[i] = map(int, input().split())

dp = [collections.defaultdict(int) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
    for cur in dp[i]:
        dp[i + 1][cur] = max(dp[i + 1][cur], dp[i][cur])
        if cur < t:
            dp[i + 1][cur + a[i]] = max(dp[i + 1][cur + a[i]], dp[i][cur] + b[i])
print(max(dp[n].values()))

Submission status: Wrong Answer

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

n,w = inpl()
wv = [inpl() for i in range(n)]
dp = [[0 for i in range(w+1)] for j in range(n+1)]
wv.sort()
wv.sort(key=lambda x:x[1])
ww = [0] * n
vv = [0] * n
for i in range(n):
    ww[i],vv[i] = wv[i][0], wv[i][1]
for i in range(n-1):
    for j in range(w+1):
        if j >= ww[i]:
            dp[i+1][j] = max(dp[i][j], dp[i][j-ww[i]]+vv[i])
        else:
            dp[i+1][j] = dp[i][j]
res = dp[n-1][w-1]+wv[n-1][1]

dp = [[0 for i in range(w+1)] for j in range(n+1)]
wv.sort()
ww = [0] * n
vv = [0] * n
for i in range(n):
    ww[i],vv[i] = wv[i][0], wv[i][1]
for i in range(n-1):
    for j in range(w+1):
        if j >= ww[i]:
            dp[i+1][j] = max(dp[i][j], dp[i][j-ww[i]]+vv[i])
        else:
            dp[i+1][j] = dp[i][j]
print(max(res,dp[n-1][w-1]+wv[n-1][1]))

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
d=[None]*N
for i in range(N):
  a,b=map(int,input().split())
  d[i]=[a,b]

dp=[-1]*(3000*2)
dp[0]=0
for i in range(len(d)):
  t=d[i][0]
  v=d[i][1]
  for j in range(T-1,-1,-1):
    if dp[j]!=-1:
      dp[j+t]=max(dp[j+t],dp[j]+v)
      
print(max(dp))

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline().rstrip
show_flg=False
#show_flg=True

ans=0

n,T=LI()
c=[]
maxA=0
for i in range(n):
    a,b=LI()
    c.append((a,b))
c=sorted(c,key=lambda x:x[1]/x[0])
l_a,l_b=c.pop()
n-=1
maxA=max([a for a,b in c])

dp=[[0 for i in range(T+maxA+2)] for i in range(n+1)]

for i in range(n):
    a,b=c[i]
    show(dp)
    for t in range(a,T+maxA+1):
        dp[i+1][t]=dp[i][t]
        if t-a<T-0.5:
            dp[i+1][t]=max(dp[i][t-a]+b,dp[i+1][t])
show(dp)
ans=0
ans=max(dp[n][:T])+l_b
#for t in range(T+maxA+2):
#    ans=max(ans,dp[n][t])
        
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=sorted(l, key=lambda x: x[1])
l=l[::-1]
#print(l)
dp=[[0]*(t+1) for i in range(n+1)]
q=[0]
for i in range(1,n+1):
    for j in range(t+1):
        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]
    if dp[i][-1]==dp[i-1][-1]:
        #print(l[i])
        q.append(l[i-1][1])
q.sort()
#print(dp)
#print(q)
print(max(dp[-1][-2]+q[-1],dp[-1][-1]))

Submission status: Wrong Answer

Solution code:
def get():
    return map(int,input().split())

N,W=get()
w=[0]*N
v=[0]*N
for i in range(N):
    w[i],v[i]=get()
if W>=sum(w):
    print(sum(v))
else:
    dp=[0]*(W+1)
    dp2 = 0
    for i in range(N):
        for j in range(w[i]+W,w[i]-1,-1):
            if j >= W:
                dp2=max(dp2,dp[W-1]+v[i])
            else:
                dp[j]=max(dp[j],dp[j-w[i]]+v[i])
    print(dp2)

Submission status: Wrong Answer

Solution code:
N,T=map(int,input().split())
x=[]
for i in range(N):
    Ai,Bi=map(int,input().split())
    x.append((Ai,Bi))

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

dp=[[0 for j in range(T)] for i in range(N)] # i番目までの料理（最後の料理を含まない）でj分後（<=T-1分後）までに得られる満足度の最大値（単純dp）

# 単純dp
for i in range(N-1):
    for j in range(T):
        if x[i][0]>j:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-x[i][0]]+x[i][1])

print(dp[N-1][T-1]+x[-1][1])

Submission status: Wrong Answer

Solution code:
def main():
    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())

    u = [0] + [B[0]] * (T)
    v = [0] * A[0] + [B[0]] * (T - A[0] + 1)
    
    for i in range(1, N):
        a, b = A[i], B[i]
        for t in range(T, 0, -1):
            vv = b + v[t - 1]
            if t >= a:
                vv = max(vv, b + u[t - a])
            if u[t] < vv:
                u[t] = vv
        for t in range(T, a - 1, -1):
            vv = b + v[t - a]
            if v[t] < vv:
                t = vv
    print(u[T])

            



main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

n,t = map(int,input().split())

abl = []
for _ in range(n):
    a,b = map(int,input().split())
    abl.append((a,b))
    
dp = [[0 for i in range(t-1)] for j in range(n+1)]

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

for _ in range(n):
    m = max([i[1] for i in abl])
    rest_food.append(m)
    abl.pop(0)

ans = 0
for i in range(n):
    tmp = rest_food[i] + dp[i][t-2]
    if tmp > ans:
        ans = tmp

if dp[n][t-2] > ans:
    ans = dp[n][t-2]

print(ans)

Submission status: Wrong Answer

Solution code:
n,t = map(int,input().split())

food = [[0,0] for i in range(n)]
for i in range(n):
  a,b = map(int,input().split())
  food[i][0] = a
  food[i][1] = b

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


dp = [[-10000000 for i in range(t+4000)] for j in range(n+1)]
dp[0][0] = 0


for i in range(n):
  a = food[i][0]
  b = food[i][1]
  for j in range(t):
    eat = dp[i][j]+b
    next_time = j+a
    # print(j,next_time)
    if dp[i+1][next_time] < eat:
      dp[i+1][next_time] = eat
    if dp[i+1][j] < dp[i][j]:
      dp[i+1][j] = dp[i][j]

# for i in range(n):
#   for j in range(10,21):
#     print(dp[i][j],end=" ")
#   print()
print(max(dp[-1]))

Submission status: Wrong Answer

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

A = []
B = []
for _ in range(N):
    aa, bb = map(int, input().split())

    A.append(aa)
    B.append(bb)

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

for x in range(1, N + 1):
    for y in range(T + MAX + 1):
        if y >= A[x - 1] and (y - A[x - 1]) < T:
            dp[x][y] = max(dp[x - 1][y], dp[x - 1][y - A[x - 1]] + B[x - 1])
        
        else:
            dp[x][y] = dp[x - 1][y]
        
        
ans = -1
for d in dp:
    ans = max(ans, max(d))
    #print(d)
print(ans)

Submission status: Wrong Answer

Solution code:
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
dp = [[0] * (T+2) for _ in range(N+1)]
AB.sort(key = lambda x:x[1], reverse=True)
AB[0][0] = 0
for i in range(1,N+1):
    for j in range(1,T+2):
        if j < AB[i-1][0]:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j-AB[i-1][0]] + AB[i-1][1], dp[i-1][j])
print(dp[N][T])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import  numpy as np

def solve():
    N, T = map(int, input().split())
    C = [tuple(map(int, input().split())) for _ in range(N)]
    C.sort(key=lambda c: c[0]/c[1])
    dp = np.zeros(T+1, dtype=int)
    t = np.arange(T)
    for a,b in C:
        s = np.minimum(T, t+a)
        dp[s] = np.maximum(dp[t]+b, dp[s])
    return dp[T]

if __name__ == '__main__':
    print(solve())

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()

n,t=LI()
t-=1
dp=[[-1]*3010 for _ in range(3010)]
l=[LI() for _ in range(n)]

def rec(i,j,cnt):
  if dp[i][j]>=0:
    return dp[i][j]

  if i==n:
    res=0
  elif j<l[i][0]:
    if cnt==n-1:
      cnt+=1
      res=max(rec(i+1,j,cnt),rec(i+1,j-l[i][0],cnt)+l[i][1])
    else:
      res=rec(i+1,j,cnt)
  else:
    cnt+=1
    res=max(rec(i+1,j,cnt),rec(i+1,j-l[i][0],cnt)+l[i][1])
  dp[i][j]=res
  return res

def main():
  return rec(0,t,0)

# main()
print(main())

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import  numpy as np

def solve():
    N, T = map(int, input().split())
    C = [tuple(map(int, input().split())) for _ in range(N)]
    C.sort(key=lambda c: c[0]/c[1])
    dp = np.zeros(T+1, dtype=int)
    t = np.arange(T)
    for a,b in C:
        s = np.minimum(T, t+a)
        dp[s] = np.maximum(dp[t]+b, dp[s])
        print(dp)
    return dp[T]

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l=sorted(l, key=lambda x: x[1])
l=l[::-1]
#print(l)
dp=[[0]*(t+1) for i in range(n+1)]
q=[0]
for i in range(1,n+1):
    for j in range(t+1):
        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]
    if dp[i][-1]==dp[i-1][-1]:
        #print(l[i])
        q.append(l[i-1][1])
q.sort()
#print(dp)
#print(q)
print(dp[-1][-1]+q[-1])

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
N, T = map(int, readline().split())
AB = [list(map(int, readline().split())) for _ in range(N)]
A, B = map(list, zip(*AB))
dp = [0]*(T + max(A) + 1)
L = len(dp)
for i in range(N):
    dp2 = dp[:]
    for t in range(L):
        if t >= A[i] and t - A[i] < T:
            dp2[t] = max(dp2[t], dp[t-A[i]] + B[i] )
    dp = dp2[:]
    for i in range(1, L):
        dp[i] = max(dp[i], dp[i-1])
print(max(dp))

Submission status: Wrong Answer

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

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

D.sort(key=lambda x:x[1],reverse=True)

DP=[[0,0] for i in range(T+1)]

for ind,(x,y) in enumerate(D):
    for time in range(T,x-1,-1):
        if DP[time][0]<DP[time-x][0]+y:

            if DP[time-x][1]==ind:
                DP[time]=[DP[time-x][0]+y,ind+1]
            else:
                DP[time]=[DP[time-x][0]+y,DP[time-x][1]]

        elif DP[time][0]==DP[time-x][0]+y:

            if DP[time-x][1]==ind:
                DP[time][1]=min(ind+1,DP[time][1])
            else:
                DP[time][1]=min(DP[time-x][1],DP[time][1])

    #print(DP)

D.append((0,0))
                
ANS=D[-2][1]

for x,y in DP[:-1]:
    if x==0:
        continue
    ANS=max(ANS,x+D[y][1])

print(ANS)