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: Wrong Answer

Solution code:
N,W = map(int,input().split())
Weight = []
Value = []
for i in range(N):
    tmp_w,tmp_v = map(int,input().split())
    Weight.append(tmp_w)
    Value.append(tmp_v)


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

Submission status: Wrong Answer

Solution code:
N,W = map(int,input().split())
W_V = [0 for i in range(N)]
for i in range(N):
    tmp_w,tmp_v = map(int,input().split())
    W_V[i] = [tmp_v,tmp_w]
ans = 0
Weight = 0
W_V.sort(reverse=True)

for i in range(N):
    if Weight+W_V[i][1]<=W:
        ans += W_V[i][0]
        Weight += W_V[i][1]
print(ans)

Submission status: Wrong Answer

Solution code:
#EDPC
N,W = map(int,input().split())
WV = [list(map(int,input().split())) for i in range(N)]
dp = [0 for i in range(W+1)]
for i in range(N):
    w,v = WV[i][0],WV[i][1]
    for j in range(W):
        if j == 0 and w <= W:
            dp[w] = max(v,dp[w])
        else:
            if dp[j] != 0 and j+w <= W:
                dp[j+w] = max(dp[j]+v,dp[j+w])
print(max(dp))

Submission status: Wrong Answer

Solution code:
#EDPC
N,W = map(int,input().split())
WV = [list(map(int,input().split())) for i in range(N)]
dp = [0 for i in range(W+1)]
for i in range(N):
    w,v = WV[i][0],WV[i][1]
    for j in range(W):
        if j == 0 and w <= W:
            dp[w] = v
        else:
            if dp[j] != 0 and j+w <= W:
                dp[j+w] = dp[j]+v
print(max(dp))

Submission status: Wrong Answer

Solution code:
#EDPC
N,W = map(int,input().split())
WV = [list(map(int,input().split())) for i in range(N)]
dp = [0 for i in range(W+1)]
for i in range(N):
    w,v = WV[i][0],WV[i][1]
    for j in range(W):
        if j == 0:
            dp[j+w] = v
        else:
            if dp[j] != 0 and j+w <= W:
                dp[j+w] = dp[j]+v
print(max(dp))

Submission status: Wrong Answer

Solution code:
n, maxweight = [int(i) for i in input().split()]
items = [[int(i) for i in input().split()] for j in range(n)]
dp = [[0 for i in range(2)] for j in range(n)]
dp[0] = items[0]

for i in range(1, n):
    temp = []
    for j in range(i+1):
        if dp[i-j][0] + items[i][0] <= maxweight:
            temp.append([dp[i-j][0] + items[i][0], dp[i-j][1] + items[i][1]])
    maxdp = temp.index(max(temp))
    dp[i][0], dp[i][1] = temp[maxdp][0], temp[maxdp][1]

print(dp[-1][1])

Submission status: Wrong Answer

Solution code:
def number():
    return int(input())


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


def seq_space(n):
    return [list(map(int, input().split())) for _ in range(n)]


N, W = space()
WV = seq_space(N)

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

for i in range(1, N+1):
    w, v = WV[i-1]
    for j in range(w, W+1):
        DP[i][j] = max(DP[i-1][j-w] + v, DP[i-1][j])

print(max(DP[-1]))

Submission status: Wrong Answer

Solution code:
def number():
    return int(input())


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


def seq_space(n):
    return [list(map(int, input().split())) for _ in range(n)]


N, W = space()
WV = seq_space(N)

DP = [[0] * (W+1) for _ in range(N+1)]
print(len(DP), len(DP[0]))

for i in range(1, N+1):
    w, v = WV[i-1]
    for j in range(W+1):
        if j - w < 0:
            DP[i][j] = DP[i-1][j]
        else:
            DP[i][j] = max(DP[i-1][j-w] + v, DP[i-1][j])

print(DP)

Submission status: Wrong Answer

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

dp = [0] * (W + 1)
mw = 0
for w, v in Bs:
    mw = min(W, mw + w)
    for i in range(mw, w - 1, -1):
        dp[i] = max(dp[i], dp[i - w] + v)

print(dp[W])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

n, k = map(int, sys.stdin.readline().split())

dp = np.zeros((n+1, k+1))

for i in range(1,n+1):
    w, v = map(int, sys.stdin.readline().split())
    dp[i] = dp[i-1]
    dp[i][w:] = np.maximum(dp[i-1][:-w]+v, dp[i-1][w:])

print(np.max(dp[n]))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

n, k = map(int, sys.stdin.readline().split())

dp = np.zeros((n+1, k+1))

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

Submission status: Wrong Answer

