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 .

Solution (Python):
import sys
input=sys.stdin.readline
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)]
for i in range(N):
    for j in range(W+1):
        if j>=w[i]:
            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][W])

Solution (Python):
n,w=map(int,input().split())
dp=[0]*(w+1)
for i in range(n):
    wi,vi=map(int,input().split())
    for j in range(w-wi,-1,-1):
        dp[j+wi]=max(dp[j+wi],dp[j]+vi)
print(dp[-1])

Solution (Python):
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)]
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+1][sum_w], dp[i][sum_w - w[i]] + v[i])
        dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w])
print(dp[N][W])

Solution (Python):
n,w = map(int,input().split())

items = [list(map(int,input().split())) for i in range(n)]

dp = [[0]*(w+1) for i in range(n+1)]

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

print(dp[n][w])

Solution (Python):
n,w = map(int,input().split())
v = [list(map(int,input().split())) for _ in range(n)]
dp = [[0]*(w+1) for _ in range(n+1)]
for i in range(1,n+1):
  for j in range(w+1):
    if j < v[i-1][0]:
      dp[i][j] = dp[i-1][j]
    else:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-v[i-1][0]]+v[i-1][1])
print(dp[n][w])

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
N,W = 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]*(W+1) for _ in range(N+1)]

def memo(i,j):
  if dp[i][j]:
    return dp[i][j]
  if i == N:
    dp[i][j] = 0
  elif w[i] > j:
    dp[i][j] = memo(i+1,j)
  else:
    dp[i][j] = max(memo(i+1,j),memo(i+1,j-w[i])+v[i])
  return dp[i][j] 
print(memo(0,W))

Solution (Python):
n,w = map(int,input().split())
v = [list(map(int,input().split())) for _ in range(n)]
dp = [[0]*(w+1) for _ in range(n+1)]
for i in range(1,n+1):
  for j in range(w+1):
    if j < v[i-1][0]:
      dp[i][j] = dp[i-1][j]
    else:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-v[i-1][0]]+v[i-1][1])
print(dp[n][w])

Solution (Python):
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())
  dp[w:] = np.maximum(dp[w:],dp[:-w]+v)

print(dp[-1])

Solution (Python):
n,w = map(int,input().split())
v = []
wt = []
for _ in range(n):
    x,y=map(int,input().split())
    wt.append(x)
    v.append(y)
a = [[0 for x in range(w+1)] for x in range(n+1)] 
for i in range(n+1):
    for j in range(w+1):
        if i == 0 or j==0:
            a[i][j]=0
        elif wt[i-1]<=j:
            a[i][j]=max(a[i-1][j],(v[i-1]+a[i-1][j-wt[i-1]]))
        else:
            a[i][j]=a[i-1][j]
print(a[n][w])

Solution (Python):
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):
    wi, vi = wv[i]
    for j in range(w+1):
        if j<wi:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j], dp[i][j-wi]+vi)
            
print(dp[n][w])

Solution (Python):
N,W=map(int,input().split())
L = [list(map(int, input().split())) for i in range(N)]
dp = [[0 for j in range(110000)] for i in range(110)]
for i in range(1,N+1):
  for j in range(W+1):
    dp[i][j]=dp[i-1][j]
    if j-L[i-1][0]>=0:
      dp[i][j]=max(dp[i][j],dp[i-1][j-L[i-1][0]]+L[i-1][1])
ans=dp[N][W]
print(ans)

Solution (Python):
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 = [[0]*(W+1) for i in range(N+1)]


for i in range(1,N+1):
    
    for j in range(W+1):
        if j >= w[i-1]:
            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(dp[-1][-1])

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,W = inpl()
wv = [inpl() for _ in range(n)]
dp = [0] * (W+1)
for i in range(n):
    w,v = wv[i]
    for j in range(w,W+1)[::-1]:
        dp[j] = max(dp[j], dp[j-w] + v)
print(dp[W])

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


for i in range(N+10):
  WV.append([0,0])


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


