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:
# -*- coding: utf-8 -*-

import numpy as np

EDPC = "https://atcoder.jp/contests/DP/"
TDPC = "https://atcoder.jp/contests/tdpc/tasks"

############### TESTCASE ###############
test = ""
#test = \
"""
3 8
3 3
4 5
5 6
ans 9
"""

"""
6 15
6 5
5 6
6 4
6 6
3 5
7 2
ans 17
"""

"""
N<100, W<10*9
wi<10**9, vi<10**3 
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################
#np.maximum(*arrays) == np.max(np.vstack(*arrays), axis=0)
#(np.maximum(np.array([1,3,2]), np.array([2, 1, 3])) == 
# np.max(np.vstack([np.array([1,3,2]), np.array([2, 1, 3])]), axis=0)).all()
################# MAIN #################

N, W = map(int, input2().split())
WV = np.array([tuple(map(int, input2().split())) for _ in range(N)], dtype="int64")
Wsum = np.sum(WV[:, 0])
Vsum = np.sum(WV[:, 1])

dp = np.full(Vsum+1, Wsum, dtype="int64")
for w, v in WV:
    dp[:Vsum-v+1] = np.min(np.vstack([dp[:Vsum-v+1], dp[v:] - w]), axis=0)
    
########################################
print(dp[dp < W+1].size - 1) # == np.where(dp < W+1)[0][-1]

Submission status: Accepted

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

import numpy as np

EDPC = "https://atcoder.jp/contests/DP/"
TDPC = "https://atcoder.jp/contests/tdpc/tasks"

############### TESTCASE ###############
test = ""
#test = \
"""
3 8
3 3
4 5
5 6
ans 9
"""

"""
6 15
6 5
5 6
6 4
6 6
3 5
7 2
ans 17
"""

"""
N<100, W<10*9
wi<10**9, vi<10**3 
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################
#np.maximum(*arrays) == np.max(np.vstack(*arrays), axis=0)
#(np.maximum(np.array([1,3,2]), np.array([2, 1, 3])) == 
# np.max(np.vstack([np.array([1,3,2]), np.array([2, 1, 3])]), axis=0)).all()
################# MAIN #################

N, W = map(int, input2().split())
WV = np.array([tuple(map(int, input2().split())) for _ in range(N)], dtype="int64")
Wsum = np.sum(WV[:, 0])
Vsum = np.sum(WV[:, 1])

dp = np.full(Vsum+1, Wsum, dtype="int64")
for w, v in WV:
    dp[:Vsum-v+1] = np.min(np.vstack([dp[:Vsum-v+1], dp[v:] - w]), axis=0)
    
print(np.where(dp < W+1)[0][-1])

Submission status: Accepted

Solution code:
def wczytaj_liste(): 
    wczytana_lista = input()
    lista_znakow = wczytana_lista.split()
    ostateczna_lista = []
    for element in lista_znakow:
        ostateczna_lista.append(int(element))
    return ostateczna_lista

def plecak_rozwiazanie_2(pojemnosc, lista_przedmitow):
    bez_przedmiotu = [0] + [10**18] * len(lista_przedmitow) * 1000
    for waga, wartosc in lista_przedmitow:
        z_przedmiotem = [None] * (len(lista_przedmitow) * 1000 +1)
        for i in range(len(z_przedmiotem)): 
            if wartosc > i: 
                z_przedmiotem[i] = bez_przedmiotu[i]
            else: 
                z_przedmiotem[i] = min(bez_przedmiotu[i-wartosc]+waga, bez_przedmiotu[i])
        bez_przedmiotu = z_przedmiotem
    for i in reversed(range(len(bez_przedmiotu))):
        if bez_przedmiotu[i] <= pojemnosc:
            return i

def plecak_2():
    N, W = wczytaj_liste()
    przedmioty = []
    for i in range(N): 
        waga, wartosc = wczytaj_liste()
        przedmioty.append((waga, wartosc))
    print (plecak_rozwiazanie_2(W,przedmioty))  
    
plecak_2()

Submission status: Accepted

