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^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

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

1 1000000000
1000000000 10

Sample Output 2

10

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: Accepted

Solution code:
n, m = map(int, input().split())
goods = [tuple(map(int, input().split())) for _ in range(n)]
s = sum(list(map(lambda x: x[1], goods)))
dp = [[10 ** 15 for _ in range(s+1)] for _ in range(n+1)]
dp[0][0] = 0
for i in range(n):
    for j in range(s+1):
        w, v = goods[i]
        if j >= v:
            dp[i+1][j] = min(dp[i][j], dp[i][j - v] + w)
        else: dp[i+1][j] = dp[i][j]
maxv = 0
for j in range(s+1):
    if dp[n][j] <= m:
        maxv = j
print(maxv)

Submission status: Accepted

Solution code:
import numpy as np
n, W = map(int, input().split())
dp = np.full(n*10**3+1, W+1,'i8')
dp[0] = 0
for i in range(n):
  w, v = map(int, input().split())
  dp[v:] = np.minimum(dp[v:], dp[:-v]+w)
print(np.max(np.where(dp<=W)))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, W = map(int,input().split(" "))
    w,v = [0]*(N+1),[0]*(N+1)
    for i in range(N):
        w[i+1],v[i+1] = map(int,input().split(" "))

    V = sum(v)
    dp = [ [float('inf')]*(V+1) for _ in range(N+1) ]
    for i in range(N+1):
        dp[i][0] = 0

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

    for j in range(V,-1,-1):
        if dp[N][j] <= W:
            print(j)
            break

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/3/2
Update on 2019/3/5
Solved on 2019/3/
@author: shinjisu
"""


# Educational Dynamic Programming
# import math
import numpy as np


def getInt(): return int(input())


# def getIntList(): return [int(x) for x in input().split()]
def getIntList(): return np.array(input().split(), dtype=np.longlong)


# def zeros(n): return [0]*n
def zeros(n): return np.zeros(n, dtype=np.longlong)


# def getIntLines(n): return [int(input()) for i in range(n)]
def getIntLines(n):
    data = zeros(n)
    for i in range(n):
        data[i] = getInt()
    return data


def getIntMat(n):  # obsoleted
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat
def getIntMat(n, m): # n行に渡って、1行にm個の整数
    mat = zeros((n, m))
    for i in range(n):
        mat[i] = getIntList()
    return mat


def zeros2(n, m): return [zeros(m)]*n # obsoleted zeros((n, m))で代替


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**18


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x


def prob_A():  # Frog 1
    N = getInt()
    H = getIntList()
    dmp((N, H), 'N, H')
    c = zeros(N)
    c[1] = abs(H[1]-H[0])
    for i in range(2, N):
        c[i] = min(c[i-1] + abs(H[i]-H[i-1]),
                   c[i-2] + abs(H[i]-H[i-2]))
    dmp(c, 'c')
    return c[N-1]


def prob_B_TLE():  # Frog 2 3/16 TLE
    N, K = getIntList()
    H = getIntList()
    dmp((N, K, H), 'N, K, H')
    c = np.zeros(N)
    c[0] = 0
    for i in range(1, N):
        #c[i] = INF
        for j in range(max(0, i-K), i):
            c[i] = min(c[i], c[j] + abs(H[i]-H[j]))
    dmp(c, 'c')
    return c[N-1]


def prob_B():  # Frog 2
    N, K = getIntList()
    H = np.array(input().split(), dtype='int')
    dmp((N, K, H), 'N, K, H')
    c = np.zeros(N, dtype=np.longlong)
    c[0] = 0
    for i in range(1, N):
        c[i] = np.min(c[max(0, i-K):i] + np.abs(H[max(0, i-K):i] - H[i]))
    dmp(c, 'c')
    return c[N-1]


def prob_C():  # Vacation
    N = getInt()
    a = zeros(N)
    b = zeros(N)
    c = zeros(N)
    for i in range(N):
        a[i], b[i], c[i] = getIntList()
    dmp(N, 'N')
    dmp(a, 'a')
    dmp(b, 'b')
    dmp(c, 'c')
    dpa = zeros(N)
    dpb = zeros(N)
    dpc = zeros(N)
    dpa[0] = a[0]
    dpb[0] = b[0]
    dpc[0] = c[0]
    for i in range(1, N):
        dpa[i] += max(dpb[i-1], dpc[i-1]) + a[i]
        dpb[i] += max(dpc[i-1], dpa[i-1]) + b[i]
        dpc[i] += max(dpa[i-1], dpb[i-1]) + c[i]
    dmp(dpa, 'dpa')
    dmp(dpb, 'dpb')
    dmp(dpc, 'dpc')
    return max(dpa[N-1], dpb[N-1], dpc[N-1])


def prob_D_incomplete():  # Knapsack 1
    N, W = getIntList()
    w = zeros(N)
    v = zeros(N)
    for i in range(N):
        w[i], v[i] = getIntList()
    dmp((N, W), 'N, W')
    dmp(w, 'w')
    dmp(v, 'v')
    dpw = zeros(N)
    dpv = [0]*N
    knap = zeros(N)  # ナップに入っているか否か
    dpw[0] = w[0]
    dpv[0] = v[0]
    knap[0] = 1
    for i in range(1, N):
        if dpw[i-1]+w[i] <= W:
            dpw[i] = dpw[i-1] + w[i]
            dpv[i] = dpv[i-1] + v[i]
            knap[i] = 1
            continue
        swap = -1
        maxV = -INF
        minW = INF
        for j in range(0, i):
            if v[i] > v[j] and knap[j] == 1 and dpw[i-1]-w[j]+w[i] <= W:
                if v[j] >= maxV:
                    maxV = v[j]
                    swap = j
            elif v[i] == v[j] and knap[j] == 1 \
                    and dpw[i-1]-w[j]+w[i] <= W and w[i] < w[j]:
                if w[j] <= minW:
                    minW = w[j]
                    swap = j
        if swap > 0:
            dpw[i] = dpw[i-1] - w[j] + w[i]
            dpv[i] = dpv[i-1] - v[j] + v[i]
            knap[i] = 1
            knap[j] = 0
        else:
            dpw[i] = dpw[i-1]
            dpv[i] = dpv[i-1]

    dmp(dpw, 'dpw')
    dmp(dpv, 'dpv')
    dmp(knap, 'knap')
    return dpv[N-1]


def prob_D():  # Knapsack 1
    N, W = getIntList()
    w, v = zeros(N), zeros(N)
    dmp((N, W))
    for i in range(N):
        w[i], v[i] = getIntList()
    dmp(w, 'weight')
    dmp(v, 'value')
    dp = zeros(W+1)
    dmp(dp.size)
    for i in range(N):
        dmp(dp, 'dp')
        dmp(w[i], 'w[i]')
        dp[w[i]:] = np.maximum(dp[w[i]:],
                               dp[:W-w[i]+1] + v[i])
    dmp(dp, 'dp')
    return dp[W]


def prob_E():  # Knapsack 2
    N, W = getIntList()
    w, v = zeros(N), zeros(N)
    dmp((N, W))
    for i in range(N):
        w[i], v[i] = getIntList()
    dmp(w, 'weight')
    dmp(v, 'value')
    dp = zeros(v.sum()+1)
    dp += w.sum()+1
    dp[0] = 0
    dmp(dp.size, 'dp.size')
    for i in range(N):
        dmp((w[i], v[i], dp), 'w[i], v[i], dp')
        #dp[v[i]:] = np.maximum(dp[v[i]:],
        #                       dp[:v.sum()-v[i]+1] + w[i])
        """
        for j in range(v[i], v.sum()+1):
            # dmp((j, dp[j]), 'j, dp[j]')
            if dp[j] == 0:
                dp[j] = dp[j-v[i]] + w[i]
                dmp((j,dp), '0-dp')
            elif dp[j] > dp[j-v[i]] + w[i]:
                dp[j] = dp[j-v[i]] + w[i]
                dmp((j,dp), 'dp')
        """
        dp[v[i]:] = np.minimum(dp[v[i]:],
                               dp[:v.sum()-v[i]+1] + w[i])
    dmp(dp, 'final dp')
    maxVal = 0
    minWeight = INF
    for i in range(len(dp)):
        if dp[i] <= W and i >= maxVal:
            maxVal = i
    dmp(maxVal)
    return maxVal


debug = False  # True False
ans = prob_E()
print(ans)
#for row in ans:
#    print(row)

Submission status: Accepted

Solution code:
import numpy as np
n, wt = map(int, input().split())
 
d = np.full(n*1000+1,wt+1, 'i8')
d[0] = 0
for _ in range(n):
  w, v = map(int, input().split())
  d[v:] = np.minimum(d[v:], d[:-v]+w)
print(np.max(np.where(d<=wt)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python2
"""
This file is part of https://github.com/cheran-senthil/PyRival
Copyright 2019 Cheran Senthilkumar <hello@cheran.io>

