Problem:


Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^5

1 \leq w_i \leq W

1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.



Sample input:
3 8
3 30
4 50
5 60


Sample output:
90

Submission status: Time Limit Exceeded

Solution code:
n,W = map(int,input().split())
w,v = [0] * n ,[0] * n
for i in range(n):
    w[i] ,v[i] = map(int,input().split())
dp=[[-1 for i in range(W + 1)] for j in range(n + 1)]


def rec(i,j):
    if dp[i][j] !=-1:
        return dp[i][j]
    if i == n:
        res = 0
    elif j < w[i]:
        res = rec(i + 1, j)
    else:
        res = max(rec(i + 1 ,j),rec(i + 1,j -w[i]) + v[i])
    dp[i][j] =res
    return res

print(rec(0,W))

Submission status: Time Limit Exceeded

Solution code:
n,W = map(int,input().split())
w,v = [0] * n ,[0] * n
for i in range(n):
    w[i] ,v[i] = map(int,input().split())
dp=[[-1 for i in range(W + 1)] for j in range(n + 1)]


def rec(i,j):
    if dp[i][j] !=-1:
        return dp[i][j]
    if i == n:
        res = 0
    elif j < w[i]:
        res = rec(i + 1, j)
    else:
        res = max(rec(i + 1 ,j),rec(i + 1,j -w[i]) + v[i])
    dp[i][j] =res
    return res

print(rec(0,W))

Submission status: Time Limit Exceeded

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

dp = [[None for __ in range(M+1)] for _ in range(N+1)]
dp[0] = [0 for _ in range(M+1)]
for i in range(1, N+1):
    dp[i] = [max(dp[i-1][w], dp[i-1][w-W[i-1]] + V[i-1]) if W[i-1] <= w else dp[i-1][w] for w in range(M+1)]
    # for w in range(M+1):
    #     if W[i-1] <= w:
    #         dp[i][w] = max(dp[i-1][w], dp[i-1][w-W[i-1]] + V[i-1])
    #     else:
    #         dp[i][w] = dp[i-1][w]


print(max(dp[-1]))

Submission status: Time Limit Exceeded

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

dp = [[None for __ in range(M+1)] for _ in range(N+1)]
dp[0] = [0 for _ in range(M+1)]
for i in range(1, N+1):
    for w in range(M+1):
        if W[i-1] <= w:
            dp[i][w] = max(dp[i-1][w], dp[i-1][w-W[i-1]] + V[i-1])
        else:
            dp[i][w] = dp[i-1][w]


