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):
n,W = map(int,input().split())
wvl = []
for _ in range(n):
   w,v = map(int,input().split())
   wvl.append((w,v))

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

for i in range(W+1):
    if wvl[0][0] <= i:
        dp[0][i] = wvl[0][1]

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

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

Solution (Python):
N, W = map(int, input().split())
items = [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):
        
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        
        if j + items[i][0] > W:
            continue
        dp[i+1][j+items[i][0]] = max(dp[i][j] + items[i][1], dp[i+1][j+items[i][0]])
print(dp[N][W])

Solution (Python):
import sys

def main():
    _input = sys.stdin.readline

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

    wv.sort()

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

    sum_w = 0
    for w, v in wv:
        sum_w += w
        for j in range(min(W, sum_w), w-1, -1):
            if DP[j-w] == -1:
                continue
            if DP[j] < DP[j-w] + v:
                DP[j] = DP[j-w] + v
    print(max(DP))


if __name__ == '__main__':
    main()

Solution (Python):
def solve():
    N, W = map(int, input().split())
    w = []
    v = []
    for i in range(N):
        tmpw, tmpv = map(int, input().split())
        w.append(tmpw)
        v.append(tmpv)

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

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

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

if __name__ == '__main__':
    solve()

Solution (Python):
def main():
    n,lim_w=map(int,input().split())
    wv = []
    for _ in range(n):
        tmp = list(map(int,input().split()))
        wv.append(tmp)
    dp = [[0] * (lim_w+1) for i in range(n+1)]

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

    print(dp[n][lim_w])
if __name__=='__main__':
    main()

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

dp=[0]*(w+1)

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

print(max(dp))

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

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

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

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

ws, vs = zip(*[map(int, input().split()) for _ in range(n)])

dp = [0] * (w + 1)

for i in range(n):
  for j in reversed(range(w + 1)):
    if j + ws[i] <= w:
      dp[j + ws[i]] = max(dp[j + ws[i]], dp[j] + vs[i])
      
print(dp[w])

Solution (Python):

def main():
    n, w = map(int, input().split())
    weight = [0 for _ in range(110)]
    value = [0 for _ in range(110)]

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

    
    dp = [[0 for _ in range(110000)] for i in range(110)]

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

    print(dp[n][w]) 

main()

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

ws = [0] * n
vs = [0] * n

for i in range(n):
  a, b = map(int, input().split())
  ws[i] = a
  vs[i] = b
  
dp = [0] * (w + 1)

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

Solution (Python):

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

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

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

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

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

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

Solution (Python):
def solve(N, W, weightValues):
    dp = [0] * (W + 1)
    s = 0
    for w, v in weightValues:
        s += w 
        for i in range(min(W, s), w - 1, -1): 
            if dp[i - w] + v > dp[i]:
                dp[i] = dp[i - w] + v

    print(max(dp))

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

N, W = iln()
w_v = []
for i in range(N):
  w_v.append(iln())
  
solve(N, W, w_v)

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 for _ in range(W+1)] for _ in range(N+1)]

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

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(input()) 
def ifn(): return float(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def fmn(): return map(float, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, input().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD

N, W = imn()
wv = [iln() for _ in range(N)]

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

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

Solution (Python):
n, ww = 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] * (100100) for i in range(110)]

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

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

print(dp[n][ww])

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,W = map(int,input().split())
w = []
h = []
for i in range(n):
    a,b = map(int,input().split())
    w.append(a)
    h.append(b)
dp = [[0]*(W+1) for i in range(n+1)]
for i in range(n):
    for j in range(W+1):
        if j < w[i]: dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]]+h[i])
print(dp[n][W])

Solution (Python):
import sys 
sys.setrecursionlimit(2147483647)

N,W = map(int,input().split())
A = [list(map(int, input().split())) for i in range(N)]
dp = [[0 for _ in range(W+1) ] for _ in range(N + 1)] 

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