for i in range(N):
  for sum_w in range(W+1):

    if sum_w - WV[i][0] >= 0:
      dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w - WV[i][0]] + WV[i][1])

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


print(dp[N][W])

Solution (Python):
import numpy as np

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

Solution (Python):



'''
3 8
3 30
4 50
5 60
'''

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

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

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


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


print(dp[N][W])

Solution (Python):
N, W = map(int, input().split())
items = [list(map(int, input().split())) for _ in range(N)]
weights = list(map(lambda x: x[0], items))
values = list(map(lambda x: x[1], items))

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

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

Solution (Python):
def solve(items, n, w):
    dp = [0] * (w+1)
    for i in range(n):
        wi, vi = items[i]
        for j in range(w, wi-1, -1):
            dp[j] = max(dp[j], dp[j-wi] + vi)

    return dp[w]


def main():
    inp = lambda: [int(x) for x in input().split()]
    n, w = inp()
    items = [inp() for _ in range(n)]
    print(solve(items, n, w))


if __name__ == '__main__':
    main()

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

Solution (Python):
def solve(items, n, w):
    f = [[0] * (w + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        wi, vi = items[i-1]
        for j in range(w + 1):
            f[i][j] = f[i-1][j]
            if j >= wi:
                f[i][j] = max(f[i][j], f[i-1][j-wi] + vi)
    return f[n][w]


def main():
    inp = lambda: [int(x) for x in input().split()]
    n, w = inp()
    items = [inp() for _ in range(n)]
    print(solve(items, n, w))


if __name__ == '__main__':
    main()

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

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

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


print(dp[-1])

Solution (Python):
N,W = map(int,input().split())
table = [ list(map(int,input().split(" "))) for i in range(N)]
N += 1
W += 1
dp = [[0 for i in range(W)] for j in range(N)]

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


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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N, W = map(int, input().split())
    items = [tuple(map(int, input().split())) for _ in range(N)]

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

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

if __name__ == '__main__':
    main()

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

w = []
v = []

for i in range(N):
    a,b = list(map(int,input().split()))
    w.append(a)
    v.append(b)

DP = [[-float('inf')]*(W+1) for i in range(N+1)]
DP[0][0] = 0
for i in range(N):
    for j in range(W+1):
        DP[i+1][j] = DP[i][j]
        if j>=w[i]:
            DP[i+1][j] = max(DP[i+1][j],DP[i][j-w[i]]+v[i])

print(max(DP[N]))

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

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

print(dp[W])

Solution (Python):
import numpy as np
import sys


def main():
    input = sys.stdin.readline
    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())
    ndp = np.zeros(W + 1, dtype=np.int64)
    for w, v in zip(w, v):
        np.maximum(ndp[:-w] + v, ndp[w:], out=ndp[w:])
    print(ndp[-1])


if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
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())
  dp[w:] = np.maximum(dp[:-w] + v, dp[w:])
print(dp[W])

Solution (Python):
import sys
input = sys.stdin.readline

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

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

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

print(max(dp[N]))

Solution (Python):
def main():
    N, W = map(int, input().split())
    wv = [list(map(int, input().split())) for _ in range(N)]
    dp = [[0] * (W+10) for _ in range(N+1)]
    for i in range(N):
        for w in range(W+1):

            
            if w - wv[i][0] >= 0:
                dp[i+1][w] = max(dp[i+1][w], dp[i][w-wv[i][0]] + wv[i][1])

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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
  num,capacity,*wv =map(int,open(0).read().split())
  weights,values = wv[::2], wv[1::2]
  DP=[-1]*(capacity+1)
  DP[0]=0
  for weight,value in zip(weights, values):
    left=capacity-weight
    DP[weight:]=[max(DP[i]+value,DP[i+weight]) for i in range(left,-1,-1)][::-1]
  print(max(DP))
      
if __name__=="__main__":
  main()