print(max(dp[-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

input = sys.stdin.readline

N, W = map(int, input().split())
dp = np.zeros((N + 1, W + 1), dtype=np.int64)

for i in range(1, N + 1):
    weight, value = map(int, input().split())
    for w in range(W + 1):
        if weight <= w:
            dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weight] + value)
        else:
            dp[i][w] = dp[i - 1][w]

print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
n, W =map(int,input().split())
w = [0] * n
v = [0] * n
for i in range(n):
    w[i], v[i] = map(int, input().split())

def ks(W,w,v,n):
    if n == 0 or W == 0 : 
        return 0
    if (w[n-1] > W): 
        return ks(W, w, v, n - 1)
    else:
        return max(v[n - 1] + ks(W - w[n - 1], w, v, n - 1), 
                   ks(W, w, v, n - 1))
print(ks(W,w,v,n))

Submission status: Time Limit Exceeded

Solution code:
n, W = map(int, input().split())
dp = [[0] * (W + 1) for _ in range(n + 1)]

dp[0][W] = 0
for i in range(n):
    w, v = map(int, input().split())
    for j in range(W + 1):
        if j >= w:
            dp[i + 1][j] = max(dp[i][j - w] + v, dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
print(dp[n][W])

Submission status: Time Limit Exceeded

Solution code:
# import doctest


def solve(N, W, wv):
    """
    >>> solve(3, 8, [(5, 60), (4, 50), (3, 30)])
    90
    """
    # dp[i][j] = 品物iまでから重さの総和がj以下になるように選んだ時の価値の最大値
    # dp[i][j] = max(dp[i - 1][j - w] + v, dp[i - 1][j])

    # 初期化
    dp = [[None for _ in range(W + 1)] for _ in range(N + 1)]
    for j in range(W + 1):
        w, v = wv[0]
        dp[1][j] = v if w <= j else 0

    for i in range(2, N + 1):
        w, v = wv[i - 1]
        for j in range(W + 1):
            if w > j:
                dp[i][j] = dp[i - 1][j]
                continue
            picked = dp[i - 1][j - w] + v
            ignored = dp[i - 1][j]
            dp[i][j] = max(picked, ignored)

    return dp[N][W]


# doctest.testmod()
N, W = map(int, input().split())
wv = [tuple(map(int, input().split())) for _ in range(N)]
print(solve(N, W, wv))

Submission status: Time Limit Exceeded

Solution code:
n,W=map(int,input().split())
w_list=[]
v_list=[]
for i in range(n):
    w,v=map(int,input().split())
    w_list.append(w)
    v_list.append(v)
dp={}
def f(i,w):
    if (i,w) in dp:
        return dp[(i,w)]
    else:
        if i==n:
            return 0
        else:
            if w_list[i]<=w:
                a=max(f(i+1,w),f(i+1,w-w_list[i])+v_list[i])
            else:
                a=f(i+1,w)
            dp[(i,w)]=a
            return a
print(f(0,W))

Submission status: Time Limit Exceeded

Solution code:
dp = [[0] * 100100 for i in range(110)]

N, W = map(int, input().split())
w = [0] * N
v = [0] * N
for i in range(N):
    w[i], v[i] = map(int, input().split())

for i in range(1, N+1):
    for sum_w in range(W+1):
        if sum_w - w[i-1] >= 0:
            dp[i][sum_w] = max(dp[i][sum_w], dp[i-1][sum_w-w[i-1]] + v[i-1])
        
        dp[i][sum_w] = max(dp[i][sum_w], dp[i-1][sum_w])

print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
n,W=map(int,input().split())
w_list=[]
v_list=[]
dp=[]
for i in range(n):
    w,v=map(int,input().split())
    w_list.append(w)
    v_list.append(v)
for i in range(n+1):
    dp_sub=[]
    for j in range(W+1):
        dp_sub.append(-1)
    dp.append(dp_sub)
def f(i,w):
    if dp[i][w]>=0:
        return dp[i][w]
    else:
        if i==n:
            return 0
        else:
            if w_list[i]<=w:
                a=max(f(i+1,w),f(i+1,w-w_list[i])+v_list[i])
            else:
                a=f(i+1,w)
            dp[i][w]=a
            return a
print(f(0,W))

Submission status: Time Limit Exceeded

Solution code:
n,W=map(int,input().split())
w_list=[]
v_list=[]
dp=[]
for i in range(n):
    w,v=map(int,input().split())
    w_list.append(w)
    v_list.append(v)
for i in range(n+1):
    dp_sub=[]
    for j in range(W+1):
        dp_sub.append(-1)
    dp.append(dp_sub)
def f(i,w):
    if dp[i][w]>=0:
        return dp[i][w]
    else:
        if i==n:
            return 0
        else:
            if w_list[i]<=w:
                a=max(f(i+1,w),f(i+1,w-w_list[i])+v_list[i])
                dp[i][w]=a
                return a
            else:
                a=f(i+1,w)
                dp[i][w]=a
                return a
print(f(0,W))

Submission status: Time Limit Exceeded

Solution code:
n,W=map(int,input().split())
w_list=[]
v_list=[]
dp=[]
for i in range(n):
    w,v=map(int,input().split())
    w_list.append(w)
    v_list.append(v)
for i in range(n+1):
    dp_sub=[]
    for j in range(W+1):
        dp_sub.append(-1)
    dp.append(dp_sub)
def f(i,w):
    if dp[i][w]>=0:
        return dp[i][w]
    else:
        if i==n:
            return 0
        else:
            if w_list[i]<=w:
                return max(f(i+1,w),f(i+1,w-w_list[i])+v_list[i])
            else:
                return f(i+1,w)
print(f(0,W))

Submission status: Time Limit Exceeded

Solution code:
#Knapsack1
N, W = map(int, input().split())
weight = []
value = []
# N,W =3, 8
# wv_lst = [list(map(int, input().split())) for _ in range(N)]
for i in range(N):
    w, v = map(int, input().split())
    weight.append(w)
    value.append(v)
# weight = [3, 4, 5]
# value = [30, 50, 60]

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

for n in range(1, N+1):
    weigh = weight[n-1]
    val = value[n-1]
    for w in range(W+1):
        if w < weigh:
            dp[n][w] = dp[n-1][w]
        else:
            dp[n][w] = max(dp[n-1][w], 
                        dp[n-1][w-weigh] + val)
        
# print([dp[i] for i in range(N+1)])
print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
w = [0] * N
v = [0] * N
for i in range(N):
    w[i], v[i] = map(int, input().split())

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

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

Submission status: Time Limit Exceeded

Solution code:
N,W=map(int,input().split())
from collections import*
d=defaultdict(int)
d[0]=0
for i in range(N):
    w,v=map(int,input().split())
    for k,b in d.copy().items():
        if k+w<=W:
            d[k+w]=max(d[k+w],b+v)
print(max(d.values()))

Submission status: Time Limit Exceeded

Solution code:
N,W = map(int,input().split())
wv = [list(map(int, input().split())) for i in range(N)]
dp = [[-1]*10**5 for i in range(N)]
import sys
sys.setrecursionlimit(10**9)
def dfs(i,w):
    if i == N:
        return 0
    if dp[i][w] != -1:
        return dp[i][w]
    else:
        if w+wv[i][0] > W:
            dp[i][w] = dfs(i+1,w)
        else:
            dp[i][w] = max(dfs(i+1,w+wv[i][0])+wv[i][1], dfs(i+1,w))
    return dp[i][w]
print(dfs(0,0))

Submission status: Time Limit Exceeded

Solution code:
N,W = map(int,input().split())
wv = [list(map(int, input().split())) for i in range(N)]
dp = [[-1]*10**5 for i in range(N)]
import sys
sys.setrecursionlimit(10**9)
def dfs(i,w):
    if i == N:
        return 0
    if dp[i][w] != -1:
        return dp[i][w]
    else:
        if w+wv[i][0] > W:
            dp[i][w] = dfs(i+1,w)
            return dp[i][w]
        else:
            dp[i][w] = max(dfs(i+1,w+wv[i][0])+wv[i][1], dfs(i+1,w))
    return dp[i][w]
print(dfs(0,0))

Submission status: Time Limit Exceeded

Solution code:
n, w = map(int, input().split())
ws, vs = [], []
for _ in range(n):
    ww, vv = map(int, input().split())
    ws.append(ww)
    vs.append(vv)

dp = [[0 for _ in range(w+1)] for _ in range(n)]
dp[0] = [vs[0] if j >= ws[0] else 0 for j in range(w+1)]

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

print(dp[n-1][w])

Submission status: Time Limit Exceeded

Solution code:
import copy
N , W = map(int, input().split())
item =[list(map(int, input().split())) for i in range(N)]
dp = [0] * (W+1)
dp2 = [0] * (W+1)
for i in range(N):
    w = item[i][0]
    v = item[i][1]
    for j in range(1, W+1):
        if j - w >= 0:
            dp2[j] = max(dp[j], dp[j-w] + v)
    dp = copy.copy(dp2)
print(dp[W])

Submission status: Time Limit Exceeded

Solution code:
N,Wmax=map(int,input().split(' '))
info=[ list(map(int,input().split(' '))) for i in range(N) ]
W = [ info[i][0] for i in range(N) ]
V = [ info[i][1] for i in range(N) ]

#dp[i][j]=i番目の品物までだけを選び重量制限をjとしたときの価値の最大値
dp = []

for i in range(N):
	if i==0:
		#重量制限がW[0]以上なら荷物を積むのが最善
		dp.append([ V[0] if W[0]<=w else 0 for w in range(Wmax+1) ])	
	else:
		#今回の重量制限をwとする
		#今回は荷物を積む場合: dp[i][w] = dp[i-1][w-w[i]]+v[i] 
		#今回は荷物を積まない場合: dp[i][w] = dp[i-1][w]
		#よってこの2つのうち大きいほうをdp[i][w]に採用する
		l = []
		for w in range(Wmax+1):
			val1 = (dp[i-1][w-W[i]]+V[i]) if w-W[i]>=0 else 0
			val2 = dp[i-1][w]
			l.append(max(val1,val2))
		dp.append(l)
print(dp[N-1][Wmax])

Submission status: Time Limit Exceeded

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

N, W = [int(i) for i in input().split()]
wv = []

for i in range(N):
    wv.append([int(i) for i in input().split()])
wv.sort()
#print(wv)

s =set([0])
dp = [0 for i in range(W+1)]
for i in range(N):
    s2 = set()
    #print(s)
    for j in s:
        if wv[i][0] + j <= W:
            dp[wv[i][0] + j] = dp[j] + wv[i][1]
            s2.add(wv[i][0] + j)
            #print(s2)
        else:
            break
        s = s | s2

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:

N, W = [int(i) for i in input().split()]
wv = []

for i in range(N):
    wv.append([int(i) for i in input().split()])
wv.sort()
#print(wv)

s =set([0])
dp = [0 for i in range(W+1)]
for i in range(N):
    s2 = set()
    #print(s)
    for j in s:
        if wv[i][0] + j <= W:
            dp[wv[i][0] + j] = dp[j] + wv[i][1]
            s2.add(wv[i][0] + j)
            #print(s2)
        else:
            break
        s = s | s2

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:

N, W = [int(i) for i in input().split()]
wv = []
s =set([0])

for i in range(N):
    wv.append([int(i) for i in input().split()])

dp = [0 for i in range(W+1)]
for i in range(N):
    s2 = set()
    for j in s:
        if wv[i][0] + j <= W:
            dp[wv[i][0] + j] = dp[j] + wv[i][1]
            s2.add(wv[i][0] + j)
        s = s | s2
print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(N)]
#品物は0～N-1と考えるので注意！

DP = [[-1]*(W+1) for _ in range(N-1)] #0~N-2
#DP[depth:0~N-1][0~W] calc()の結果をメモる
#DP[N-1][]については、W-pwsum>=N-1の重さならN-1の重さ
##W-pwsum<N-1の重さ なら 0
##→W-(N-1)>=pwsumとW-(N-1)<pwsumで場合分け
DP.append([0]*(W+1))
tmppwsum = 0
weiofMinus1, valofNminus1 = wv[N-1]
while W-weiofMinus1>=tmppwsum:
  DP[N-1][tmppwsum] = valofNminus1
  tmppwsum += 1


#合計pwsumの重さを入れた状態で、
#depth以降の品物を選ぶ場合の最大価値を返す関数
def calc(depth, pwsum):
  #メモ済みだったらメモした値を返す
  dp =  DP[depth][pwsum]
  if dp != -1: return dp
  
  nowWei, nowVal = wv[depth] #今選んでる品物のこと
  if pwsum + nowWei > W:
    #入れると重さ超過なので、入れずに次を考える
#    if depth >= N-1:
#      #今の品物が最後なら、以降得られる価値は0
#      DP[depth][pwsum] = 0
#      return 0
#    else:
    #次の品物が無い場合はDPで省かれている
      #次の品物がある場合、今のを入れずに次へいく
    irenai = calc(depth+1, pwsum)
    DP[depth][pwsum] = irenai
    return irenai
  if pwsum + nowWei <= W:
    #入れても大丈夫な場合
#    if depth >= N-1:
#      #今の品物が最後なら、今の品物しか入れられない
#      DP[depth][pwsum] = nowVal
#      return nowVal
    #次の品物が無い場合はDPで省かれている
      #次の品物がある場合、今のを入れる入れないの2択
    ireru = nowVal + calc(depth+1, pwsum+nowWei)
    irenai = calc(depth+1, pwsum)
    ans = max(ireru, irenai)
    DP[depth][pwsum] = ans
    return ans
    
print(calc(0, 0))

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(N)]
#品物は0～N-1と考えるので注意！