Solution code:
def main():
    INF = 10 ** 12
    N, W = map(int, input().split())
    ws, vs = [], []
    for _ in range(N):
        w, v = map(int, input().split())
        ws.append(w)
        vs.append(v)
    sum_v = sum(vs)
    dp = [[INF] * (sum_v + 1) for _ in range(2)]
    dp[0][0] = 0
    for i in range(N):
        now_i, next_i = i & 1, (i+1) & 1
        for j in range(sum_v + 1):
            if j - vs[i] >= 0:
                dp[next_i][j] = min(dp[now_i][j], dp[now_i][j - vs[i]] + ws[i])
            else:
                dp[next_i][j] = dp[now_i][j]
    ans = 0
    for i in range(sum_v + 1):
        if dp[N&1][i] <= W:
            ans = i
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import defaultdict
INF = float("inf")


def solve(N: int, W: int, w: "List[int]", v: "List[int]"):

    L = 10**5+1
    DP = [[INF]*(L) for _ in range(N+1)]
    for i in range(N+1):
        DP[i][0] = 0

    for i in range(N):
        for j in range(L):
            DP[i+1][j] = DP[i][j]
            if 0 <= j-v[i] < L:
                DP[i+1][j] = min(DP[i+1][j], DP[i][j-v[i]]+w[i])

    for j in range(L-1, -1, -1):
        if DP[N][j] <= W:
            print(j)
            return

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    w = [int()] * (N)  # type: "List[int]"
    v = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        w[i] = int(next(tokens))
        v[i] = int(next(tokens))
    solve(N, W, w, v)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

for i in range(10**5,0,-1):
    if dp[-1][i] <= w:
        print(i)
        exit()

Submission status: Accepted

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

dp = [[10**11] * (n * (10**3) + 1) for _ in range(n+1)]

for i in range(1,n+1):
    dp[i-1][0] = 0
    w_temp, v_temp = wv[i-1]
    for j in range(1, (n*10**3)+1):
        if(j >= v_temp):
            dp[i][j] = min(dp[i-1][j],
                            dp[i-1][j-v_temp] + w_temp)
        else:
            dp[i][j] = dp[i-1][j]

last = dp[-1]
ans = 0
for j in range((n*10**3)+1):
    if(last[j] <= w):
        ans = j

print(ans)

Submission status: Accepted

Solution code:
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())

v_sum = sum(value)

dp = [[10**12 for _ in range(v_sum+1)] for i in range(n)]

for i in range(n):
    if i == 0:
        dp[i][0] = 0
        dp[i][value[0]] = weight[0]
    else:
        for k in range(v_sum+1):
            if k < value[i]:
                dp[i][k] = dp[i-1][k]
            else:
                dp[i][k] = min(dp[i-1][k], dp[i-1][k-value[i]]+weight[i])

for i in range(v_sum, -1, -1):
    if dp[n-1][i] <= w:
        print(i)
        exit()

Submission status: Accepted

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

weight = []
value = []

for i in range(n):
    wi, vi = map(int, input().split())
    weight.append(wi)
    value.append(vi)

v_sum = sum(value)

dp = [[10**12 for _ in range(v_sum+1)] for i in range(n)]

for i in range(n):
    if i == 0:
        dp[i][0] = 0
        dp[i][value[0]] = weight[0]
    else:
        for k in range(v_sum+1):
            if k < value[i]:
                dp[i][k] = dp[i-1][k]
            else:
                dp[i][k] = min(dp[i-1][k], dp[i-1][k-value[i]]+weight[i])

for i in range(v_sum, -1, -1):
    if dp[n-1][i] <= w:
        print(i)
        exit()

Submission status: Accepted

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

weight = []
value = []

for i in range(n):
    wi, vi = map(int, input().split())
    weight.append(wi)
    value.append(vi)

v_sum = sum(value)

dp = [[float('inf') for _ in range(v_sum+1)] for i in range(n)]

for i in range(n):
    if i == 0:
        dp[i][0] = 0
        dp[i][value[0]] = weight[0]
    else:
        for k in range(v_sum+1):
            if k < value[i]:
                dp[i][k] = dp[i-1][k]
            else:
                dp[i][k] = min(dp[i-1][k], dp[i-1][k-value[i]]+weight[i])

for i in range(v_sum, -1, -1):
    if dp[n-1][i] <= w:
        print(i)
        exit()

