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:
import numpy as np

N, W = map(int, input().split())
wv = np.array([[int(i) for i in input().split()] for j in range(N)])

max_v = 10**3*(N+1)
dp = np.full([N+1,max_v], np.inf)
dp[0,0] = 0
 
for i in range(N):
    # 遷移元なし
    dp[i+1,:wv[i,1]] = dp[i,:wv[i,1]]
    # 遷移元あり
    dp[i+1,wv[i,1]:] = np.minimum(dp[i,wv[i,1]:], dp[i,:max_v-wv[i,1]]+wv[i,0])

print(np.max(np.where(dp <= W)))

Submission status: Wrong Answer

Solution code:
import numpy as np

# 入力
N, W = map(int, input().split())
wv = np.array([[int(i) for i in input().split()] for j in range(N)])

# DPテーブル
dp = np.zeros((N+1)*(W+1), dtype=int).reshape((N+1), (W+1))

# DPループ
for i in range(N):    # i番目の荷物について検討する
    dp[i+1][wv[i-1][0]:] = np.maximum(dp[i][wv[i-1][0]:], dp[i][:(W+1)-wv[i-1][0]] + wv[i-1][1])
        
# 二次元リストの一番右下が最適解
print(dp[N][W])

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
dp = [-1 for _ in range(w+1)]
dp[0] = 0
items = []
for _ in range(n):
    weight, value = map(int, input().split())
    items.append([weight, value])
items.sort()
max_weight = 0
for weight, value in items:
    max_weight += weight
    for i in range(min(max_weight, w+1)):
        if dp[i] == -1:
            continue
        elif weight + i <= min(max_weight, w):
            dp[i+weight] = max(dp[i+weight], dp[i] + value)

print(dp[w])

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
dp = [-1 for _ in range(w+1)]
dp[0] = 0
items = []
for _ in range(n):
    weight, value = map(int, input().split())
    items.append([weight, value])
items.sort()
max_weight = 0
for weight, value in items:
    max_weight += weight
    for i in range(min(max_weight, w+1)):
        if dp[i] == -1:
            continue
        elif weight + i <= min(max_weight, w):
            dp[i+weight] = max(dp[i+weight], dp[i] + value)

print(dp[w])
print(dp)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,W = map(int,input().split())
"""
DP = np.zeros(W+1,dtype=int)
for _ in range(N):
  w,v = map(int,input().split())
  np.maximum(DP[:-w]+v,DP[w:],out=DP[w:])
print(DP.max())
"""
DP= np.full(N*10000+1,W+1)
DP[0] = 0
for _ in range(N):
  w,v = map(int,input().split())
  np.minimum(DP[:-v]+w,DP[v:],out = DP[v:])
print(np.max(np.where(DP<=W)))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    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())
    # https://qiita.com/drken/items/dc53c683d6de8aeacf5a#dp-%E5%AE%9F%E8%A3%85%E3%81%AE%E9%A0%86%E5%BA%8F
    
    
    INF = 10**10  # INF+INFを計算してもオーバーフローしない範囲で大きく
    # dpテーブル dp[i][j]:= i番目までの荷物の中から重さj以下で選ぶ通り数
    dp = [[0]*(W+1) for _ in range(N+1)]
    """
    初期化の値は，
        最小化問題：INF, 
        最大化問題：-INF（0以上の値しか取らないなら-1, あるいは0でもいいかも)
        数え上げ問題：0
        確率問題：0
        Yes/No判定問題:False
    """
    
    # 初期条件
    # dp[0][0] = 0
    
    # ループ
    for i in range(1,N+1):
        for j in range(W+1):
    
            """
            dp[from] の値の更新が完了していればノード from からノード to への緩和を行っていい．貰うdpであれ配るdpであれ．
            両者の違いを意識すべきケースはcf. 
            https://qiita.com/drken/items/ace3142967c4f01d42e9#%E8%B2%B0%E3%81%86-dp-%E3%81%A8%E9%85%8D%E3%82%8B-dp-%E3%81%AE%E6%AF%94%E8%BC%83
    
            # 遷移式がたくさんある場合この中でさらにforループを回したりする cf. https://atcoder.jp/contests/dp/tasks/dp_b
            """
            # i番目選ぶ
            if j-w[i-1]>=0:
                dp[i][j] = max(dp[i][j], dp[i-1][j-w[i-1]]+v[i-1])
            # 選ばない
            else:
                dp[i][j]=max(dp[i][j],dp[i-1][j-w[i-1]])
    
    # 出力
    print(dp[N][W])
    

    