Solution code:
import sys

n, k = map(int, sys.stdin.readline().split())

dp = [0 for i in range(k+1)]


for i in range(n):
    w, v = map(int, sys.stdin.readline().split())
    
    temp = dp.copy()
    for j in range(k+1):
        if j+w <= k:
            dp[j+w] = max(temp[j+w], temp[j]+v)
            
print(max(dp))
print(dp)

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
dp = [-1 for _ in range(w + 1)]
dp[0] = 0
ans = 0
for w, v in (map(int, input().split()) for _ in range(n)):
    for i in range(len(dp) - 1, w - 1, -1):
        if dp[i - w] >= 0:
            dp[i] = max(dp[i - w] + v, v)
            ans = max(ans, dp[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
wv = [tuple(map(int, input().split())) for _ in range(n)]
dp = [-1 for _ in range(w + 1)]
dp[0] = 0
for w, v in wv:
    for i in [i for i, v in enumerate(dp) if v >= 0]:
        if i + w > len(dp) - 1:
            break
        if dp[i] >= 0:
            dp[i + w] = max(dp[i] + v, v)
print(max(dp))

Submission status: Wrong Answer

Solution code:
def dp():
    n,w = map(int,input().split())
    lis = (map(int,input().split()) for i in range(n))
    
    dpp = [0] * (w + 1)
    for weight, value in lis:
        for j in range(w,weight-1,-1):
            dp = dpp[j-w] + value
            if dpp[j] < dp:
                dpp[j] = dp
    return max(dpp)
print(dp())

Submission status: Wrong Answer

Solution code:
n, weightMax = map(int, input().split())
itemList = [[0 for _ in range(100**3+1)] for _ in range(n+1)]

for i in range(n):
    w,v = list(map(int, input().split()))
    for j in range(100**3+1):
        if j-v >= 0:
            itemList[i+1][j] = max([itemList[i][j],itemList[i][j-v] + w])
        else:
            itemList[i+1][j] = itemList[i][j]
for i in range(100**3+1):
    if itemList[n][i]==weightMax+1:
        print(i-1)
        break

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def resolve():
    (N, W) = list(map(int, input().split(" ")))
    wvs = [list(map(int, input().split(" "))) for _ in range(N)]
    dps = [[0] * W for _ in range(N)]

    for i in range(N):
        for j in range(W):
            if j >= wvs[i][0]:
                dps[i][j] = wvs[i][1] if i==0 else dps[i-1][(j+1)-wvs[i][0]] + wvs[i][1]
            dps[i][j] = dps[i][j] if i==0 else max(dps[i][j], dps[i-1][j])

    print(dps[-1][W-1])

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    (N, W) = list(map(int, input().split(" ")))
    wvs = [list(map(int, input().split(" "))) for _ in range(N)]
    dps = [[0] * W for _ in range(N)]

    for i in range(N):
        for j in range(W):
            if j >= wvs[i][0]:
                dps[i][j] = wvs[i][1] if i==0 else dps[i-1][(j+1)-wvs[i][0]] + wvs[i][1]
            dps[i][j] = dps[i][j] if i==0 else max(dps[i][j], dps[i-1][j])

    print(max(dps[-1]))

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

for i in range(N):
  w[i], v[i] = [int(_) for _ in input().split()]
  
dp = [[0] * (W+1) for _ in range(N+1)]
# dp[i][j] i番目までの荷物で重さがjを超えないように選んだときの価値の最大値
for i in range(N):
  for j in range(W+1):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    if j + w[i] <= W:
      dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]], dp[i+1][j] + v[i])
      
print(dp[N][W])

Submission status: Wrong Answer

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

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

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

      
print(dp[N][W])

Submission status: Wrong Answer

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

dp = [[0]*(W+1)]*(N+1)

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

print(max(dp[N]))

Submission status: Wrong Answer

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

dp = [[0]*(W+1)]*(N+1)

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

print(max(dp[N]))

Submission status: Wrong Answer

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

dp = [[0]*(W+1)]*(N+1)

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

print(max(dp[N]))

Submission status: Wrong Answer

Solution code:
from numpy import*
N,W=map(int, input().split())
dp=zeros(W+1)
for i in range(N):
    w,v=map(int, input().split())
    dp[w:]=maximum(dp[w:], dp[:-w]+v)
    print(dp)
print(int(dp[W]))

Submission status: Wrong Answer

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

# dp[i個目][重さ合計] = 最大価値
dp = [[0 for i in range(w+1)] for _ in range(n+1)]
# print(dp)
for i in range(1, n+1):
	for iw in range(w):
		iw_im1 =  iw - wv[i-1][0]
		if(iw_im1 >= 0):
			dp[i][iw] = max(dp[i-1][iw_im1] + wv[i-1][1], dp[i-1][iw])
		else:
			dp[i][iw] = dp[i-1][iw]