Solution (Python):
n,w=list(map(int,input().split()))
a=[]
for i in range(n):
    a.append(list(map(int,input().split())))
a.sort()
b=[]
for i in range(n):
    b.append([0]*(w+1))
for i in range(1,w+1):
    if i>=a[0][0]:
        b[0][i]=a[0][1]

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

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

Solution (Python):
def zeros(h,*w): return [[0] * w[0]  for _ in range(h)] if w else [[0] * h  for _ in range(h)]

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

dp = zeros(n+1,W+1)

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

ans = max( dp[n] )

print(ans)

Solution (Python):


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

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

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

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
import numpy as np
n,W = mi()
a = [tuple(int(i) for i in input().split()) for _ in range(n)]
dp = np.array([0]*(W+1))
for w,v in a:
    ed = W+1-w
    dp[w:W+1] = np.maximum(dp[w:W+1], dp[0:W+1-w] + v)
print(dp[W])

Solution (Python):







N,W=map(int,input().split())
w,v=[0],[0]
def main():
  for _ in range(N):
      wi,vi=map(int,input().split())
      w.append(wi)
      v.append(vi)

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


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

  print(dp[N][W])
main()

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


dp = [[0] + [-1] * (L+3) for _ in range(N+3)]
dp[0] = [0] * (L+4)

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

print(dp[N][L])

Solution (Python):

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

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

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

Solution (Python):

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


import numpy as np
dp = np.zeros((N+1, W+1), int)

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

print(dp[-1, -1])

Solution (Python):
import sys

import numpy as np

input = sys.stdin.readline
N, weights = map(int, input().split())


dp = np.zeros(weights + 1, dtype=np.int64)

while N:
    w, v = map(int, input().split())
    np.maximum(dp[:-w] + v, dp[w:], out=dp[w:])
    
    
    
    
    N -= 1
print(dp[-1])

Solution (Python):
import numpy as np

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


dp = np.zeros(weights + 1, dtype=np.int64)

while N:
    w, v = map(int, input().split())
    np.maximum(dp[:-w] + v, dp[w:], out=dp[w:])
    
    
    
    
    N -= 1
print(dp[-1])

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, islice
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')

N, W = MAP()
wv = [LIST() for _ in range(N)]

dp = np.zeros(W+1, dtype=np.int64)
for w, v in wv:
	np.maximum(dp[w:], dp[:-w]+v, out=dp[w:])

print(dp[W])

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, islice
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')

N, W = MAP()
w, v = ZIP(N)

dp = [0]*(W+1)

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, islice
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')

N, W = MAP()
w, v = ZIP(N)

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][j], dp[i][j-w[i]]+v[i])
		else:
			dp[i+1][j] = dp[i][j]
print(dp[N][W])

Solution (Python):
n,w = map(int,input().split())
a = []
p = []
for i in range(n):
    k,v = map(int,input().split())
    a.append(v)
    p.append(k)

dp = []
for i in range(n+1):
    o = []
    for j in range(w+1):
        o.append(0)

    dp.append(o)


for i in range(0,n):
    for j in range(0,w+1):
        if p[i] > j:
            dp[i+1][j] = dp[i][j]

        else:
            dp[i+1][j] = max(a[i] + dp[i][j - p[i]], dp[i][j])

print(dp[n][w])

Solution (Python):
n,w = map(int,input().split())
a = []
p = []
for i in range(n):
    k,v = map(int,input().split())
    p.append(k)
    a.append(v)

dp = []
for i in range(n+1):
    o = []
    for j in range(w+1):
        o.append(0)

    dp.append(o)


for i in range(n):
    for j in range(w+1):
        if p[i] > j:
            dp[i+1][j] = dp[i][j]

        else:
            dp[i+1][j] = max(a[i] + dp[i][j - p[i]], dp[i][j])

print(dp[n][w])

Solution (Python):
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):
    w_, v_ = wv[i]
    for j in range(w+1):
        if j < w_:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j-w_]+v_, dp[i][j])
print(dp[n][w])