"""
from __future__ import division, print_function

import itertools
import os
import sys
from atexit import register
from io import BytesIO


class dict(dict):
    """dict() -> new empty dictionary"""

    def items(self):
        """D.items() -> a set-like object providing a view on D's items"""
        return dict.iteritems(self)

    def keys(self):
        """D.keys() -> a set-like object providing a view on D's keys"""
        return dict.iterkeys(self)

    def values(self):
        """D.values() -> an object providing a view on D's values"""
        return dict.itervalues(self)


def gcd(x, y):
    """greatest common divisor of x and y"""
    while y:
        x, y = y, x % y
    return x


range = xrange

filter = itertools.ifilter
map = itertools.imap
zip = itertools.izip

sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))

input = lambda: sys.stdin.readline().rstrip('\r\n')

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

w, v = [0.0] * n, [0] * n
for i in range(n):
    wi, vi = input().split()
    w[i], v[i] = float(wi), int(vi)

max_val = sum(v)
dp = [[float('inf')] * (max_val + 1) for _ in range(n)]
for i in range(n):
    dp[i][0] = 0.0

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

print(int(max(i for i, val in enumerate(dp[-1]) if val <= m)))

Submission status: Accepted

Solution code:

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

# dp[i][v] : i-1番目までで価値がvのときの重さを表す