DP = [[-1]*(W+1) for _ in range(N-1)] #0~N-2
#DP[depth:0~N-1][0~W] calc()の結果をメモる
#DP[N-1][]については、W-pwsum>=N-1の重さならN-1の重さ
##W-pwsum<N-1の重さ なら 0
##→W-(N-1)>=pwsumとW-(N-1)<pwsumで場合分け
DP.append([0]*(W+1))
tmppwsum = 0
weiofMinus1, valofNminus1 = wv[N-1]
while W-weiofMinus1>=tmppwsum:
  DP[N-1][tmppwsum] = valofNminus1
  tmppwsum += 1


#合計pwsumの重さを入れた状態で、
#depth以降の品物を選ぶ場合の最大価値を返す関数
def calc(depth, pwsum):
  #メモ済みだったらメモした値を返す
  dp =  DP[depth][pwsum]
  if dp != -1: return dp
  
  nowWei, nowVal = wv[depth] #今選んでる品物のこと
  if pwsum + nowWei > W:
    #入れると重さ超過なので、入れずに次を考える
#    if depth >= N-1:
#      #今の品物が最後なら、以降得られる価値は0
#      DP[depth][pwsum] = 0
#      return 0
#    else:
    #次の品物が無い場合はDPで省かれている
      #次の品物がある場合、今のを入れずに次へいく
    irenai = calc(depth+1, pwsum)
    DP[depth][pwsum] = irenai
    return irenai
  if pwsum + nowWei <= W:
    #入れても大丈夫な場合