resolve()

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
g = []
for i in range(n):
    g.append(list(map(int, input().split())))
m = [[0 for j in range(w+1)]for i in range(n+1)]
for i in range(n):
    for j in range(w+1):
        if j-g[i-1][0] < 0:
            m[i+1][j] = m[i][j]
        else:
            m[i+1][j] = max(m[i][j], m[i][j-g[i][0]]+g[i][1])
print(m[n][w])

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
g = []
for i in range(n):
    g.append(list(map(int, input().split())))
m = [[0 for j in range(w+1)]for i in range(n+1)]
for i in range(n+1):
    for j in range(w+1):
        if j-g[i-1][0] < 0:
            m[i][j] = m[i-1][j]
        else:
            m[i][j] = max(m[i-1][j], m[i-1][j-g[i-1][0]]+g[i-1][1])
print(m[n][w])

Submission status: Wrong Answer

Solution code:
import numpy as np
n,W,*wv=map(int,open(0).read().split())
w=np.array(wv[::2])
v=np.array(wv[1::2])
dp=np.zeros([n+1,W+1],dtype=int)
for i in range(1,n+1):
  now=np.zeros(W+1,dtype=int)
  print(dp[i-1],now)
  now[w[i-1]:]=dp[i-1][:-w[i-1]]+v[i-1]
  print(dp[i-1],now)
  dp[i]=np.maximum(dp[i-1],now)
print(dp[-1][-1])

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

for i in range(n):
    w[i],v[i] = [int(e) for e in input().split()]

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

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

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

Submission status: Wrong Answer

Solution code:
def func(x):
    return x[0]

n,w = map(int,input().strip().split(" "))

arr = []
for i in range(0,n):
    wi,vi = map(int,input().strip().split(" "))
    arr.append([vi/wi,vi,wi])

arr.sort(key=func,reverse=True)
ans = 0
for i in arr:
    if w-i[2] >= 0:
        ans += i[1]
        w -= i[2]
print(int(ans))

Submission status: Wrong Answer

Solution code:
def func(x):
    return x[0]

n,w = map(int,input().strip().split(" "))

arr = []
for i in range(0,n):
    wi,vi = map(int,input().strip().split(" "))
    arr.append([vi/wi,vi,wi])

arr.sort(key=func,reverse=True)
ans = 0
for i in arr:
    if w-i[2] >= 0:
        ans += i[1]
        w -= i[2]
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
def func(x):
    return x[0]

n,w = map(int,input().strip().split(" "))

arr = []
for i in range(0,n):
    wi,vi = map(int,input().strip().split(" "))
    arr.append([vi/wi,vi,wi])

arr.sort(key=func)
ans = 0
for i in arr:
    if w-i[2] >= 0:
        ans += i[1]
        w -= i[2]
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
def knapSack(W, wt, val, n): 
    K = [[0 for x in range(W+1)] for x in range(n+1)] 
  
    # Build table K[][] in bottom up manner 
    for i in range(n+1): 
        for w in range(W+1): 
            if i==0 or w==0: 
                K[i][w] = 0
            elif wt[i-1] <= w: 
                K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]],  K[i-1][w]) 
            else: 
                K[i][w] = K[i-1][w] 
  
    return K[n][W] 
  
n, W = map(int, input().split()) 
val = [] 
wt = [] 