maxval = 100001
dp = [[1000000000000 for i in range(maxval)] for j in range(N+1)]
dp[0][0] = 0

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

mv = 0
for i in range(N+1):
    for j in range(maxval):
        j0 = maxval - 1 - j
        if dp[i][j0] <= W:
            if mv < j0:
                mv = j0

print(mv)

Submission status: Accepted

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

supremum = 10**5

dp = [float("inf") for i in range(supremum+1)]
dp[0] = 0

for i in range(N) :
    weight_i,value_i = L[i]    
    for value in range(supremum,value_i-1,-1) :
        if dp[value-value_i] + weight_i <= W :
            dp[value] = min(dp[value], dp[value-value_i] + weight_i)

for i in range(supremum,-1,-1) :
    if dp[i] < float("inf") :
        ans = i
        break

print(ans)

Submission status: Accepted

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

supremum = 10**5

dp = [float("inf") for i in range(supremum+1)]
dp[0] = 0

for i in range(N) :
    weight_i,value_i = L[i]    
    for value in range(supremum,value_i-1,-1) :
        if dp[value-value_i] + weight_i <= W :
            dp[value] = min(dp[value], dp[value-value_i] + weight_i)

for i in range(supremum,-1,-1) :
    if dp[i] <= W :
        ans = i
        break

print(ans)

Submission status: Accepted

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

supremum = 10**5

dp = [float("inf") for i in range(supremum+1)]
dp[0] = 0

for i in range(N) :
    weight_i,value_i = L[i]    
    for j in range(supremum,value_i-1,-1) :
        if dp[j-value_i] < float("inf") :
            dp[j] = min(dp[j], dp[j-value_i] + weight_i)

for i in range(supremum,-1,-1) :
    if dp[i] <= W :
        ans = i
        break

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N,W=map(int,input().split())
w=[0 for i in range(N)]
v=[0 for i in range(N)]
for i in range(N):
    w[i],v[i]=map(int,input().split())
total_v=sum(v)
INF=sum(w)+1
dp=[INF for i in range(total_v+1)]
dp[0]=0
dp=np.array(dp)
for i in range(N):
    dp[v[i]:]=np.minimum(dp[v[i]:],dp[:-v[i]]+w[i])
for i in reversed(range(1,total_v+1)):
    if dp[i]<=W:
        print(i)
        break

Submission status: Accepted

Solution code:
import numpy as np
n,w = [int(x) for x in input().split()]
wv = np.array([[int(y) for y in input().split()] for z in range(n)],dtype=int)
sum_wv = np.sum(wv,axis=0)
sum_v = sum_wv[1]
sum_w = sum_wv[0]
dp = np.full(sum_v+1,sum_w,dtype="int")
dp[0] = 0
for wei,val in wv:
    dp[val:] = np.minimum(dp[val:],dp[:-val]+wei)