Submission status: Accepted

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

dp = [[float('inf') for _ in range(10**5+1)] for i in range(n)]

v_sum = 0

for i in range(n):
    wi, vi = map(int, input().split())
    v_sum += vi
    if i == 0:
        dp[i][0] = 0
        dp[i][vi] = wi
    else:
        for k in range(v_sum+1):
            if k < vi:
                dp[i][k] = dp[i-1][k]
            else:
                dp[i][k] = min(dp[i-1][k], dp[i-1][k-vi]+wi)

for i in range(v_sum, -1, -1):
    if dp[n-1][i] <= w:
        print(i)
        exit()

Submission status: Accepted

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

dp = [[float('inf') for _ in range(10**5+1)] for i in range(n)]

for i in range(n):
    wi, vi = map(int, input().split())
    if i == 0:
        dp[i][0] = 0
        dp[i][vi] = wi
    else:
        for k in range(10**5+1):
            if k < vi:
                dp[i][k] = dp[i-1][k]
            else:
                dp[i][k] = min(dp[i-1][k], dp[i-1][k-vi]+wi)

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

Submission status: Accepted

Solution code:
N,W = map(int, input().split())
weight = [0]*N
value = [0]*N
for i in range(N):
    w, v = map(int, input().split())
    weight[i] = w
    value[i] = v
    
dp = [[10**13]*(sum(value)+1) for _ in range(N+1)]

for i in range(N):
    dp[i][0] = 0
    for j in range(sum(value)+1):
        if j < value[i]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = min(dp[i][j], dp[i][j-value[i]]+weight[i])
            
for j in range(sum(value), -1, -1):
    if dp[N][j] <= W:
        ans = j
        break
        
print(ans)

Submission status: Accepted

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

N, W = map(int, input().split())
ws = [0]
vs = [0]

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

inf = sum(ws)+1
V = int(sum(vs))   # N * max(vs)
# V = int(100 * 1e3)   # N * max(vs)
# Weight[i番目までの品番][価値j]
Weight = [[inf for _ in range(V+1)] for __ in range(N+1)]

if ws[1] <= W:
  Weight[1][vs[1]] = ws[1]
  value_max = vs[1]
else:
  value_max = 0

for _ in range(N+1):
  Weight[_][0] = 0

# pdb.set_trace()

for num in range(2, N+1):
  for value in range(1, V+1):
    if value >= vs[num]:   # 新しい品物を入れられるとき
      Weight[num][value] = min(Weight[num][value], Weight[num-1][value], Weight[num-1][value-vs[num]] + ws[num])
      if Weight[num][value] <= W:
        value_max = max(value_max, value)
      else:
        Weight[num][value] = inf
    else:   # 新しい品物を入れられないとき
      # 重さを変えることはできない
      Weight[num][value] = Weight[num-1][value]
      # Weight[num][value] = max(Weight[num][value], Weight[num-1][value])

# print(Weight)
print(value_max)

Submission status: Accepted

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

N, W = map(int, input().split())
ws = [0]
vs = [0]

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

inf = 2e11
V = int(sum(vs))   # N * max(vs)
# V = int(100 * 1e3)   # N * max(vs)
# Weight[i番目までの品番][価値j]
Weight = [[inf for _ in range(V+1)] for __ in range(N+1)]

if ws[1] <= W:
  Weight[1][vs[1]] = ws[1]
  value_max = vs[1]
else:
  value_max = 0

for _ in range(N+1):
  Weight[_][0] = 0

# pdb.set_trace()

for num in range(2, N+1):
  for value in range(1, V+1):
    if value >= vs[num]:   # 新しい品物を入れられるとき
      Weight[num][value] = min(Weight[num][value], Weight[num-1][value], Weight[num-1][value-vs[num]] + ws[num])
      if Weight[num][value] <= W:
        value_max = max(value_max, value)
      else:
        Weight[num][value] = inf
    else:   # 新しい品物を入れられないとき
      # 重さを変えることはできない
      Weight[num][value] = Weight[num-1][value]
      # Weight[num][value] = max(Weight[num][value], Weight[num-1][value])