Solution (Python):











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

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

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

answer = -1
for d in dp:
    answer = max(answer, max(d))
print(answer)

Solution (Python):










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

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

Solution (Python):
N, W = map(int, input().split())
weight = []
value = []
for i in range(N):
    w, v = map(int, input().split())
    weight.append(w)
    value.append(v)
dp = [[0 for _ in range(100005)] for _ in range(105)]
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 - weight[i] >= 0:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j-weight[i]]+value[i])
print(dp[N][W])

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

Solution (Python):
N,W = map(int,input().split())
wv =[[0,0]]
dp = []
Counting = []
for _ in range(N):
    wv.append(list(map(int,input().split())))
for _ in range(N+1):
    dp.append([0]*(W+1))
    Counting.append([0]*(W+1))
for i in range(1,N+1):
    for sum_w in range(W+1):
        if Counting[i-1][sum_w] > sum_w:
            continue
        elif sum_w-wv[i][0]<0:
            dp[i][sum_w] =  dp[i-1][sum_w]
            Counting[i][sum_w]  = Counting[i-1][sum_w]
        else:
            if dp[i-1][sum_w] < dp[i-1][sum_w-wv[i][0]]+wv[i][1]:
                Counting[i][sum_w]  = Counting[i-1][sum_w-wv[i][0]]  + wv[i][0]
            else:
                Counting[i][sum_w]  = Counting[i-1][sum_w]
            dp[i][sum_w] = max(dp[i-1][sum_w],dp[i-1][sum_w-wv[i][0]]+wv[i][1])
print(dp[-1][-1])

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

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

Solution (Python):
def knapSack(W, wt, val, n): 
    K = [[0 for x in range(W+1)] for x in range(n+1)] 
  
    
    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())

wt = []
val = []

for i in range(n):
    wtt,vall=map(int,input().split())
    wt.append(wtt)
    val.append(vall)
ans = knapSack(w,wt,val,n)
print(ans)

Solution (Python):
import numpy as np

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

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



dp = np.array([0] * (_W + 1))
for i in range(1, N + 1):
    w, v = W[i], V[i]
    np.maximum(dp[:-w] + v, dp[w:], out=dp[w:])
    


print(dp.max())

Solution (Python):
import numpy as np

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

Solution (Python):

def solve_knapsack(weight_limit: int, weights: list, values: list) -> int:
    dp = [0] * (weight_limit + 1)  
    for w, v in zip(weights, values):
        for cur_w in range(weight_limit, w - 1, -1):
            dp[cur_w] = max(dp[cur_w], dp[cur_w - w] + v)
    return dp[-1]


def main():
    N, W, *WV = map(int, open(0).read().split())
    print(solve_knapsack(W, WV[::2], WV[1::2]))


if __name__ == "__main__":
    main()

Solution (Python):
N,w = [int(x) for x in input().split()]
ll = []
for i in range(N):
    abc = [int(x) for x in input().split()]
    ll.append(abc)
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 ll[i-1][0] <= j:
            DP[i][j] = max(DP[i-1][j-ll[i-1][0]]+ll[i-1][1],DP[i-1][j])
        else:
            DP[i][j] = DP[i-1][j]
print(DP[N][w])

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

weight = [None] * N
value = [None] * N
dp = [[None] * (W+1) for _ in range(N+1)]

for i in range(N):
  weight[i], value[i] = map(int, input().split())

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

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

ans = dp[N][W]
print(ans)

Solution (Python):
import sys
import numpy as np
N, W = map(int, input().split())
def knapsack(n, w):
    
    dp = np.zeros([N+1, W+1], dtype='int64')
    
    for i in range(N):
        w, v = map(int, input().split())
        
        dp[i+1][:w] = dp[i][:w]
        dp[i+1][w:] = np.maximum(dp[i][w:], dp[i][:-w] + v)
    return dp[-1][-1]

print(knapsack(N, W))