for p in range(sum_v,1,-1):
    if(dp[p] <= w):
        print(p)
        break

Submission status: Accepted

Solution code:
import itertools as itr
inf = int(1e9)
n, w = list(map(int, input().split()))
warr = [0]*(n+1)
varr = [0]*(n+1)
for i in range(n):
    wi, vi = list(map(int, input().split()))
    warr[i+1] = wi
    varr[i+1] = vi
vm = max(varr)    
vmax = int(n*vm)+1
dp = [[inf]*vmax for _ in range(n+1)]
for i in range(n+1):
    dp[i][0] = 0
ans = 0
for i, j in itr.product(range(1,n+1), range(1,vmax)):
    jv = j-varr[i]
    if jv <0:
        jv = 0
    dp[i][j] = min(dp[i-1][jv] + warr[i], dp[i-1][j])
    if j > ans and dp[i][j] <= w:
        ans = j
  
print(ans)

Submission status: Accepted

Solution code:
N, W = [int(i) for i in input().split()]
V = [[int(i) for i in input().split()] for j in range(N)]

INF = 10 ** 18
sup_v = 10 ** 5
DP = [INF] * (sup_v + 1)
DP[0] = 0

for w, v in V:
    for i in range(sup_v, -1, -1):
        DP[i] = min(DP[i], DP[i-v] + w)

for i in range(sup_v, -1, -1):
    if DP[i] <= W:
        print(i)
        break

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
inf = 1 << 60
# dp[i][j] = i個目までの商品を価値jで運んだときの重さの最小値
dp = [[inf for j in range(100001)] for i in range(N + 1)]
dp[0][0] = 0

for i in range(1, N + 1):
    wi, vi = map(int, input().split())
    for v in range(0, 100001):
        dp[i][v] = min(dp[i][v], dp[i - 1][v])
    for v in range(vi, 100001):
        dp[i][v] = min(dp[i][v], dp[i - 1][v - vi] + wi)

ans = [(i,v) for i,v in enumerate(dp[N])]
ans = list(filter(lambda e: e[1] <= W, ans))
ans = ans[-1][0]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n,w = [int(x) for x in input().split()]
wv = np.array([[int(y) for y in input().split()] for z in range(n)],dtype=int)
sum_wv = np.sum(wv,axis=0)
sum_v = sum_wv[1]
sum_w = sum_wv[0]
dp = np.full(sum_v+1,sum_w,dtype="int")
dp[0] = 0
for wei,val in wv:
    dp[val:] = np.minimum(dp[val:],dp[:-val]+wei)
for p in range(sum_v,1,-1):
    if(dp[p] <= w):
        print(p)
        break

Submission status: Accepted

Solution code:
MAX_V = 100100
MAX_N = 101
INF = pow(2, 62)

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

dp = [[INF for _ in range(MAX_V)] for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N):
    for sum_v in range(MAX_V):
        # print(i, sum_v, sum_v - v[i] >= 0)
        if sum_v - v[i] >= 0:
            dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - v[i]] + w[i])
        dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])
        # print(dp[i + 1][sum_v])

ans = max([i for i in range(len(dp[N])) if dp[N][i] <= W])
# for l in dp:
#     print([n for n in l if not n == INF])

print(ans)

Submission status: Accepted

Solution code:
MAX_V = 100100
MAX_N = 101
INF = pow(2, 63)

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

dp = [[INF for _ in range(MAX_V)] for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N):
    for sum_v in range(MAX_V):
        # print(i, sum_v, sum_v - v[i] >= 0)
        if sum_v - v[i] >= 0:
            dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - v[i]] + w[i])
        dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])
        # print(dp[i + 1][sum_v])

ans = max([i for i in range(len(dp[N])) if dp[N][i] <= W])
# for l in dp:
#     print([n for n in l if not n == INF])

print(ans)

Submission status: Accepted

Solution code:
MAX_V = 100100
MAX_N = 101
INF = pow(2, 40)

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

dp = [[INF for _ in range(MAX_V)] for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N):
    for sum_v in range(MAX_V):
        # print(i, sum_v, sum_v - v[i] >= 0)
        if sum_v - v[i] >= 0:
            dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - v[i]] + w[i])
        dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])
        # print(dp[i + 1][sum_v])