print(max(dp[n]))

Submission status: Wrong Answer

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

import numpy as np
ndp = np.zeros(W+1,dtype= np.int64)
print(ndp)
for w,v in zip(w,v):
    np.maximum(ndp[:-w] + v, ndp[w:], out = ndp[w:])
print(ndp[-1])

Submission status: Wrong Answer

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

value = [0] * (W + 1)

for _ in range(N):
  w, v = map(int, input().split())
  m = max(value[:W-w+1])
  for i in range(1, W-w+1):
    if value[i] == m:
      value[w+i] = max(v + value[i], value[w+i])
  value[w] = max(v, value[w])
print(max(value))

Submission status: Wrong Answer

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

value = [0] * (W + 1)

for _ in range(N):
  w, v = map(int, input().split())
  i = value.index(max(value[:W-w+1]))
  value[w+i] = max(v + value[i], value[w+i])
  value[w] = max(v, value[w])
print(max(value))

Submission status: Wrong Answer

Solution code:
def main():
    N, W = map(int, input().split())
    dp = [[0] * (W + 1) for _ in range(N + 1)]
    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],
                    dp[i][j - w] + v
                )
            else:
                dp[i + 1][j] = dp[i][j]
    print(dp)


main()

Submission status: Wrong Answer

Solution code:
def main():
    N, W = map(int, input().split())
    WV = []
    for _ in range(N):
        w, v = map(int, input().split())
        WV.append((w, v))
    dp = [[0] * (W + 1) for _ in range(N + 1)]
    for i in range(N):
        for j in range(W + 1):
            if j >= WV[i][0]:
                dp[i + 1][j] = max(
                    dp[i][j],
                    dp[i][j - WV[i][0]] + WV[i][1]
                )
            else:
                dp[i + 1][j] = dp[i][j]
    print(dp)


main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n, max_w = map(int, input().split())

dp = [-1 for _ in range(max_w + 1)]
dp[0] = 0

for i in range(n):
    w, v = map(int, input().split())
    queue = list()
    for i in range(0, max_w - w + 1):
        if dp[i] > -1:
            queue.append(i)
    for i in queue:
        dp[i + w] = max(dp[i + w], dp[i] + v)

print(max(dp))

Submission status: Wrong Answer

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

dp = [[0 for i in range(Weight+1)] for j in range(N+1)]
for i in range(Weight+1):
    dp[0][i] = 0

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

print(dp[N][Weight])

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
N, W = map(int, input().split())

dp = Counter()
dp[0] = 0

for _ in range(N):
    w, v = map(int, sys.stdin.readline().split())
    for k in dp.copy().keys():
        if k + w <= W:
            dp[k + w] = max(dp[k + w], dp[k] + v)

print(max(dp.values()))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

input = sys.stdin.readline

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

dp = np.zeros((N+1, W+1))

for i in range(N):
    for w in range(W+1):
        if w >= Weight[i]:
            dp[i+1, w] = dp[i, w - Weight[i]] + V[i]
        else:
            dp[i+1, w] = dp[i, w]

print(int(dp[N, W]))

Submission status: Wrong Answer

Solution code:
n,w = map(int,input().split())
old = dict()
old[0]=0
for _ in range(n):
  wc,v = map(int,input().split())
  new = old.copy()
  for x in old:
    if x + wc <= w:
      new[wc+x] = max(new.get(wc+x,0), old[x]+v)
    old = new
print(max(old.values()))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,W = [int(i) for i in input().split()]
wv = [[int(i) for i in input().split()] for j in range(n)] # 縦「n」行

dp = [0]*(W+1)
dp[0]=0
newdp = [0]*(W+1)
for i in range(n):
    w, v = wv[i]
    for j in range(W):
        if j+w > W: break
        newdp[j+w] = max(dp[j] + v, dp[j+w])
    newdp, dp = dp, newdp
#    print(dp)

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
D - Knapsack 1
https://atcoder.jp/contests/dp/tasks/dp_d