#    if depth >= N-1:
#      #今の品物が最後なら、今の品物しか入れられない
#      DP[depth][pwsum] = nowVal
#      return nowVal
    #次の品物が無い場合はDPで省かれている
      #次の品物がある場合、今のを入れる入れないの2択
    ireru = nowVal + calc(depth+1, pwsum+nowWei)
    irenai = calc(depth+1, pwsum)
    DP[depth][pwsum] = max(ireru, irenai)
    return max(ireru, irenai)
    
print(calc(0, 0))

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(N)]
#品物は0～N-1と考えるので注意！

DP = [[-1]*(W+1) for _ in range(N)]
#DP[depth:0~N-1][0~W] calc()の結果をメモる

#合計pwsumの重さを入れた状態で、
#depth以降の品物を選ぶ場合の最大価値を返す関数
def calc(depth, pwsum):
  #メモ済みだったらメモした値を返す
  dp =  DP[depth][pwsum]
  if dp != -1: return dp
  
  nowWei, nowVal = wv[depth] #今選んでる品物のこと
  if pwsum + nowWei > W:
    #入れると重さ超過なので、入れずに次を考える
    if depth >= N-1:
      #今の品物が最後なら、以降得られる価値は0
      DP[depth][pwsum] = 0
      return 0
    else:
      #次の品物がある場合、今のを入れずに次へいく
      irenai = calc(depth+1, pwsum)
      DP[depth][pwsum] = irenai
      return irenai
  if pwsum + nowWei <= W:
    #入れても大丈夫な場合
    if depth >= N-1:
      #今の品物が最後なら、今の品物しか入れられない
      DP[depth][pwsum] = nowVal
      return nowVal
    else:
      #次の品物がある場合、今のを入れる入れないの2択
      ireru = nowVal + calc(depth+1, pwsum+nowWei)
      irenai = calc(depth+1, pwsum)
      DP[depth][pwsum] = max(ireru, irenai)
      return max(ireru, irenai)
    