print(max(dp[N]))

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = {False: 'No', True: 'Yes'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


def main():
    N, W = MI()
    dp = [0] * (W+1)
    w = [0] * N
    v = [0] * N

    for i in range(N):
        w[i], v[i] = MI()

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

    print(dp[-1])


if __name__ == '__main__':
    main()

Solution (Python):
def knapsack(N,W,weight,value):
    
    inf=float("inf")
    dp=[[-inf for i in range(W+1)] for j in range(N+1)]
    for i in range(W+1): 
        dp[0][i]=0
    
    for i in range(N):
        for w in range(W+1):
            if weight[i]<=w: 
                dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
            else: 
                dp[i+1][w]=dp[i][w]
    return dp[N][W]

n,w=map(int,input().split())
weight=[0]*n
value=[0]*n
for i in range(n):
    weight[i],value[i]=map(int,input().split())

print(knapsack(n,w,weight,value))

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

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = {False: 'No', True: 'Yes'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


def main():
    N, W = MI()
    dp = [0] * (W+1)
    w = [0] * N
    v = [0] * N

    for i in range(N):
        w[i], v[i] = MI()

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

    print(max(dp))


if __name__ == '__main__':
    main()

Solution (Python):

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

inf = float("inf")
dp = [[-inf for _ in range(W+1)] for _ in range(N+1)]
for i in range(W+1):
    dp[0][i] = 0

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

Solution (Python):
n,w=map(int,input().split())
a=[[int(i) for i in input().split()]for i in range(n)]
dp=[[0 for i in range(w+1)]for i in range(n+1)]
for i in range(n):
    for sum_w in range(1,w+1):
        if sum_w-a[i][0]>=0:
            dp[i+1][sum_w]=max(dp[i][sum_w],dp[i][sum_w-a[i][0]]+a[i][1])
        else:
            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 _ in range(N)]

dp = [[0]*(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 + items[i][0] > W:  
            continue
        
        dp[i+1][j+items[i][0]] = max(dp[i][j]+items[i][1], dp[i+1][j+items[i][0]])
print(dp[N][W])

Solution (Python):
[n,w] = [int(i) for i in input().split()]
wei_val = []
for i in range(n):
    wei_val.append([int(j) for j in input().split()])
"""
dp[i][w_zantei] = v
i : i番目までの品物
w_zantei : 合計の重さがこれになる
v : 価値

更新式
dp[i][w_zantei] = max(dp[i][w_zantei],dp[i - 1][w_zantei - wei_val[0]] + wei_val[1]) (if w >= w_zantei+wei_val[i][0])
dp[i][w_zantei] = dp[i-1][w_zantei] (if w < w_zantei+wei_val[0])
"""
dp = [[0]* (w+1) for i in range(n)]
for i in range(n):
    for w_zantei in range(w+1):
        if w_zantei >= wei_val[i][0]:
            dp[i][w_zantei] = max(dp[i-1][w_zantei],dp[i - 1][w_zantei - wei_val[i][0]] + wei_val[i][1])
        else:
            dp[i][w_zantei] = dp[i-1][w_zantei]
print(dp[n-1][w])

Solution (Python):
def solve(N, W, weightValues):
    dp = [0] * (W + 1)
    s = 0
    for w, v in weightValues:
        s += w
        for j in range(min(W, s), w - 1, -1):
            if dp[j - w] + v > dp[j]:
                dp[j] = dp[j - w] + v

    print(max(dp))


N, W = map(int, input().split())
weightValues = sorted([list(map(int, input().split())) for i in range(N)])
solve(N, W, weightValues)

Solution (Python):

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

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 i in range(1, N + 1):
    row = DP[i - 1][:]
    for j in range(W, -1, -1):
        new_j = j + w[i - 1]
        if 0 <= new_j <= W:
            row[new_j] = max(row[new_j], row[j] + v[i - 1])
    DP.append(row)
print(DP[-1][-1])

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



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

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

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

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

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

print(dp[-1])

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii())
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

n,w = mii()
wei,val= [0],[0]
for _ in range(n):
    p,q=mii()
    wei.append(p)
    val.append(q)

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

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


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

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

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

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

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

Solution (Python):
import numpy as np
N, W = map(int, input().split())
wv = np.array([list(map(int, input().split())) for _ in range(N)])

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

for w, v in wv:
    dp[w:] = np.maximum(dp[w:], dp[:-w]+v)

ans = dp[W]
print(ans)

Solution (Python):
import numpy as np
N, W = map(int, input().split())
wv = np.array([list(map(int, input().split())) for _ in range(N)])

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

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

ans = dp[W]
print(ans)

Solution (Python):
def chmax(a, b):
    if a < b:
        a = b
        return a
    else:
        return a


n, w = map(int, input().split())
ls = [[int(_) for _ in input().split()] for _ in range(n)]

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

for i in range(1, n + 1):
    for sumw in range(w + 1):
        if sumw + ls[i - 1][0] <= w:
            dp[i][sumw + ls[i - 1][0]] = chmax(dp[i][sumw + ls[i - 1][0]], dp[i - 1][sumw] + ls[i - 1][1])
        
        dp[i][sumw] = chmax(dp[i][sumw], dp[i - 1][sumw])

print(dp[n][w])

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(input()) 
def ifn(): return float(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def fmn(): return map(float, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, input().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def add_mod(a,b): return (a+b) % MOD

N, W = imn()
wv = [iln() 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())

w, v = [0] * N, [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] >= 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())
wvlist = [tuple(map(int, input().split())) for _ in range(N)]
dp = [[0] * (W + 1) for _ in range(N + 1)]
for i in range(N):
    for sum_w in range(W + 1):
        if sum_w >= wvlist[i][0]:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wvlist[i][0]] + wvlist[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())
l=[list(map(int,input().split())) for i in range(n)]
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-l[i-1][0]>=0:
      dp[i][j]=max(dp[i-1][j],dp[i-1][j-l[i-1][0]]+l[i-1][1])
    else:
      dp[i][j]=dp[i-1][j]
print(max(dp[n]))

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(input()) 
def ifn(): return float(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def fmn(): return map(float, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, input().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
MOD = 10**9+7

N,W = imn()
wv = [iln() 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 wv[i][0] <= sum_w:
            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):

"""
Created on Tue Jan 21 18:19:39 2020

@author: matsui
"""


N,W=map(int,input().split())
dp=[0]*(W+1)
L = sorted([list(map(int,input().split())) for i in range(N)])
s=0

for w, v in L:
    s+=w
    for j in range(min(W,s),w-1,-1):
        if dp[j-w]+v > dp[j]: dp[j]=dp[j-w]+v
    
print(max(dp))

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

Solution (Python):
N, W = map(int, input().split(" "))
weights = []
values = []

for _ in range(N):
    w, v = map(int, input().split(" "))
    weights.append(w)
    values.append(v)

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

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

print(dp[N][W])

Solution (Python):
def solve():
    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):
        for j in range(W - w[i], -1, -1):
            dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i])
    print(dp[W])


if __name__ == '__main__':
    solve()

Solution (Python):


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

for _ in range(N):
    w, v = list(map(int, input().split()))
    item_list.append((w, v))

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

for i in range(1, N + 1):
    w, v = item_list[i - 1]
    for j in range(W + 1):
        
        before_best_value = dp_table[i - 1][j]
        dp_table[i][j] = before_best_value
        
        if j - w < 0:
            continue
        value = dp_table[i - 1][j - w] + v
        best_value = max(before_best_value, value)
        dp_table[i][j] = best_value

ans = max(dp_table[-1])
print(ans)

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 

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

Solution (Python):
def solve(N, W, weight_values):
  dp = [0 for _ in range(W + 1)]

  for i in range(N):
    i_weight, i_value = weight_values[i]
    for w in range(W, i_weight - 1, -1):
      
      temp = dp[w - i_weight] + i_value
      if temp > dp[w]:
        dp[w] = temp

  print(dp[W])

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


N, W = iln()
w_v = []
for i in range(N):
  w_v.append(iln())
  
solve(N, W, w_v)

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

Solution (Python):
def solver():
	N,W = map(int, input().split())
	item = []
	for i in range(N):
		item.append(list(map(int, input().split())))
	dp = [0 for _ in range(W+1)]
 
	for i in range(0,N):
		for j in range(W, item[i][0]-1, -1):
			dp[j] = max(dp[j], dp[j-item[i][0]]+item[i][1])
	print(dp[W])
 
solver()

Solution (Python):




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





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


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



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

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

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

for n in range(1,N+1):
    for w in range(W+1):
        if w>=w_li[n-1][0]:
            li[w][n] = max(li[w][n-1] , li[w-w_li[n-1][0]][n-1]+w_li[n-1][1])
        else:
            li[w][n] = li[w][n-1]
print(li[W][N])

Solution (Python):

from collections import defaultdict


def main():
    N, W, *WV = map(int, open(0).read().split())
    dp = defaultdict(int)
    dp[0] = 0
    for w, v in zip(*[iter(WV)] * 2):
        for cur_w, cur_v in tuple(dp.items()):
            if w + cur_w <= W:
                dp[w + cur_w] = max(dp[w + cur_w], v + cur_v)
    print(max(dp.values()))


if __name__ == "__main__":
    main()