Solution (Python):
N,W=map(int,input().split())
MV=[list(map(int, input().split())) for _ in range(N)]
dp=[0]*(W+1)

for w,v in MV:
    for tw in range(W,w-1,-1):
        dp[tw]=max(dp[tw],dp[tw-w]+v)
        

print(max(dp))

Solution (Python):
N, W = map(int, input().split())
dp = [0] * (W+1)

for _ in range(N):
    w, v = map(int, input().split())
    for dp_w in reversed(range(W+1)):
        dp_v = dp[dp_w]
        if (dp_w == 0 or dp_v > 0) and dp_w + w <= W:
            dp[dp_w + w] = max(dp[dp_w + w], dp[dp_w] + v)

print(max(dp))

Solution (Python):
n, w = map(int, input().split())
dp = [[-10 ** 9 for _ in range(w+1)] for _ in range(n)]
wn, vn = map(int, input().split())
dp[0][0] = 0
if wn <= w:
    dp[0][wn] = vn
for i in range(1, n):
    wn, vn = map(int, input().split())
    for j in range(w+1):
        if (j-wn) >= 0:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-wn]+vn)
        else:
            dp[i][j] = dp[i-1][j]


ans = 0
for val in dp[n-1]:
    if val > ans:
        ans = val
print(ans)

Solution (Python):
n, w = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]
dp = [[0 for _ in range(w + 1)] for _ in range(n + 1)]

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

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

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

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


print(dp[N][W])

Solution (Python):
n,w=map(int,input().split())
weight=[]
value=[]
for _ in range(n):
    ww,vv=map(int,input().split())
    weight.append(ww)
    value.append(vv)
dp=[[0 for i in range(w+1)] for j in range(n+1)]
for i in range(0,n):
    for w_sum in range(w+1):
        if w_sum >= weight[i]:
            dp[i+1][w_sum]=max(dp[i+1][w_sum],dp[i][w_sum-weight[i]]+value[i])
        dp[i+1][w_sum]=max(dp[i+1][w_sum],dp[i][w_sum])
print(dp[n][w])

Solution (Python):
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):
    w_, v_ = wv[i]
    for j in range(w+1):
        if j < w_:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j-w_]+v_, dp[i][j])
print(dp[n][w])

Solution (Python):

import numpy as np

n, w = map(int, input().split())
dp = np.full(w + 1, 0, dtype=int)



for _ in range(n):
    wi, vi = map(int, input().split())
    dp[wi:] = np.maximum(dp[:-wi] + vi, dp[wi:])
print(dp[-1])

Solution (Python):

from sys import stdin,stdout

input = stdin.readline
print = stdout.write

vi = [0]
wi = [0]

n,W=map(int,input().split())
for i in range(n):
    a,b = map(int,input().split())
    vi.append(b)
    wi.append(a)

    
dp = [[0]*(W+1) for i in range(n+1)]
    
for j in range(1,n+1):
    for k in range(1,W+1):
        if(wi[j]>k) :
            dp[j][k] = dp[j-1][k]
        else:
            dp[j][k] = max(dp[j-1][k],dp[j-1][k-wi[j]]+vi[j])

print(str(dp[n][W]))

Solution (Python):
def submit():
    n, w = map(int, input().split())
    dp = [[0] * (w + 1) for _ in range(n + 1)]

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

submit()

Solution (Python):
from numpy import*
f=lambda:map(int,input().split())
N,W=f()
T=zeros(W+1,int)
exec('w,v=f();T[w:]=maximum(T[w:],T[:-w]+v);'*N)
print(T[-1])

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

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

Solution (Python):


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

jwels=[]

for _ in range(N):
    jwels.append(list(map(int,input().split())))

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

for w,v in jwels:
    for i in range(W+1)[::-1]:
        if dp[i]!=-1 and i+w<=W:
            dp[i+w]=max(dp[i]+v,dp[i+w])
            
print(max(dp))

Solution (Python):
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]+v,dp[w:])
print(int(dp[-1]))

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