print(calc(0, 0))

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(N)]
#品物は0～N-1と考えるので注意！

#合計pwsumの重さ、合計pvsumの価値を入れた状態で、
#depth以降の品物を選ぶ場合の最大価値を返す関数
def calc(depth, pwsum, pvsum):
  nowWei, nowVal = wv[depth] #今選んでる品物のこと
  if pwsum + nowWei > W:
    #入れると重さ超過なので、入れずに次を考える
    if depth >= N-1: return pvsum
      #今の品物が最後なら、そのまま返す
    else:
      #次の品物がある場合、今のを入れずに次へいく
      irenai = calc(depth+1, pwsum, pvsum)
      return irenai
  if pwsum + nowWei <= W:
    #入れても大丈夫な場合
    if depth >= N-1: return pvsum+nowVal
      #今の品物が最後なら、今の品物を入れて返す
    else:
      #次の品物がある場合、今のを入れる入れないの2択
      ireru = calc(depth+1, pwsum+nowWei, pvsum+nowVal)
      irenai = calc(depth+1, pwsum, pvsum)
      return max(ireru, irenai)
    
print(calc(0, 0, 0))

Submission status: Time Limit Exceeded

Solution code:
n,W = map(int,input().split())
w = [0]*n;v = [0]*n
for i in range(n):
    w[i],v[i] = map(int,input().split())