# print(Weight)
print(value_max)

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
V = 10**5
weight = [[float("inf") for i in range(V + 1)] for j in range(N + 1)]
weight[0][0] = 0
for i in range(1, N + 1):
    w, v = map(int, input().split())

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

ans = 0
for j in range(V + 1):
    if weight[N][j] <= W:
        ans = max(ans, j)

print(ans)

Submission status: Accepted

Solution code:
n,w=map(int,input().split())
sum_val=0
ws=[]
vs=[]
for i in range(n):
    wi,vi=map(int,input().split())
    ws.append(wi)
    vs.append(vi)
    sum_val+=vi
dp=[10**12 for i in range(sum_val+1)]
dp[0]=0
for i in range(n):
    for j in range(sum_val-vs[i],-1,-1):
        dp[j+vs[i]]=min(dp[j+vs[i]],dp[j]+ws[i])
ans=0
for i in range(sum_val+1):
    if dp[i]<=w:
        ans=max(ans,i)
print(ans)

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)
max_v = n*max(v)
c = [[0 for j in range(max_v)] for i in range(n)]

for i in range(max_v):
    if i+1 > v[0]:
        c[0][i] = float('inf')
    else:
        c[0][i] = w[0]

for i in range(1,n):
    for j in range(max_v):
        if j+1 > v[i]:
            c[i][j] = min(c[i-1][j], w[i]+c[i-1][j-v[i]])
        else:
            c[i][j] = min(c[i-1][j], w[i])
        # if j+1 < w[i]:
        #     c[i][j] = c[i-1][j]
        # else:
        #     diff = (j+1)-w[i]
        #     if diff > 0:
        #         c[i][j] = max(c[i-1][j], v[i]+c[i-1][diff-1])
        #     else:
        #         c[i][j] = max(c[i-1][j], v[i])
for i in range(1,max_v+1)[::-1]:
    if c[-1][i-1] <= W:
        print(i)
        break

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
INF = float('inf')
MAX_V = 100100

dp = [[INF for _ in range(MAX_V+1)] for _ in range(N+1)]
dp[0][0] = 0
weight = []
value = []
for i in range(N):
    w, v = map(int, input().split())
    weight.append(w)
    value.append(v)

for i in range(N):
    for sum_v in range(MAX_V + 1):
        # i 番目の品物を選ぶ場合
        if sum_v - value[i] >= 0:
            dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - value[i]] + weight[i])
        # i 番目の品物を選ばない場合
        dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])

ans = 0
for sum_v in range(MAX_V + 1):
    if dp[N][sum_v] <= W:
        ans = sum_v

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
N, W = map(int, input().split())
H = []
H.append([0,0])
for _ in range(N) :
  S = str(input())
  H.append(list(map(int, S.split())))
#index
Hw = 0
Hv = 1
 
#print (H)
Vmax = (10**3) * N
#Vmax = (60) * N
 
#合計価値viのときの最小重量
dp = [[sys.maxsize for _ in range(Vmax+1)] for _ in range(N+1)]

for i in range(1, N+1) :
  #Niのvの反映
  dp[i][H[i][Hv]] = min(H[i][Hw], dp[i][H[i][Hv]])
  #VjのときにNiを選んだ場合/選ばない場合
  for j in range(Vmax+1) :
    if dp[i-1][j] != sys.maxsize :
      #Niを選ばないとき
      dp[i][j] = min(dp[i-1][j], dp[i][j])
      #Niを選ぶとき
      if (j + H[i][Hv]) <= Vmax+1 :
        dp[i][j + H[i][Hv]] = min(dp[i-1][j] + H[i][Hw], dp[i][j + H[i][Hv]])
        
  #print (dp[i][:])
  """
  out = copy.deepcopy(dp[i][:])
  line = ""
  for x in range(len(out)) :
      if out[x] == sys.maxsize :
        line += "_" + "\t"
      else :    
        line += str(out[x]) + "\t"
  print(line)
  """

for k in range(Vmax) :
  if dp[N][Vmax-k] <= W :
    print(Vmax-k)
    exit()

Submission status: Accepted

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

N, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(N)]
wv = np.array(wv)
V = max(wv[:, 1])
dp = np.array([float('inf')]*(N*V+1))