for _ in range(n):
    w, v = map(int, input().split()) 
    val.append(v)
    wt.append(w)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, W = map(int, input().split())
w, v = [], []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)

DP = np.zeros((N+1,W+1), dtype=np.int64)

for i in range(N):
    DP[i+1][w[i]:] = np.maximum(DP[i][:-w[i]]+ v[i], DP[i][w[i]:])

#print(DP)
print(DP[-1][-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys

import fractions
import copy
import bisect
import math
import numpy as np
import itertools
from itertools import combinations_with_replacement
#import math#数学的計算はこれでいける。普通に0.5乗しても計算可能
#w=input()
from operator import itemgetter
from sys import stdin
#input = sys.stdin.readline#こっちの方が入力が早いが使える時に使っていこう
from operator import mul
from functools import reduce
from collections import Counter
#from collections import deque

#input = stdin.readline


j=0
k=0
n=3
r=1
a=[0]
#n=int(input())
#r=int(input())
#print(M)
#A=int(input())
#B=int(input())
#print(N)
"1行1つの整数を入力を取得し、整数と取得する"


#number_list=list(map(int, input().split(" ")))#数字の時
#print(number_list)
"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#メモ
for i in number_list:#こっちの方がrage使うより早いらしい
    print(number_list[i-1])#

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"
'''

'''
#array=[input().split()for i in range(N)]
#print(type(array[0][0]))
#print(array)
"正方行列にstr型の値を入力"
'''

#brray=[list(map(int, input().split(" ")))for i in range(N)]
#print(type(brray[0][0]))
#print(brray)
'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"
#以下に別解を記載
#N, M = [int(i) for i in input().split()]
'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''




#s=input()
#a=[int(i) for i in s]
#print(a[0])
#print([a])
#単数値．桁ごとに分割したい．入力と出力は以下の通り
#イメージとして1桁ごとにリストに値を入れているかんじ
#intを取ると文字列分解に使える
'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()#str style 何個でもいけることが多い
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''


#a= stdin.readline().rstrip()
#print(a.upper())
"aという変数に入っているものを大文字にして出力"


#a,b=map(int, input().split())  #int style 複数数値入力　「A B」みたいなスペース空いた入力のとき

#なんかうまく入力されるけど
#a=[[int(i) for i in 1.strip()]for 1 in sys.stdin]
#a = [[int(c) for c in l.strip()] for l in sys.stdin]]
#print(a)
#複数行の数値を入力して正方行列を作成


##############################################################################################
##############################################################################################
#under this line explains example calculation

'''
コンビネーションの組み合わせの中身を出力する形式
for i in itertools.combinations(brray, 2)
combinationsをpermutationsにすれば順列になる
今回なら(abc133B)
入力
1 2
5 5
-2 8
出力
[[1, 2], [5, 5], [-2, 8]]
もちろん一次元リストでも使えるし
何よりiもリストのように使えるので便利
'''

#nCr combination
'''
def cmb(n,r):
    #When n < r , this function isn't valid
    r= min(n-r,r)
    #print(n,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n-r, -1))
    #flochart mul(n,n-1)=x
    #next     mul(x,n-2)........(n-r+1,n-r)
    #mul read a,b and returns a*b

    under = reduce(mul, range(1, r+1))
    #print(over, under)
    #reduce is applied mul(1,2)=2
    #next   mul(2,3)=6
    #next   mul(6,4)=4.........last(r!,r+1)=r+1!

    return over // under
    #// is integer divide
    #calc example 5C2
    #over=5*4*3
    #under=3*2*1
a = cmb(n, r)
#print(a)
'''

'''
import itertools
from itertools import combinations_with_replacement

combinationについて
以下の違いを意識しよう
combinations() p, r 長さrのタプル列、ソートされた順で重複なし

combinations_with_replacement() p, r 長さrのタプル列、ソートされた順で重複あり


使用例 　　　　　　　　　　　                  出力　　
combinations('ABCD', 2)　　　　　　　　　　　　AB AC AD BC BD CD

combinations_with_replacement('ABCD', 2)　　 AA AB AC AD BB BC BD CC CD DD
'''



'''
#集計
#example
#a=[2,2,2,3,4,3,1,2,1,3,1,2,1,2,2,1,2,1]
#a=Counter(a)
for i in a.most_common(n):print(i)
#most_common()メソッドは、出現回数が多い要素順にCounterオブジェクトを並び替えます。
#引数にint型の数字nを設定した場合は、出現回数が高い上位ｎ個の要素を返します。
#何も設定しなければ、コンテナ型にあるすべての要素を出現回数の順番に並び替えたタプル型オブジェクトを返します。
#out put
#(2, 8)
 #(1, 6)
#(3, 3)
#(4, 1)
'''


#二部探索(binary search)
#A = [1, 2, 3, 3, 3, 4, 4, 6, 6, 6, 6]
#print(A)
#index = bisect.bisect_left(A, 5) # 7 最も左(前)の挿入箇所が返ってきている
#A.insert(index, 5)
#print(index)
#print(A)
'''
bisect.bisect_left(a, x, lo=0, h=len(a))
引数
a: ソート済みリスト
x: 挿入したい値
lo: 探索範囲の下限
hi: 探索範囲の上限
(lo, hiはスライスと同様の指定方法)
bisect_leftはソートされたリストaに対して順序を保ったままxを挿入できる箇所を探索します。leftが示す通り、aにすでにxが存在している場合は、挿入箇所は既存のxよりも左側になります。また、lo, hiを指定することで探索範囲を絞り込むことも可能です。デフォルトはaの全体が探索対象です。
'''





########################################################################
########################################################################
#b2=a[:]  #1次元のときはコピーはこれで良い
#print(b2)

#a= [[0]*3 for i in range(5)] #2次元配列はこう準備、[[0]*3]*5だとだめ
#b3=copy.deepcopy(a)  #2次元配列はこうコピーする
#print(b3)

def main():


    j=0
    k=0
    #n=int(input())
    #r=int(input())


    "1行1つの整数を入力を取得し、整数と取得する"
    #number_list=list(map(int, input().split(" ")))#数字の時
    #print(number_list)
    "12 21 332 とか入力する時に使う"
    "1行に複数の整数の入力を取得し、整数として扱う"
    n,w=map(int,input().split(" "))


    dp= [[0]*(w+1) for i in range(n+1)]#0からwまでのw+1回計算するから
    #print(dp)＃初期条件が入る分計算回数+1列分必要(この場合は判断すべきものの数)
    brray=[list(map(int, input().split(" ")))for i in range(n)]
    #weght valueの順番で入ってるよ
    #dp=[list(map(int,for i in range(n)]

    #brray[0][0]=[weight][value]
    #print(brray)
    for i in range(n):
        for j in range(w+1):#0からwまでのw+1回計算するから
            #print(brray[i][0], brray[i][1])#この入力大丈夫
            if j >= brray[i][0]:
                dp[i+1][j]=max(dp[i][j], dp[i][j-brray[i][0]]+brray[i][1])

            else:
                dp[i+1][j]=dp[i][j]

    print(max(dp[n][:]))
    print(dp)#最終列がn+1列目であることに注意
    #s=input()
    #a=[int(i) for i in s]#int取るとstrでも行ける
    #print(a)
    '''
    入力
    1234
    出力
    [1, 2, 3, 4]
    '''



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def knapsack(N, W, w, v):
    dp = []
    for i in range(N + 1):
        dp.append([-1] * (W + 1))
    for i in range(N + 1):
        dp[i][0] = 0
    for i in range(W + 1):
        dp[0][i] = 0

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

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

ans = knapsack(N, W, w, v)

print(ans)

Submission status: Wrong Answer

Solution code:
n,w=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(n)]
z=[[0]*(w+1) for _ in xrange(2)]
for i in xrange(n):
    for j in xrange(w+1):
        if a[i][0]<=j:
            z[(i+1)%2][j]=max(z[(i+1)%2][j],z[i%2][j-a[i][0]]+a[i][1])
print max(z[(n)%2])

Submission status: Wrong Answer

Solution code:
n,w=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(n)]
z=[[0]*(w+1) for _ in xrange(2)]
for i in xrange(n):
    for j in xrange(w+1):
        if a[i][0]<=j:
            z[(i+1)%2][j]=max(z[(i+1)%2][j],z[i%2][j-a[i][0]]+a[i][1])
print max(z[(w+1)%2])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

Submission status: Wrong Answer

Solution code:
[n,w] = [int(i) for i in input().split()]
dp = [[0]*(w+1) for i in range(n)]
wv = []
for i in range(n):
    wv.append([int(j) for j in input().split()])
"""
dp[i][w] = v
i : i番目まで
w : それまでの合計
"""

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

Submission status: Wrong Answer

Solution code:
[n,w] = [int(i) for i in input().split()]
dp = [[0]*w for i in range(n)]
wv = []
for i in range(n):
    wv.append([int(j) for j in input().split()])
"""
dp[i][w] = v
i : i番目まで
w : それまでの合計
"""

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
def resolve():
    n, w = map(int, input().split())
    goods = [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 < goods[i][0] + 1:
                dp[i + 1][j] = dp[i][j]

            else:
                dp[i + 1][j] = max(dp[i][j], dp[i][j - goods[i][0] + 1] + goods[i][1])

    print(dp[n][w])
    
resolve()

Submission status: Wrong Answer

Solution code:
n,w=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(n)]
z=[[0]*(w+1)]
for i in xrange(n):
    z.append(z[-1][:])
    for j in xrange(min(w,a[i][0])+1):
		z[i+1][j]=max(z[i+1][j],z[i][j-a[i][0]]+a[i][1])
print max(z[-1])

Submission status: Wrong Answer

Solution code:
import numpy as np

def resolve():
    n, w = map(int, input().split())
    goods = np.array([list(map(int, input().split())) for _ in range(n)])

    dp = np.zeros(1)
    print(knapsack(n - 1, w, goods, dp))



def knapsack(i, j, goods, dp):
    """残りi個の品物から、重さがj以下になるように選んだときの価値の最大値をreturnする関数"""
    if i < 0:
        # もう品物は残っていない
        return 0
    if j < goods[i][0]:
        # 品物が重すぎて入らない
        return knapsack(i - 1, j, goods, dp)
    else:
        # 品物を入れる場合と入れない場合とで、価値が大きい方をreturn 
        return max(knapsack(i - 1, j - goods[i][0], goods, dp) + goods[i][1], knapsack(i - 1, j, goods, dp))

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def knapsack(sizes, vals, volume):
    n = len(sizes)

    # dp[i][j] is the max value at item i while using capacity j
    dp = [[0] * (volume+1) for _ in range(n)] # j is 0...volume

    for i in range(n):
        for j in range(volume+1):
            dp[i][j] = max(dp[i][j], dp[i-1][j-sizes[i]] + vals[i]) if j - sizes[i] >= 0 else max(dp[i][j], dp[i-1][j])

    return dp[-1][-1]


if __name__ == "__main__":
    n, volume = [int(x) for x in input().split()]
    sizes, vals = [], []
    for _ in range(n):
        s, v = [int(x) for x in input().split()]
        sizes.append(s)
        vals.append(v)
    print(knapsack(sizes, vals, volume))

Submission status: Wrong Answer

Solution code:
def knapsack(sizes, vals, volume):
    n = len(sizes)

    dp = [[0] * (volume+1) for _ in range(n)] # j is 0...volume

    for i in range(n):
        for j in range(sizes[i], volume+1):
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-sizes[i]] + vals[i])

    return dp[-1][-1]


if __name__ == "__main__":
    n, volume = [int(x) for x in input().split()]
    sizes, vals = [], []
    for _ in range(n):
        s, v = [int(x) for x in input().split()]
        sizes.append(s)
        vals.append(v)
    print(knapsack(sizes, vals, volume))

Submission status: Wrong Answer

Solution code:
def knapsack(sizes, vals, volume):
    n = len(sizes)

    dp = [[0] * (volume+1) for _ in range(n)] # j is 0...volume

    for i in range(n):
        for j in range(sizes[i], volume+1):
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-sizes[i]] + vals[i])

    #print(dp)
    return dp[-1][-1]


if __name__ == "__main__":
    n, volume = [int(x) for x in input().split()]
    sizes, vals = [0], [0]
    for _ in range(n):
        s, v = [int(x) for x in input().split()]
        sizes.append(s)
        vals.append(v)
    print(sizes, vals)
    print(knapsack(sizes, vals, volume))

Submission status: Wrong Answer

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,w=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(n)]
a.sort()
z=[[0]*(w+1)]
for i in xrange(n):
    z.append(z[-1][:])
    for j in xrange(w+1):
        if a[i][0]<=j:
            z[i+1][j]=max(z[i+1][j],z[i][j-a[i][0]]+a[i][1])
        else:
          break
print max(z[-1])

Submission status: Wrong Answer

Solution code:
def solve(items, w):
    dp = []
    for item in items:
        curr = []
        for i in range(w+1):
            if i - item[0] >= 0:
                if len(dp) > 0:
                    curr.append(max(dp[-1][i], dp[-1][i - item[0]]+item[1]))
                else:
                    curr.append(curr[i-item[0]]+item[1])
            elif len(dp) > 0:
                curr.append(dp[-1][i])
            else:
                curr.append(0)
        dp.append(curr)
    return dp[-1][-1]


if __name__ == "__main__":
    n, w = map(int, input().split())
    items = [[int(x) for x in input().split()] for y in range(n)]
    print(solve(items, w))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

# import os
# path = os.getcwd()
# path = os.path.join(path,"input.txt")
# sys.stdin = open(path)

input = sys.stdin.readline
N,W = map(int,input().split())
w,v = [],[]
for _ in range(N):
    w_,v_ = map(int,input().split())
    w.append(w_)
    v.append(v_)
w,v = np.array(w),np.array(v)
ind = np.argsort(v)
w,v = w[ind[::-1]],v[ind[::-1]]

cost = 0
w_total =0
v_total = 0
for w_,v_ in zip(w,v):
    if w_ + w_total <= W:
        v_total += v_
        w_total += w_
    else: 
        continue
    if w_total == W:
        break

w_total_inv = 0
v_total_inv = 0
for w_,v_ in zip(w[::-1],v[::-1]):
    if w_ + w_total_inv <= W:
        v_total_inv += v_
        w_total_inv += w_
    else: 
        continue
    if w_total_inv == W:
        break
print(max(v_total,v_total_inv))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

# import os
# path = os.getcwd()
# path = os.path.join(path,"input.txt")
# sys.stdin = open(path)

input = sys.stdin.readline
N,W = map(int,input().split())
w,v = [],[]
for _ in range(N):
    w_,v_ = map(int,input().split())
    w.append(w_)
    v.append(v_)
w,v = np.array(w),np.array(v)
ind = np.argsort(v)
w,v = w[ind[::-1]],v[ind[::-1]]

cost = 0
w_total  =0
v_total = 0
for w_,v_ in zip(w,v):
    if w_ + w_total <= W:
        v_total += v_
        w_total += w_
    else: 
        continue
    if w_total == W:
        break
print(v_total)

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

dp = [[0]*(W+1) for _ in range(N+1)]
# dp[i][sum_w]
# i-1番目までの品物から重さがsum_wを超えないように選んだ時の価値の総和
# sum_w = 0, 1, 2, ... ,W

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

        dp[i+1][sum_w] = max(dp[i][sum_w], dp[i][sum_w])
print(dp[N][W])

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

dp = [[0]*(W+1) for _ in range(N+1)]
# dp[i][sum_w]
# i-1番目までの品物から重さがsum_wを超えないように選んだ時の価値の総和
# sum_w = 0, 1, 2, ... ,W

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

        dp[i+1][sum_w] = max(dp[i][sum_w], dp[i][sum_w])
print(dp)

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

dp = [[0]*(W+1) for _ in range(N+1)]
# dp[i][sum_w]
# i-1番目までの品物から重さがsum_wを超えないように選んだ時の価値の総和
# sum_w = 0, 1, 2, ... ,W

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

Submission status: Wrong Answer

Solution code:
def solve(cost,weight,n,m):
    dp=[[0]*(m+1) for i in range(n+1)]
    for i in range(1,n+1):
        for j in range(1,m+1):
            if(weight[i-1]>j):
                dp[i][j]=dp[i-1][j]
            else:
                dp[i][j]=max(dp[i-1][j],cost[i-1]+dp[i-1][j-weight[i-1]])

    for i in range(len(dp)):
        print(dp[i])
    return dp[-1][-1]


n,m=map(int,input().split())
cost=[]
weight=[]
for _ in range(n):
    a,b=map(int,input().split())
    weight.append(a)
    cost.append(b)
dp=[[0]*n for i in range(n)]

print(solve(cost,weight,n,m))

Submission status: Wrong Answer

Solution code:
def solve(cost,weight,n,m):
    dp=[[0]*(m+1) for i in range(n+1)]
    for i in range(1,n+1):
        for j in range(1,m+1):
            if(weight[i-1]>j):
                dp[i][j]=dp[i-1][j]
            else:
                dp[i][j]=cost[i-1]+dp[i][j-weight[i-1]]

    return dp[-1][-1]


n,m=map(int,input().split())
cost=[]
weight=[]
for _ in range(n):
    a,b=map(int,input().split())
    weight.append(a)
    cost.append(b)
dp=[[0]*n for i in range(n)]

print(solve(cost,weight,n,m))

Submission status: Wrong Answer

Solution code:
def solve(cost,weight,n,m):

    #Base case
    if(m<=0 or n<=0):
        return 0

    res=0

    if(weight[n]>m):
        return solve(cost,weight,n-1,m)

    #Recursive case
    op1=solve(cost,weight,n-1,m-weight[n-1])+cost[n-1]
    op2=solve(cost,weight,n-1,m)

    return max(op1,op2)



n,m=map(int,input().split())
cost=[]
weight=[]
for _ in range(n):
    a,b=map(int,input().split())
    weight.append(a)
    cost.append(b)
print(solve(cost,weight,n-1,m))

Submission status: Wrong Answer

Solution code:
def solve(cost,weight,n,m):

    #Base case
    if(m<=0 or n<=0):
        return 0

    res=0

    #Recursive case
    op1=solve(cost,weight,n-1,m-weight[n-1])+cost[n-1]
    op2=solve(cost,weight,n-1,m)

    return max(op1,op2)



n,m=map(int,input().split())
cost=[]
weight=[]
for _ in range(n):
    a,b=map(int,input().split())
    weight.append(a)
    cost.append(b)
print(solve(cost,weight,n,m))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

DP = np.zeros(W + 1)

for i in range(N):
    # print(DP[w[i]:])
    # print(DP[:-w[i]])
    # print(DP[:-w[i]] + v[i])
    DP[w[i]:] = np.maximum(DP[:-w[i]] + v[i], DP[w[i]:])
    # print(DP)
print(DP[-1])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

dp = np.full(N * 1000 + 1, W + 1, dtype=np.int64)
dp[0] = 0

for _ in range(N):
    w, v = map(int, input().split())
    np.minimum(dp[:-v] + w, dp[v:], out=dp[v:])

print(((dp <= W) * np.arange(N * 1000 + 1)).max())

Submission status: Wrong Answer

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

dp = [[0]*(W+1) for j in range(n+1)]

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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



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


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