ans = max([i for i in range(len(dp[N])) if dp[N][i] <= W])
# for l in dp:
#     print([n for n in l if not n == INF])

print(ans)

Submission status: Accepted

Solution code:
MAX_V = 100100
MAX_N = 101
INF = pow(2, 60)

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

dp = [[INF for _ in range(MAX_V)] for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N):
    for sum_v in range(MAX_V):
        # print(i, sum_v, sum_v - v[i] >= 0)
        if sum_v - v[i] >= 0:
            dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - v[i]] + w[i])
        dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])
        # print(dp[i + 1][sum_v])

ans = max([i for i in range(len(dp[N])) if dp[N][i] <= W])
# for l in dp:
#     print([n for n in l if not n == INF])

print(ans)

Submission status: Accepted

Solution code:
MAX_V = 100100
MAX_N = 101
INF = pow(2, 64)

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

dp = [[INF for _ in range(MAX_V)] for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N):
    for sum_v in range(MAX_V):
        # print(i, sum_v, sum_v - v[i] >= 0)
        if sum_v - v[i] >= 0:
            dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - v[i]] + w[i])
        dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])
        # print(dp[i + 1][sum_v])

ans = max([i for i in range(len(dp[N])) if dp[N][i] <= W])
# for l in dp:
#     print([n for n in l if not n == INF])

print(ans)

Submission status: Accepted

Solution code:
MAX_V = 100100
MAX_N = 101
INF = pow(2, 70)

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

dp = [[INF for _ in range(MAX_V)] for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N):
    for sum_v in range(MAX_V):
        # print(i, sum_v, sum_v - v[i] >= 0)
        if sum_v - v[i] >= 0:
            dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - v[i]] + w[i])
        dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])
        # print(dp[i + 1][sum_v])

ans = max([i for i in range(len(dp[N])) if dp[N][i] <= W])
# for l in dp:
#     print([n for n in l if not n == INF])

print(ans)

Submission status: Accepted

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

dp = [[float("inf")] * (sum_value + 1) for _ in range(N + 1)]

dp[0][0] = 0
for i in range(N):
    for j in range(sum_value + 1):
        if j - items[i][1] >= 0:
            dp[i + 1][j] = min(dp[i][j], dp[i][j - items[i][1]] + items[i][0])
        else:
            dp[i + 1][j] = dp[i][j]
for i in range(sum_value, -1, -1):
    w = dp[N][i]
    if w <= W:
        print(i)
        exit()

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


def solve():
    MaxV = 10**3*N+1
    dp = [[float('inf')]*MaxV for _ in range(N+1)]
    dp[0][0] = 0

    for i in range(1, N+1):
        for v in range(MaxV):
            if items[i-1][1] <= v:
                dp[i][v] = min(dp[i-1][v], dp[i-1]
                               [v-items[i-1][1]]+items[i-1][0])
            else:
                dp[i][v] = dp[i-1][v]
    ans = 0
    for i, v in enumerate(dp[N]):
        if v <= W:
            ans = i

    print(ans)


solve()

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


inf = float('inf')


def get_int():
    return int(input())


def get_str():
    return input().strip()


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


def get_char_list():
    return list(input().strip())


# inputs
N, W = 0, 0
Ws, Vs = [0], [0]


def set_inputs():
    global N, W, Ws, Vs
    N, W = get_list_of_int()
    ws, vs = [], []
    for _ in range(N):
        w, v = get_list_of_int()
        ws.append(w)
        vs.append(v)
    Ws, Vs = ws, vs