dp = [[-1 for i in range(W+1)] for j in range(n+1)]
def dppp(weight,i):
    global dp,w,v
    if i == n:return 0
    elif dp[i][weight] != -1:
        return dp[i][weight]
    elif w[i] > weight:
        dp[i][weight] = dppp(weight,i+1)
    else:
        dp[i][weight] = max(dppp(weight,i+1),
        dppp(weight-w[i],i+1)+v[i])
    return dppp(weight,i)
print(dppp(W,0))

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split(" "))
wv = [[int(i) for i in input().split(" ")] for _ in range(N)]

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

for i in range(N):
    for j in range(W+1):
        if j - wv[i][0] >= 0:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j - wv[i][0]] + wv[i][1])
        
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
val = [0]*(W+1)
for i in range(N):
  w, v = map(int, input().split())
  for j in reversed(range(w,W+1)):
    val[j] = max(val[j], val[j-w] + v) 
print(val[W])

Submission status: Time Limit Exceeded

Solution code:
N, W = [int(i) for i in input().split()]
weight, value = [0] * N, [0] * N
for i in range(N):
    weight[i], value[i] = [int(i) for i in input().split()]

dp = [[0] * (W+1) for _ in range(N)]
dp[0][weight[0]] = value[0]
for i in range(1, N):
    for w in range(W+1):
        if w-weight[i] >= 0:
            dp[i][w] = max(dp[i-1][w-weight[i]]+value[i], dp[i-1][w])
        else:
            dp[i][w] = dp[i-1][w]
print(max(dp[N-1]))

Submission status: Time Limit Exceeded

Solution code:
#問題 D
import numpy as np
N, W = map(int,input().split())
VAL = np.zeros(W+1)
val = np.zeros(W+1)
for n in range(N):
    w,v = map(int,input().split())
    for weight in range(w,W+1):
        val[weight] = max(VAL[weight],VAL[weight-w]+v)
    VAL = np.copy(val)
#    print(val)
print(int(VAL[W]))
    # listをコピーするときは注意　https://qiita.com/utgwkk/items/5ad2527f19150ae33322

Submission status: Time Limit Exceeded

Solution code:
#問題 D
import numpy as np
N, W = map(int,input().split())
VAL = [0]*(W+1)
val = [0]*(W+1)
for n in range(N):
    w,v = map(int,input().split())
    for weight in range(w,W+1):
        val[weight] = max(VAL[weight],VAL[weight-w]+v)
    VAL = val[:]
print(VAL[W])
    # listをコピーするときは注意　https://qiita.com/utgwkk/items/5ad2527f19150ae33322

Submission status: Time Limit Exceeded

Solution code:
#問題 D
N, W = map(int,input().split())
VAL = [0]*(W+1)
val = [0]*(W+1)
for n in range(N):
    w,v = map(int,input().split())
    for weight in range(1,W+1):
        if weight >= w:
            val[weight] = max(VAL[weight],VAL[weight-w]+v)
    VAL = val[:]
print(VAL[W])
    # listをコピーするときは注意　https://qiita.com/utgwkk/items/5ad2527f19150ae33322

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, W = list(map(int, input().split()))

# init
# dp[w]
# w: 0-W
dp = np.zeros(W + 1).astype(int)

# loop
for i in range(1, N + 1):
    w, v = list(map(int, input().split()))
    for j in np.where(dp > 0)[0]:
        if j + w > W:
            continue
        dp[j + w] = max(dp[j] + v, dp[j + w])
    dp[w] = v

# output
print(max(dp))

Submission status: Time Limit Exceeded

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

DP = [0] * (W + 1)
for w, v in wvList:
    for rj in range(W, w - 1, -1):
        num = DP[rj - w] + v
        if num > DP[rj]:
            DP[rj] = num

print(max(DP))

Submission status: Time Limit Exceeded

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

ans = 0
DP = [0] * (W + 1)
for i in range(N):
    w, v = map(int, input().split())
    for rj in range(W, w - 1, -1):
        num = DP[rj - w] + v
        if num > DP[rj]:
            DP[rj] = num

print(max(DP))

Submission status: Time Limit Exceeded

Solution code:
import sys

N, W = list(map(int, input().split()))

ans = 0
DP = [0] * (W + 1)
for i in range(N):
    dp = []
    w, v = map(int, sys.stdin.readline().split())
    for j, element in enumerate(DP):
        if j >= w:
            dp.append(DP[j - w] + v if element < DP[j - w] + v else element)
        else:
            dp.append(element)
    DP = dp