dp[0] = 0
for i in range(N):
    w, v = wv[i]
    dp[v:] = np.minimum(dp[v:], dp[:-v]+w)
print(max(np.where(dp<=W)[0]))

Submission status: Accepted

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

v_max = 100*(10**3) + 1
INF = 10**18

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

for i in range(n):
    weight, val = info[i]
    for j in range(v_max):
        dp[i+1][j] = min(dp[i][j], dp[i+1][j])
        if j - val >= 0:
            dp[i+1][j] = min(dp[i][j-val] + weight, dp[i+1][j])

ans = 0
for i, num in enumerate(dp[n]):
    if num <= w:
        ans = i
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

input = sys.stdin.readline


def main():
    N, W = [int(i) for i in input().strip().split()]
    MAX_V = 10**3 * N

    dp = np.full([N + 1, MAX_V + 1], np.inf)
    dp[0][0] = 0
    for n in range(N):
        w, v = [int(i) for i in input().strip().split()]
        dp[n + 1] = dp[n]
        dp[n + 1][v:] = np.minimum(dp[n][v:], dp[n][:-v] + w)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections


MAX_N = 110
MAX_V = 100100

Item = collections.namedtuple("Item", ("weight", "value"))


def main():
    N, W = [int(i) for i in input().split()]
    items = [Item(int(i), int(j)) for (i, j) in [input().split() for _ in range(N)]]
    dp = [[float('inf')] * MAX_V for _ in range(MAX_N)]

    dp[0][0] = 0

    for i in range(N):
        item = items[i]
        for sum_v in range(MAX_V):
            # i番目の品物を選ぶ
            if sum_v - item.value >= 0:
                dp[i + 1][sum_v] = min(dp[i + 1][sum_v], dp[i][sum_v - item.value] + item.weight)
            dp[i + 1][sum_v] = min(dp[i + 1][sum_v], dp[i][sum_v])

    ans = 0
    for sum_v in range(MAX_V):
        if dp[N][sum_v] <= W:
            ans = sum_v

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

sum_v_max = n * 1000
dp = [0] + [float('inf')] * sum_v_max

for i in range(n):
    for j in range(sum_v_max, 0, -1):
        dp[j] = min(dp[j], dp[max(j - lst[i][1], 0)] + lst[i][0])

print(bisect_right(dp, w) - 1)

Submission status: Accepted

Solution code:
N,W=map(int,input().split())
INF=10**9+1
import numpy as np
dp=np.full(N*1000+1,INF,dtype=int)
dp[0]=0
for i in range(N):
  w,v=map(int,input().split())
  np.minimum(dp[:-v]+w,dp[v:],out=dp[v:])

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

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
INF = 10**12
goods = [tuple(map(int, input().split())) for _ in range(N)]
total_val = 0
for item in goods:
    total_val += item[1]
dp = [[INF for i in range(total_val+1)] for j in range(N+1)]
dp[0][0] = 0
for i, item in enumerate(goods):
    i += 1
    for v in range(total_val+1):
        if v < item[1]:
            dp[i][v] = dp[i-1][v]
        else:
            dp[i][v] = min(dp[i-1][v], dp[i-1][v-item[1]] + item[0])

for i in range(total_val+1):
    if dp[N][i] <= W:
        ans = i
print(ans)

Submission status: Accepted

Solution code:
INF = float("inf")


def main():
    N, W = map(int, input().split())

    # dp[0..N][value]: weight
    dp = [[INF for i in range(1000 * 100 + 1)] for _ in range(N + 1)]
    dp[0][0] = 0

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

        for v in range(1000 * 100 + 1):
            if value <= v:
                dp[n + 1][v] = min(dp[n][v - value] + weight, dp[n][v])
            else:
                dp[n + 1][v] = dp[n][v]

    for v in range(1000 * 100, -1, -1):
        if dp[N][v] <= W:
            print(v)
            break


main()

Submission status: Accepted

Solution code:
n,w = map(int, input().split())
a = [[int(x) for x in input().split()] for i in range(n)]
dp = [10**100 for i in range(10**5 + 1)]
dp[0] = 0
for i in range(n):
    for j in range(10**5, -1, -1):
        if dp[j] != 10**100:
            dp[j + a[i][1]] = min(dp[j + a[i][1]], dp[j] + a[i][0])