def main():
    set_inputs()
    value_sum = sum(Vs)
    weights = [inf] * (value_sum + 1)
    items = [0] * (value_sum + 1)
    weights[0] = 0
    for v in range(1, value_sum + 1):
        weights[v], items[v] = min((weights[v - Vs[i]] + Ws[i], items[v - Vs[i]] | 1 << i) if v - Vs[i] >= 0 and (items[v - Vs[i]] & 1 << i) == 0 else (inf, 0) for i in range(N))
    max_value = next(i for i in reversed(range(value_sum + 1)) if weights[i] <= W)
    print(sum(Vs[i] if items[max_value] & 1 << i > 0 else 0 for i in range(N)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

INF = float('inf')
num = 10**5 + 10
dp = [[INF]*(10**5+10) for _ in range(110)]

dp[0][0] = 0
for i in range(n):
    for v in range(10**5+1):
        if v + nap[i][1] <= 10**5+1:
            dp[i+1][v+nap[i][1]] = min(dp[i+1][v+nap[i][1]], dp[i][v] + nap[i][0])
        dp[i+1][v] = min(dp[i+1][v], dp[i][v])

for v in reversed(range(10**5+1)):
    if dp[n][v] <= w:
        print(v)
        exit()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**8) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

INF = pow(10,15)
SIZE = pow(10,5)+1

n,w = li()
wv = [tuple(li()) for _ in range(n)]

dp = [[INF]*(SIZE + 10) for _ in range(n+1)]
for i in range(n+1):
    dp[i][0] = 0

for idx in range(n):
    wi, vi = wv[idx]
    for val in range(SIZE):
        if val - vi < 0:
            dp[idx+1][val] = min(dp[idx+1][val],
                                 dp[idx][val])
            
        else:
            dp[idx+1][val] = min(dp[idx+1][val],
                                 dp[idx][val-vi]+wi,
                                 dp[idx][val])
        

ans = 0     
for val in range(SIZE):
    if dp[n][val] <= w:
        ans = val
        
print(ans)

Submission status: Accepted

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

dp = [{0:0} for i in range(N+1)]
for i,(w,v) in enumerate(src):
    for v0,w0 in dp[i].items():
        if v0 in dp[i+1]:
            dp[i+1][v0] = min(dp[i+1][v0], w0)
        else:
            dp[i+1][v0] = w0
        if w0+w > W: continue
        if v0+v in dp[i+1]:
            dp[i+1][v0+v] = min(dp[i+1][v0+v], w0+w)
        else:
            dp[i+1][v0+v] = w0+w
print(max(dp[-1].keys()))

Submission status: Accepted

Solution code:
from sys import stdin

INF = float('inf')

MAX = 100000
n, W = map(int, raw_input().strip().split())
inp = stdin.readlines()

dp = [[INF for i in xrange(MAX + 1)] for j in xrange(n)]

i = 0
for line in inp:
    wi, vi = map(int, line.strip().split())

    if i == 0:
        dp[0][vi] = wi
        dp[0][0] = 0
    else:
        for V in xrange(vi):
            dp[i][V] = dp[i - 1][V]
        
        dp[i][vi] = wi
        
        for V in xrange(vi, MAX + 1):
            dp[i][V] = min(dp[i][V], dp[i - 1][V], dp[i - 1][V - vi] + wi)

    i += 1

ans = 0
for v in xrange(MAX + 1):
    if dp[n - 1][v] <= W: ans = max(ans, v)

print ans

Submission status: Accepted

Solution code:
import numpy as np

N, W = map(int, input().split())
dp = np.full(N*1000+1,W+1,dtype=np.int)
dp[0] = 0

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

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

Submission status: Accepted

Solution code:
from sys import stdin
N, W = map(int, input().split())
s = list(list(map(int, line.split())) for line in stdin.read().splitlines())

sumv = sum(s[i][1] for i in range(N))

INF = 10e12
dp = [INF] * (sumv + 1)  # minimum w required to achieve  value V. dp[v] = w
dp[0] = 0

for n in range(N):
    w0, v0 = s[n]
    for v in range(sumv, v0 - 1, -1):
        dp[v] = min(dp[v], dp[v - v0] + w0)

for i in range(sumv, -1, -1):
    if dp[i] <= W:
        print(i)
        break

Submission status: Accepted

Solution code:
from sys import stdin
N, W = map(int, input().split())
s = list(list(map(int, line.split())) for line in stdin.read().splitlines())

sumv = sum(s[i][1] for i in range(N))

INF = 10e12
dp = [INF] * (sumv + 1)  # minimum w required to achieve  value V. dp[v] = w
dp[sumv] = sum(s[i][0] for i in range(N))
dp[0] = 0

for n in range(N):
    w0, v0 = s[n]
    for v in range(sumv, v0 - 1, -1):
        dp[v] = min(dp[v], dp[v - v0] + w0)

for i in range(sumv, -1, -1):
    if dp[i] <= W:
        print(i)
        break

Submission status: Accepted

Solution code:
from sys import stdin
N, W = map(int, input().split())
s = list(list(map(int, line.split())) for line in stdin.read().splitlines())

sumv = sum(s[i][1] for i in range(N))

INF = float('inf')
dp = [INF] * (sumv + 1)  # minimum w required to achieve  value V. dp[v] = w
dp[sumv] = sum(s[i][0] for i in range(N))
dp[0] = 0

for n in range(N):
    w0, v0 = s[n]
    for v in range(sumv, v0 - 1, -1):
        dp[v] = min(dp[v], dp[v - v0] + w0)

for i in range(sumv, -1, -1):
    if dp[i] <= W:
        print(i)
        break

Submission status: Accepted

Solution code:
n,k=(int(i) for i in input().split())
e = [[int(i) for i in input().split()] for j in range(n)]
#print(n,k,e)
maxv=0
for i in range(n):
    maxv+=e[i][1]
#print(maxv)

dp = [[0 for i in range(maxv+1)] for j in range(n+1)]
#print(dp)

for i in range(1, maxv+1):
    dp[0][i] = float('inf')
#print(dp)

for j in range(n):
    for i in range(1, maxv+1):
        if i < e[j][1] :
            dp[j + 1][i] = dp[j][i]
        else:
            dp[j + 1][i] = min(dp[j][i], dp[j][i - e[j][1]] +e[j][0])
#print(dp)
ans = 0
for i in range(maxv+1):
    if dp[n][i] <= k and ans < i:
        ans = i
        
print(ans)

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
a = []
for i in range(N):
    x,y = map(int,input().split())
    a.append([x,y])
max_v, inf = 10**5, float("inf")
dp = [0] + [inf]*max_v
 
for w, v in a:
    for i, j in zip(range(max_v-v, -1, -1), range(max_v, -1, -1)):
        if dp[i]+w <= W and dp[j] > dp[i]+w:
            dp[j] = dp[i]+w
 
for weight, value in zip(dp[::-1], range(max_v, -1, -1)):
    if weight != inf:
        print(value)
        break

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

N, W = map(int, raw_input().split())
DP = [0] + [INF] * 100000
for _ in range(N):
    w, v = map(int, raw_input().split())
    for i in range(v, 100001)[::-1]:
        DP[i] = min(DP[i], DP[i - v] + w)

for i in range(100001)[::-1]:
    if DP[i] <= W:
        print i
        break

Submission status: Accepted

Solution code:
N,W = map(int,input().split())
L = list(list(map(int,input().split())) for i in range(N))
upper = 10**5
dp = [float("inf") for i in range(upper+1)] #dp[i] = "価値の総和=i"の最小重さ
dp[0] = 0

for item in range(N) :
    weight_i,value_i = L[item]
    for value in range(upper, value_i-1, -1) :
        if dp[value - value_i] + weight_i <= W :
            dp[value] = min(dp[value], dp[value - value_i] + weight_i)

for value in range(upper, -1, -1) :
    if dp[value] < float("inf") :
        ans = value
        break

print(ans)

Submission status: Accepted

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

max_size = 10**5
dp = [float("inf") for i in range(max_size+1)]
dp[0] = 0

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

for i in range(max_size, -1, -1) :
    if dp[i] < float("inf") :
        ans = i
        break

print(ans)

Submission status: Accepted

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

n,k = IN()
dp = np.full(n*1000+1, k+1, dtype=int)
dp[0] = 0

for _ in range(n):
  w,v = IN()
  dp[v:] = np.minimum(dp[:-v]+w, dp[v:])

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

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
#FROM : https://atcoder.jp/contests/dp/submissions/3971457
import sys
import numpy as np
N,W = map(int, input().split())
it = iter(map(int, sys.stdin.read().split()))

dp = np.full(N*1000 + 1 , W+1, dtype=np.int64)
# print(dp)
dp[0] = 0
for w,v in zip(it, it):
    np.minimum(dp[:-v] + w, dp[v:], out=dp[v:])


# from pdb import set_trace; set_trace()
print( ((dp<=W) * np.arange(N*1000 + 1)).max() )

Submission status: Accepted

Solution code:
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)
V = max(v)

dp = [[float("inf")]*(N*V+1) for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
	for sum_v in range(N*V+1):
		if sum_v - v[i] >=0:
			dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - v[i]] + w[i])

		dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])


for sum_v in range(N*V+1):
	if dp[N][sum_v] <= W:
		ans = sum_v
print(ans)