print(max(DP))

Submission status: Time Limit Exceeded

Solution code:
import sys

N, W = list(map(int, input().split()))

ans = 0
DP = [0] * (W + 1)
for i in range(N):
    dp = []
    w, v = map(int, sys.stdin.readline().split())
    for j, element in enumerate(DP):
        if j >= w:
            dp.append(max(DP[j - w] + v, element))
        else:
            dp.append(element)
    DP = dp

print(max(DP))

Submission status: Time Limit Exceeded

Solution code:
"""
AtCoder Educational DP Contest
https://atcoder.jp/contests/dp/tasks/dp_d

D - Knapsack 1
"""
INF = 100000007


def solve(wv_pairs):
    dp = [0] * (W + 1)
    for i in range(N):
        w_i, v_i = wv_pairs[i]
        for ww in range(W, w_i - 1, -1):
            dp[ww] = max(dp[ww], dp[ww - w_i] + v_i)
    return max(dp[ww] for ww in range(W + 1))


N, W = map(int, input().split())
pairs = [[int(s) for s in input().split()] for _ in range(N)]
res = solve(pairs)
print(res)

Submission status: Time Limit Exceeded

Solution code:
"""
AtCoder Educational DP Contest
https://atcoder.jp/contests/dp/tasks/dp_d

D - Knapsack 1
"""
INF = 100000007
import collections

def solve(wv_pairs):
    ws, vs = zip(*wv_pairs)
    dp = collections.defaultdict(lambda: -INF)
    dp[(-1, 0)] = 0
    for i in range(N):
        for ww in range(W + 1):
            dp[(i, ww)] = max(
                dp[(i - 1, ww)],
                dp[(i - 1, ww - ws[i])] + vs[i]
            )
    return max(dp[(N - 1, ww)] for ww in range(W + 1))

N, W = map(int, input().split())
pairs = [[int(s) for s in input().split()] for _ in range(N)]
res = solve(pairs)
print(res)

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
INF = -10 ** 12

# 重さW以下の時の価値の最大値
dp = [[0 for _ in range(W + 1)] for _ in range(N)]

for i in range(0, N):
    w, v = map(int, input().split())
    for j in range(W + 1):
        if j - w >= 0:
            dp[i][j] = max(dp[i - 1][j - w] + v, dp[i - 1][j])
        else:
            dp[i][j] = dp[i - 1][j]

print(dp[N - 1][W])

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
INF = -10 ** 12

# 重さW以下の時の価値の最大値
dp = [[0 for _ in range(W + 1)] for _ in range(N + 1)]

for i in range(1, N + 1):
    w, v = map(int, input().split())
    for j in range(W + 1):
        if j - w >= 0:
            dp[i][j] = max(dp[i - 1][j - w] + v, dp[i - 1][j])
        else:
            dp[i][j] = dp[i - 1][j]

print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0]*(capacity+1)
for _ in range(num):
    weight, value = map(int, input().split())
    for w in range(capacity,weight - 1,-1):
        tmp = dp[w - weight] + value
        if dp[w] < tmp:
            dp[w] = tmp

print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0]*(capacity+1)

for _ in range(num):
    weight, value = map(int, input().split())
    for w in range(capacity,weight - 1,-1):
        tmp = dp[w - weight] + value
        if dp[w] < tmp:
          dp[w] = tmp
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0]*(capacity+1)

for _ in range(num):
    weight, value = map(int, input().split())
    for w in range(capacity,weight - 1,-1):
        dp[w] = max( dp[w - weight] + value,  dp[w] )
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0]*(capacity+1)

for _ in range(num):
    weight, value = map(int, input().split())
    for w in range(capacity,weight - 1,-1):
        if w < weight:
            continue
        else:
            dp[w] = max( dp[w - weight] + value,  dp[w] )
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0]*(capacity+1)

for i in range(num):
    weight, value = map(int, input().split())
    for w in range(capacity,weight - 1,-1):
        if w < weight:
            continue
        else:
            dp[w] = max( dp[w - weight] + value,  dp[w] )
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0]*(capacity+1)