for i in range(10**5, -1, -1):
    if dp[i] <= w:
        print(i)
        exit(0)

Submission status: Accepted

Solution code:
N,W=map(int,input().split())
lw=[]
lv=[]
for i in range(N):
    w,v=map(int,input().split())
    lw.append(w)
    lv.append(v)
ldp=[[float("inf") for j in range(10**5)] for i in range(N)]
for j in range(10**5):
    if lv[0]>=j+1:
        ldp[0][j]=lw[0]
for i in range(1,N):
    for j in range(10**5):
        ldp[i][j]=min(ldp[i-1][j],ldp[i-1][j+1-lv[i]-1]+lw[i] if j+1-lv[i]>0 else lw[i])
for j in range(10**5-1,-1,-1):
    if ldp[N-1][j]<=W:
        print(j+1)
        break

Submission status: Accepted

Solution code:
N,W = map(int,input().split())
wv = []
for i in range(N):
    a = list(map(int,input().split()))
    wv.append(a)
sumv = sum([i[1] for i in wv])
dp = [[10**10 for i in range(sumv+1)] for j in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
for i in range(N):
    for j in range(sumv+1):
        if j - wv[i][1] >=0 :
            dp[i+1][j] = min(dp[i][j], dp[i][j-wv[i][1]] + wv[i][0])
        else:
            dp[i+1][j] = dp[i][j]

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

Submission status: Accepted

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

W = []
V = []

for _ in range(n):
    s, t = map(int, input().split())
    W.append(s)
    V.append(t)

tot_v = sum(V)

Dp = [[10**10 for _ in range(tot_v+1)] for _ in range(n+1)]
Dp[0][0] = 0


for i in range(n):
    for v in range(tot_v+1):
        if v >= V[i]:
            Dp[i+1][v] = min(Dp[i][v], Dp[i][v-V[i]]+W[i])

        else:
            Dp[i+1][v] = Dp[i][v]

ans = []
for v in range(tot_v+1):
    if Dp[n][v] <= w:
        ans.append(v)

print(max(ans))

Submission status: Accepted

Solution code:
N,W = map(int,input().split())
WV = [[0,0]] + [list(map(int,input().split())) for _ in range(N)]

sumV = sum([v for _,v in WV])
dp = [[float('inf') for _ in range(sumV+1)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(1, N+1):
  tmpw, tmpv = WV[i]
  for j in range(sumV+1):
    if j-tmpv>=0:
      dp[i][j] = min(dp[i-1][j],dp[i-1][j-tmpv]+tmpw)
    else:
      dp[i][j] = dp[i-1][j]

ans = -float('inf')
for row in dp:
  for j,w in enumerate(row):
    if w<=W and ans<j:
      ans = j
print(ans)

Submission status: Accepted

Solution code:
n,w = map(int,input().split())
wv = [list(map(int,input().split())) for _ in range(n)]
#dp[i][j]はi番目まででj(価値の合計)になる重さの最小値
sum_value = 0
for i in range(n):
  sum_value += wv[i][1]
dp = [[float("inf")]*(sum_value+1) for j in range(n)]
for k in range(sum_value+1):
  if k == wv[0][1]:
    dp[0][k] = wv[0][0]
for a in range(n-1):
  for b in range(sum_value+1):
    if dp[a][b-wv[a+1][1]] != float("inf"):
      dp[a+1][b] = min(dp[a][b-wv[a+1][1]]+wv[a+1][0],dp[a][b])
    elif b == wv[a+1][1]:
      dp[a+1][b] = min(wv[a+1][0],dp[a][b])
    else:
      dp[a+1][b] = dp[a][b]
ans = 0
for c in range(sum_value+1):
  if dp[-1][c] != float("inf") and w >= dp[-1][c]:
    ans = c
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, W = map(int, input().split())
b = [tuple(map(int, input().split())) for _ in range(N)]
dp = [[float("inf")] * (pow(10, 5) + 1) for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N):
  for j in range(pow(10, 5) + 1):
    if dp[i][j] != float("inf"):
      dp[i + 1][j + b[i][1]] = min(dp[i][j] + b[i][0], dp[i + 1][j + b[i][1]])
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
res = 0
for j in range(pow(10, 5) + 1):
  if dp[-1][j] <= W:
    res = max(res, j)
print(res)

Submission status: Accepted

Solution code:
from pprint import pprint
import sys

v_max = (10 ** 3) * 100
inf = 10 ** 10
# print(v_max)
N, W = map(int, sys.stdin.readline().strip().split(" "))

w = []
for _ in range(N):
    w_i, v_i = map(int, sys.stdin.readline().strip().split(" "))
    w.append((w_i, v_i))

dp = []
for _ in range(N+1):
    dp.append([inf] * (v_max+1))
dp[0][0] = 0

ans = 0
for i in range(N):
    for j in range(v_max+1):
        if j - w[i][1] >= 0 and W >= dp[i][j - w[i][1]] + w[i][0]:
            # dp[i+1][j] = max(dp[i][j - w[i][0]] + w[i][1], dp[i+1][j])
            dp[i+1][j] = dp[i][j - w[i][1]] + w[i][0]
        dp[i+1][j] = min(dp[i][j], dp[i+1][j])
        if dp[i+1][j] <= W:
            # print(dp[i+1][j], j)
            # ans = max(ans, dp[i+1][j])
            ans = max(ans, j)
# pprint(dp)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    # input
    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())
    
    # init dp
    v_max = sum(v)
    dp = [[float("inf") for i in range(v_max+1)] for _ in range(N)]
    dp[0][v[0]] = w[0]
    for i in range(N):
        dp[i][0] = 0

    for i in range(1,N):
        for j in range(v_max+1):
            if j<v[i]:
                dp[i][j] = dp[i-1][j]
            else:
                dp[i][j] = min(dp[i-1][j],dp[i-1][j-v[i]]+w[i])
    ans = 0
    for i in range(v_max+1):
        if dp[-1][i]<=W:
            ans = i
        else:
            pass
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
N,W = map (int,input().split()) 
dp = [[float("inf") for i in range(10**5+1)] for j in range(N+1)]
weight = []
value = []
for i in range(N):
    w,v = map( int, input().split() )
    weight.append(w)
    value.append(v)

dp[0][0] = 0

for i in range(1,N+1):
    for j in range(10**5+1):
        if j - value[i-1] >= 0:
            dp[i][j] = min( dp[i-1][ j - value[i-1] ] + weight[i-1], dp[i][j])
        dp[i][j] = min(dp[i][j],dp[i-1][j])
        
for i in range(10**5,-1,-1):
    if dp[N][i] <= W:
        print(i)
        sys.exit()

Submission status: Accepted

Solution code:
N,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[10**12 for _ in range(10**5+1)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(1,N+1):
    for j in range(10**5+1):
        dp[i][j] = dp[i-1][j]
        if A[i-1][1]<=j:
            dp[i][j] = min(dp[i][j],dp[i-1][j-A[i-1][1]]+A[i-1][0])
vmax = 0
for j in range(10**5+1):
    if dp[N][j]<=W:
        vmax = max(vmax,j)
print(vmax)

Submission status: Accepted

Solution code:
N, W = map(int, input().split())
dp = [[float("inf")] * (10**5 + 1) for i in range(N+1)]
dp[0][0] = 0

for i in range(N):
  w, v = map(int, input().split())
  for j in range(10**5 + 1):
    dp[i+1][j] = min(dp[i][j], (dp[i][j-v] if j >= v else 0) + w)

ans = 0
for i in range(10**5 + 1):
  if dp[-1][i] <= W:
    ans = i
print(ans)

Submission status: Accepted

Solution code:
n, W = map(int, input().split())
wv = [ list(map(int, input().split())) for _ in range(n) ]
dp = [ [float("inf")]*(10**5+1) for _ in range(n+1) ]
dp[0][0] = 0
for i in range(n):
  w, v = wv[i]
  for j in range(10**5+1):
    if j < v:
      dp[i+1][j] = dp[i][j]
    else:
      dp[i+1][j] = min(dp[i][j], dp[i][j-v]+w)
ans = 0
for j in range(10**5+1):
  if dp[n][j] <= W:
    ans = j
print(ans)