"""
import sys


def solve(N, W, items):
    ans = {0: 0}
    for w, v in items:
        u = dict()
        for x, y in ans.items():
            if x+w <= W:
                u[x+w] = y+v
        ans.update(u)
    return max([v for v in ans.values()])


def main(args):
    N, W = map(int, input().split())
    items = [[int(i) for i in input().split()] for _ in range(N)]
    ans = solve(N, W, items)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
import numpy as np
N, W = map(int, input().split())

dp = np.zeros(W+1, dtype=int)

for i in range(N):
    w, v = map(int, input().split())
    np.maximum(dp[:W-w+1]+v, dp[w:], out=dp[w:])
    print(dp)
print(dp[-1])

Submission status: Wrong Answer

Solution code:
def d():
    N, W = map(int, input().split())
    wv = [list(map(int, input().split())) for _ in range(N)]
    dp = [[0] * (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])
    return dp[i][j]


if __name__ == '__main__':
    ans = d()
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def knapsack():
    n, max_w = map(int, input().split())
    dp = defaultdict(int)
    dp[0] = 0
    for _ in range(n):
        w, v = map(int, input().split())
        for item in list(dp.keys()):
            dp[item+w] = max(dp[item] + v, dp[item+w])
    max_v = 0
    for w, v in dp.items():
        if w <= max_w:
            max_v = max(v, max_v)
    print(max_v)


knapsack()

Submission status: Wrong Answer

Solution code:
import numpy as np

N, W = list(map(int, input().split()))
dp = np.zeros((N, W), dtype=int)
w = np.zeros(N, dtype=int)
v = np.zeros(N, dtype=int)

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

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import io
import os


def main():
    n, m = map(int, input().split())

    dp = [0] * (m + 1)

    wi, vi = map(int, input().split())
    for i in range(wi, m + 1):
        dp[i] = vi

    for i in range(n - 1):
        vi, wi = map(int, input().split())
        for j in reversed(range(wi, m + 1)):
            dp[j] = max(vi + dp[j - wi], dp[j])

    print(dp[-1])


input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
main()

Submission status: Wrong Answer

Solution code:
N, W = map(int,  input().split())
wv_array = [[int(x) for x in input().split()]for y in range(N)]
wv_dict = {wv_array[0][0]:wv_array[0][1]}
for w, v in wv_array[1:]:
    tmp_dict = dict(wv_dict)
    wv_dict[w] = v
    for ww, vv in tmp_dict.items():
        if ww + w > W:
            continue
        wv_dict[ww+w] = wv_dict[w] + vv
print(max(wv_dict.values()))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def inpl(): return [int(i) for i in input().split()]

N,W = inpl()
H = {0: 0}
for i in range(N):
    w, v = inpl()
    for iw, iv in H.copy().items():
        if iw+w <=W:
            H[iw + w] = max(H.get(iw + w, 0),iv+v)

print(H.values())

Submission status: Wrong Answer

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(b,d[k]+v)
print(max(d.values()))

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split());h,u=[],[]
for i in range(n):
  aa,bb=map(int,input().split())
  h.append(aa);u.append(bb)
dp=[[0for _ in range(w)]for _ in range(n)]
for i in range(n):
  for j in range(w):
    if i==0:
      if h[0]<=j:dp[i][j]=u[0]
    else:
      if j-h[i]>=0:dp[i][j]=max(dp[i-1][j],dp[i-1][j-h[i]]+u[i])
      else:dp[i][j]=dp[i-1][j]
print(dp[n-1][w-1])

Submission status: Wrong Answer

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 or w+wv[i][0] > W:
        return 0
    if dp[i][w] != -1:
        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: Wrong Answer

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

DP = [0]*(W+1) #0~W
#各iにつき、残り重さがWの時の最大価値をDPに入れる


#i=N-1までいった時のmax(DP)が答え
for i in range(0, N, 1): #0~N-1
  w, v = wv[i]
  #残りwei(W-DPの2個目の添字)がw未満の場合入れられない
  #入れない場合は変えなければいい
  for nw in range(w, W+1, -1):
    iretaV = DP[nw-w] + v
    if iretaV > DP[nw]:
      DP[nw] = iretaV

print(max(DP))

Submission status: Wrong Answer

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 range(w,W+1):
    val[j] = max(val[j], val[j-w] + v) 
print(val[W])

Submission status: Wrong Answer

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(dp[N-1][W])

Submission status: Wrong Answer

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)]
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(dp[N-1][W])

Submission status: Wrong Answer

Solution code:
#問題 D
import numpy as np
N, W = map(int,input().split())
#普通に書くとTLEだったのでNumpy化
dp = np.zeros(W+1)
for i in range(N):
    w,v = map(int,input().split())
    dp[w:] = np.maximum(dp[w:],dp[:-w]+v)
print(dp[-1])


#dpをN*(W+1)配列とかにしてもnumpyなら間に合う。コピーに時間がかかるのかな？

# numpy化してもやっぱりTLEなので、どうもコピーが悪かったらしい
# 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(int(VAL[W]))

# 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
    # listへのアクセスは重いらしい。 https://qiita.com/pegasusBS15/items/620985fdc21c75ad8026

Submission status: Wrong Answer

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(VAL[W])
    # listをコピーするときは注意　https://qiita.com/utgwkk/items/5ad2527f19150ae33322

Submission status: Wrong Answer

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 = [[0] * (W + 1) for _ in range(N + 1)]
    for i in range(N):
        for ww in range(W + 1):
            if ww - ws[i] < 0:
                dp[i + 1][ww] = dp[i][ww]
            else:
                dp[i + 1][ww] = max(
                    dp[i][ww],
                    dp[i][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: Wrong Answer

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]
        if dp[w] < tmp:
          dp[w] = tmp
print(dp[capacity])

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
lis = []
for i in range(n):
  lis.append(list(map(int, input().split())))
for i in lis:
  i.append(i[1]/i[0])
dp = 0
for i, j, _ in sorted(lis, key=lambda x: -x[2]):
  if w >= i:
    dp += j
    w -= i
print(dp)

Submission status: Wrong Answer

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:
            if w <= capacity:
                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: Wrong Answer

Solution code:
N,W = map(int, input().split())
dp = [-1]*(W+1)
dp[0] = 0
for _ in range(N):
    w,v = map(int,input().split())
    for i in range(1,W+1):
        if i-w < 0:
            continue
        dp[i] = max(dp[i-w], dp[i-w]+v)
print(max(dp))

Submission status: Wrong Answer

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)
w_tot=sum(W)
DP=[[-float("inf") for i in range(w_tot+1)] for j in range(n)]
for i in range(n):
  DP[i][0]=0
DP[0][W[0]]=V[0]

for i in range(1,n):
  for j in range(w_tot+1):
    if j>=W[i]:
      DP[i][j]=max(DP[i-1][j],DP[i-1][j-W[i]]+V[i])
    else:
      DP[i][j]=DP[i-1][j]
print(DP[n-1][w_max])

Submission status: Wrong Answer

Solution code:
N,W = map(int,input().split())
ws, vs = [], []

for i in range(N):
    w,v = map(int,input().split())
    ws.append(w)
    vs.append(v)

# 重さの総和の値を固定して、その値の最大値を求める組み合わせを
# dp[i][sum_wN]とする
dp = [[0 for _ in range(W+1)] for _ in range(N)]

# i番目についてのやつ
ans = 0
for i in range(N):
    for j in range(W+1):
        # あるWについて
        # i番目をknapsacに加える場合

        dp[i][j] = dp[i-1][j-ws[i]] + vs[i]
        if j - ws[i] >= 0:
            if dp[i-1][j-ws[i]] + vs[i] > dp[i][j]:
                dp[i][j] = dp[i-1][j-ws[i]] + vs[i]
        else:
            dp[i][j] = dp[i-1][j]
    ans = max(ans, max(dp[i]))

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys 
input = sys.stdin.readline

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

# w=0がそのままindexになる
dp = -np.ones(W+1,dtype=np.int32)
dp[0] = 0
                
for wv in wvList:
  w,v = wv
  A = -np.ones_like(dp,dtype=np.int32)
  tp = dp.copy()
  tp[tp>=0] += v
  A[w:] = tp[:W-w+1]
  dp = np.maximum(A,dp)
print(np.max(dp))

Submission status: Wrong Answer

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

ans = [[-1 for i in range(N+1)]  for i in range(W+1)]

Submission status: Wrong Answer

Solution code:
import sys

# inputを高速化する。
input = sys.stdin.readline

# 入力
N, W = map(int, input().split())
lst_weight_value = [list(map(int, input().split())) for _ in range(N)]
print(lst_weight_value)

# def chmax(a, b):
#     if a >= b:
#         return a
#     else:
#         return b

# 重さwの品物の入れ方で達成できる価値の最大値
dp = [0] * (W + 1)

for i in range(N):
    weight, value = lst_weight_value[i]
    for w in range(W, -1, -1):
        _w = w + weight
        _v = dp[w] + value
        if _w > W:
            continue
        if _v <= dp[_w]:
            continue
        dp[_w] = _v

print(dp[-1])

Submission status: Wrong Answer

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

n, cap = map(int, input().split())
v_w = {0: 0}
for _ in range(n):
    w, v = map(int, input().split())
    for wk, vk in sorted(v_w.items(),key=lambda x:x[1]):
        nw = w + wk
        if nw > cap: break
        if nw in v_w:
            v_w[nw] = max(v_w[nw], v + vk)
        else:
            v_w[nw] = v + vk
print(max(v_w.values()))