for i in range(1, num + 1):
    weight, value = map(int, input().split())
    for w in range(capacity,weight - 1,-1):
        if w < weight:
            continue
        else:
            dp[w] = max( dp[w - weight] + value,  dp[w] )
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0]*(capacity+1)

for i in range(1, num + 1):
    weight, value = map(int, input().split())
    for w in range(capacity,-1,-1):
        if w < weight:
            continue
        else:
            dp[w] = max( dp[w - weight] + value,  dp[w] )
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0 for i in range(capacity + 1)]

for _ in range(1, num + 1):
    weight, value = map(int, input().split())
    for w in range(capacity,-1,-1):
        if w - weight >= 0:
            dp[w] = max( dp[w - weight] + value,  dp[w] )
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = map(int, input().split())
dp = [0 for i in range(capacity + 1)]

for i in range(1, num + 1):
    weight, value = map(int, input().split())
    for w in range(capacity,-1,-1):
        if w - weight >= 0:
            dp[w] = max( dp[w - weight] + value,  dp[w] )
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = list(map(int, input().split()))
dp = [0 for i in range(capacity + 1)]

for i in range(1, num + 1):
    weight, value = list(map(int, input().split()))
    for w in range(capacity,-1,-1):
        if w - weight >= 0:
            dp[w] = max( dp[w - weight] + value,  dp[w] )
print(dp[capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = list(map(int, input().split()))
dp = [[0 for i in range(capacity + 1)] for j in range(num + 1)]

for i in range(1, num + 1):
    weight, value = list(map(int, input().split()))
    for w in range(capacity + 1):
        if w - weight >= 0:
            dp[i][w] = max( dp[i-1][w - weight] + value,  dp[i-1][w] )
        else:
            dp[i][w] = dp[i-1][w]
print(dp[num][capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = list(map(int, input().split()))
actions = [[0 for i in range(capacity + 1)]]
for i in range(num):
    actions.append(list(map(int, input().split())))

dp = [[0 for i in range(capacity + 1)] for j in range(num + 1)]
for i in range(1, num + 1):
    for w in range(capacity + 1):
        if w - actions[i][0] >= 0:
            dp[i][w] = max( dp[i-1][w - actions[i][0]] + actions[i][1],  dp[i-1][w] )
        else:
            dp[i][w] = dp[i-1][w]
print(dp[num][capacity])

Submission status: Time Limit Exceeded

Solution code:
num, capacity = list(map(int, input().split()))
actions = [[0 for i in range(capacity + 1)]]
for i in range(num):
    actions.append(list(map(int, input().split())))

dp = [[0 for i in range(capacity + 1)] for j in range(num + 1)]
for i in range(1, num + 1):
    for w in range(capacity + 1):
        if w - actions[i][0] >= 0:
            dp[i][w] = max( dp[i-1][w - actions[i][0]] + actions[i][1],  dp[i-1][w] )
        else:
            dp[i][w] = dp[i-1][w]
print(max( dp[num] ))

Submission status: Time Limit Exceeded

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

ans = 0
DP = [0] * (W + 1)
for i in range(N):
    dp = []
    w, v = map(int, input().split())
    for j, element in enumerate(DP):
        if j >= w:
            dp.append(max(DP[j - w] + v, element))
        else:
            dp.append(element)
    DP = dp.copy()

print(max(DP))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as c
import sys
input = sys.stdin.readline

N,M = map(int,input().split())
W,V = [],[]
for i in range(N):
    w,v = map(int,input().split())
    W.append(w),V.append(v)

ans = 0
for i in range(1,N+1):
    for a,b in zip(c(V,i),c(W,i)):
        if sum(b) > M:
            continue
        ans = max(ans,sum(a))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,w_max=map(int,input().split())
W,V=[],[]
for i in range(n):
  w,v=map(int,input().split())
  W.append(w)
  V.append(v)
DP=[[0 for i in range(w_max+1)] for j in range(n)]
for i in range(w_max+1):
  if i>=W[0]:
    DP[0][i]=V[0]
for i in range(1,n):
  for j in range(w_max+1):
    if j>W[i]:
      DP[i][j]=max(DP[i-1][j],DP[i-1][j-W[i]]+V[i])
    elif j==W[i]:
      DP[i][j]=max(DP[i-1][j],V[i])
    else:
      DP[i][j]=DP[i-1][j]
print(DP[n-1][w_max])