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: Wrong Answer

Solution code:
from bisect import bisect_left
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 = [[0 for i in range(sumv+1)] for j in range(N+1)]

for i in range(N):
    for j in range(sumv+1):
        if j - wv[i][1] >=0 :
            dp[i+1][j] = max(dp[i+1][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:
        maxa = dp[-1][i]
        break

print(bisect_left(dp[-1],maxa))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
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 = [[0 for i in range(sumv+1)] for j in range(N+1)]

for i in range(N):
    for j in range(sumv+1):
        if j - wv[i][1] >=0 :
            dp[i+1][j] = max(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:
        maxa = dp[-1][i]
        break

print(bisect_left(dp[-1],maxa))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
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 = [[0 for i in range(sumv+1)] for j in range(N+1)]

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

Submission status: Wrong Answer

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 = [[0 for i in range(sumv+1)] for j in range(N+1)]

for i in range(N):
    for j in range(sumv+1):
        if j - wv[i][1] >=0 :
            dp[i+1][j] = max(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: Wrong Answer

Solution code:
N,W = map(int,input().split())
wv = []
for i in range(N):
    a = list(map(int,input().split()))
    wv.append(a)
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(W+1):
        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])
        else:
            dp[i][j] = dp[i-1][j]
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
## https://atcoder.jp/contests/dp/tasks/dp_d
## TODO

def solution():
    # max value for picked 0..n items with current bag weight (0), value(1)
    res = [[-1,-1] for i in range(n)]
    res[0][0] = items[0][0]
    res[0][1] = items[0][1]
    print("res[{}] = {}".format(0, res[0]))
    for i in range(1,n):
        # res[i] = max_but_limited_of(res[i-1], res[i-2] + items[i], res[i-3] + items[i]....)
        currentWeight = res[i-1][0]
        maxValue = res[i-1][1]

        for j in range(i, -1, -1):
            if res[j][1] + items[i][1] > maxValue and res[j][0] + items[i][0] <=w:
                currentWeight = res[j][0] + items[i][0]
                maxValue = res[j][1] + items[i][1]

        res[i][0] = currentWeight
        res[i][1] = maxValue

        print("res[{}] = {}".format(i, res[i]))

    return res[-1][1]


if __name__ == "__main__":
    n, w = list(map(int, input().split(" ")))

    items = [[-1]*2 for i in range(n)]

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

    print("n = {} w = {} items = {}".format(n,w,items))
    print(solution())

Submission status: Wrong Answer

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

dp = [0] + [float("inf")]*(10**3)
for i in range(n):
    for j in range(10**3,wv[i][1]-1,-1):
        dp[j] = min(dp[j],dp[j-wv[i][1]]+wv[i][0])

res = 0
for v,e in enumerate(dp):
    if e <= w:
        res = v
print(res)

Submission status: Wrong Answer

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)

for i in range(N+1):
    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):
    if dp[N][i] <= W:
        print(i)
        sys.exit()

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if j <= vi:
            dp[i][j] = min(dp[i-1][j], wi)
        if j >= vi:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)

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

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if j >= vi:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)
        else:
            dp[i][j] = min(dp[i-1][j], wi)

ans = 0
for v, x in enumerate(dp[n]):
    if x <= w:
        tmp = v
        ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if wi > j:
            dp[i][j] = min(dp[i-1][j], dp[i][j])
        else:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)

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

# for i in dp:
#     print(i)

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if wi >= j:
            dp[i][j] = min(dp[i-1][j], wi)
        else:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)

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

# for i in dp:
#     print(i)

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if j >= vi:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)
        else:
            dp[i][j] = min(dp[i-1][j], wi)

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

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if j >= vi:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)
        else:
            dp[i][j] = min(dp[i-1][j], wi)

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

print(dp)

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 10**5
# for x in a:
#     vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
# for i in range(n+1):
#     dp[i][0] = 0
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if j >= vi:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)
        else:
            dp[i][j] = dp[i-1][j]

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

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
# for i in range(n+1):
#     dp[i][0] = 0
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if j >= vi:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)
        dp[i][j] = min(dp[i-1][j], dp[i][j])

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

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+1) for _ in range(n+1)]
# for i in range(n+1):
#     dp[i][0] = 0
dp[0][0] = 0

for i in range(1, n+1):
    for j in range(vmax+1):
        wi, vi = a[i-1]['w'], a[i-1]['v']
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if j >= vi:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-vi] + wi)
        else:
            dp[i][j] = dp[i-1][j]

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

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = [dict(zip(['w', 'v'], map(int, input().split()))) for _ in range(n)]

# 価値の最大値を算出
vmax = 0
for x in a:
    vmax+=x['v']

# dpテーブルを初期化
INF = 10**7
dp = [[INF]*(vmax+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(vmax+1):
        # i番目の品物を選ぶ場合（価値がピッタリ v になるように考える）
        if j >= a[i-1]['v']:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-a[i-1]['v']] + a[i-1]['w'])
        else:
            dp[i][j] = dp[i-1][j]

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

dp = defaultdict(lambda: 1e9)
dp[0] = 1
ans = 0
for _ in range(N):
    w, v = map(int, input().split())
    for k in range(MAX_VAL, -1, -1):
        dp[k + v] = min(dp[k+v], dp[k] + w)
        if dp[k + v] <= W:
            ans = max(ans, k + v)


print(ans)

Submission status: Wrong Answer

Solution code:
import math
import numpy as np


def inputIntList():
    return [int(s) for s in input().split()]


def inputInt():
    return int(input())


def main():
    N, capacity = inputIntList()
    w = [0 for _ in range(N)]
    v = [0 for _ in range(N)]
    for i in range(N):
        w[i], v[i] = inputIntList()

    # dp = [[0 for _ in range(N * 1000 + 1)] for _ in range(N + 1)]
    maxValue = max(v)
    dp = np.zeros((N + 1, maxValue * N + 1))
    for i in range(N):
        for j in range(N * maxValue + 1):
            if j < v[i]:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i][j - v[i]] + w[i], dp[i][j])
            # print(dp)
    return max(np.argwhere(dp[N] < capacity) + 1)[0]


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
# dp[i][j]: i+1個の品物から価値がj以上になるように選んだときの重量の最小値
N, W = map(int, input().split())
w, v = [], []
for _ in range(N):
    tmp = list(map(int, input().split()))
    w.append(tmp[0])
    v.append(tmp[1])
V = sum(v)
dp = [[float("inf") for _ in range(V+1)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
    for j in range(V+1):
        if j - v[i] >= 0:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + w[i])
        else:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])

# dp[N][j]がW以下であるときのｊの最大値
ans = []
for j in range(V+1):
    if dp[N][j] <= W:
        ans.append(j)
print(max(ans))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
Vmax = 100 * N

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

dp = [[10**22] * (Vmax+1) for _ in range(N+1)]
dp[0][0] = 0

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

ans = 0

for j in range(Vmax):
    if dp[N-1][j] <= W:
        ans = max(ans,j)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def solve():
  n,wmax = (int(i) for i in input().split())
  query = []
  v = 100005
  for i in range(n):
    tw,tv = (int(i) for i in input().split())
    query.append((tw,tv))
  dp = [[-1]*(v+1) for _ in range(n+1)]
  for i in range(n):
    dp[i][0] = 0

  for i in range(n):
    nw,nv = query[i]
    for value in range(v):
      if value+nv <= v:
        if dp[i+1][value+nv] ==-1:
          if dp[i][value] > -1 and dp[i][value]+nw <=wmax:
            dp[i+1][value+nv] = dp[i][value]+nw
        else:
          if dp[i][value] != -1:
            dp[i+1][value+nv] = min(dp[i][value]+nw,dp[i+1][value+nv])
      
      dp[i+1][value] = max(dp[i][value],dp[i+1][value])
  ans = 0
  for value in range(v):
    if dp[n][value] <= wmax and dp[n][value]!= -1:
      ans = max(ans,value)
  print(ans)
solve()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

"""
20191120
93分
test:
6 8
2 3
1 2
3 6
2 1
1 3
5 85
"""


def main():
    N, W = map(int, input().split())
    ws = [None for _ in range(N)]
    vs = [None for _ in range(N)]
    for i in range(N):
        ws[i], vs[i] = map(int, input().split())
    V = sum(vs)

    # initialize
    dp = [[None for _ in range(V+1)] for _ in range(N)]
    dp[0][0] = 0
    existance = [set([]) for _ in range(N)]
    existance[0].add(0)
    dp[0][vs[0]] = ws[0]
    existance[0].add(vs[0])

    for n in range(N-1):
        for v in existance[n]:
            # not-use-update
            # if dp[n+1][v] is None:
            #     dp[n+1][v] = dp[n][v]
            # else:
            #     dp[n+1][v] = min(dp[n][v], dp[n+1][v])
            dp[n+1][v] = dp[n][v]

            existance[n+1].add(v)

            # use-update
            if dp[n+1][v+vs[n+1]] is None:
                if dp[n][v] + ws[n+1] <= W:
                    dp[n+1][v+vs[n+1]] = dp[n][v] + ws[n+1]
                    existance[n+1].add(v+vs[n+1])
            else:
                dp[n+1][v+vs[n+1]] = min(dp[n][v] + ws[n+1], dp[n+1][v+vs[n+1]])
                existance[n+1].add(v+vs[n+1])

    ans = 0
    for v in range(V, 0, -1):
        if dp[-1][v] is not None:
            ans = v
            break

    return ans


def miss_main():
    N, W = map(int, input().split())
    ws = [None for _ in range(N)]
    vs = [None for _ in range(N)]
    for i in range(N):
        ws[i], vs[i] = map(int, input().split())

    # initialize
    dp = [[None for _ in range(W+1)] for _ in range(N)]
    dp[0][0] = 0
    existance = [set([]) for _ in range(N)]
    existance[0].add(0)
    if W >= ws[0]:
        dp[0][ws[0]] = vs[0]
        existance[0].add(ws[0])

    for n in range(N-1):
        for w in existance[n]:
            # not-use-update
            if dp[n+1][w] is None:
                dp[n+1][w] = dp[n][w]
            else:
                dp[n+1][w] = max(dp[n][w], dp[n+1][w])
            existance[n+1].add(w)

            # use-update
            if w + ws[n+1] <= W:
                if dp[n+1][w+ws[n+1]] is None:
                    dp[n+1][w+ws[n+1]] = dp[n][w] + vs[n+1]
                else:
                    dp[n+1][w+ws[n+1]] = max(dp[n][w] + vs[n+1], dp[n+1][w+ws[n+1]])
                existance[n+1].add(w+ws[n+1])
    # print(np.array(dp))

    ans = 0
    for w in range(1, W+1):
        if dp[-1][w] is not None and dp[-1][w] > ans:
            ans = dp[-1][w]

    return ans


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
# coding: utf-8

"""
20191120

test:
6 8
2 3
1 2
3 6
2 1
1 3
5 85
"""


def main():
    N, W = map(int, input().split())
    ws = [None for _ in range(N)]
    vs = [None for _ in range(N)]
    for i in range(N):
        ws[i], vs[i] = map(int, input().split())
    V = sum(vs)

    # initialize
    dp = [[None for _ in range(V+1)] for _ in range(N)]
    dp[0][0] = 0
    existance = [set([]) for _ in range(N)]
    existance[0].add(0)
    dp[0][vs[0]] = ws[0]
    existance[0].add(vs[0])

    for n in range(N-1):
        for v in existance[n]:
            # not-use-update
            # if dp[n+1][v] is None:
            #     dp[n+1][v] = dp[n][v]
            # else:
            #     dp[n+1][v] = min(dp[n][v], dp[n+1][v])
            dp[n+1][v] = dp[n][v]

            existance[n+1].add(v)

            # use-update
            if dp[n+1][v+vs[n+1]] is None:
                if dp[n][v] + ws[n+1] <= W:
                    dp[n+1][v+vs[n+1]] = dp[n][v] + ws[n+1]
                    existance[n+1].add(v+vs[n+1])
            else:
                dp[n+1][v+vs[n+1]] = min(dp[n][v] + vs[n+1], dp[n+1][v+vs[n+1]])
                existance[n+1].add(v+vs[n+1])

    ans = 0
    for v in range(V, 0, -1):
        if dp[-1][v] is not None:
            ans = v
            break

    return ans


def miss_main():
    N, W = map(int, input().split())
    ws = [None for _ in range(N)]
    vs = [None for _ in range(N)]
    for i in range(N):
        ws[i], vs[i] = map(int, input().split())

    # initialize
    dp = [[None for _ in range(W+1)] for _ in range(N)]
    dp[0][0] = 0
    existance = [set([]) for _ in range(N)]
    existance[0].add(0)
    if W >= ws[0]:
        dp[0][ws[0]] = vs[0]
        existance[0].add(ws[0])

    for n in range(N-1):
        for w in existance[n]:
            # not-use-update
            if dp[n+1][w] is None:
                dp[n+1][w] = dp[n][w]
            else:
                dp[n+1][w] = max(dp[n][w], dp[n+1][w])
            existance[n+1].add(w)

            # use-update
            if w + ws[n+1] <= W:
                if dp[n+1][w+ws[n+1]] is None:
                    dp[n+1][w+ws[n+1]] = dp[n][w] + vs[n+1]
                else:
                    dp[n+1][w+ws[n+1]] = max(dp[n][w] + vs[n+1], dp[n+1][w+ws[n+1]])
                existance[n+1].add(w+ws[n+1])
    # print(np.array(dp))

    ans = 0
    for w in range(1, W+1):
        if dp[-1][w] is not None and dp[-1][w] > ans:
            ans = dp[-1][w]

    return ans


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
import sys
if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
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())

# below model is for VALUE Constraint
V = int(pow(10,3)) * N
dp = [[float('INF') for _ in range(V+1)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
	for sum_v in range(V):
		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])

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

print(max_v)

Submission status: Wrong Answer

Solution code:
import sys
if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline

def chmin(a, b):
	if a <= b:
		return a
	else:
		return b

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

# below model is for Weight Constraint
# 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 w[i] <= sum_w:
# 			dp[i+1][sum_w] = max(dp[i][sum_w-w[i]]+v[i], dp[i][sum_w])
# 		else:
# 			dp[i+1][sum_w] = dp[i][sum_w]

# below model is for VALUE Constraint
V = (max(v)) * N
dp = [[float('INF') for _ in range(V+1)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
	for sum_v in range(V):
		if sum_v - v[i] >= 0:
			dp[i+1][sum_v] = chmin(dp[i+1][sum_v], dp[i][sum_v-v[i]]+w[i])
		dp[i+1][sum_v] = chmin(dp[i+1][sum_v], dp[i][sum_v])

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

print(max_v)

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
from collections import defaultdict
n, W = map(int, sys.stdin.readline().split())

dp = [defaultdict(int) for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    for vb, wb in dp[i-1].items():
        dp[i][vb] = min(wb, dp[i].get(vb, 100000001))
        tmp = min(wb + w, dp[i].get(vb+ v, 100000001))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    dp[i][v] = min(dp[i].get(v, 100000001), w)

#print(dp[-1].items())
print(max(dp[-1].keys()))

Submission status: Wrong Answer

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

table = {i: [10**10]*(10**5+1) for i in range(N+1)}
table[0][0] = 0
def dp(N, V):
  for i in range(1, N+1):
    print(i)
    w, v = items[i-1]
    for j in range(V+1):
      table[i][j] = min(table[i-1][j-v]+w, table[i-1][j])
  for k in range(V, 0, -1):
    if table[N][k] <= W:
      return k

print(dp(N, 10**5))

Submission status: Wrong Answer

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

table = {i: [10**10]*(10**5+1) for i in range(N+1)}
table[0][0] = 0
def dp(N, V):
  for i in range(1, N+1):
    w, v = items[i-1]
    for j in range(1, V+1):
      table[i][j] = min(table[i-1][j-v]+w, table[i-1][j])
  for k in range(V, -1, -1):
    if table[N][k] <= W:
      return k

print(dp(N, 10**5))

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
n, W = map(int, sys.stdin.readline().split())

dp = [dict() for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    dp[i][v] = min(w , dp[i-1].get(v, 10**9+1))
    for vb, wb in dp[i-1].items():
        dp[i][vb] = min(dp[i].get(vb, 10*9+1), wb)
        tmp = min(wb + w, dp[i].get(vb+ v, 10**9+1))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    #print(dp[i])

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
n, W = map(int, sys.stdin.readline().split())

dp = [dict() for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    dp[i][v] = min(w , dp[i].get(v, 10**9+1))
    for vb, wb in dp[i-1].items():
        dp[i][vb] = min(dp[i].get(vb, 10*9+1), wb)
        tmp = min(wb + w, dp[i].get(vb+ v, 10**9+1))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    #print(dp[i])

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

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] != float("inf"):
    if dp[n][j] <= W:
      ans = j
    else:
      print(ans)
      exit()
print(ans)

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
from collections import defaultdict
n, W = map(int, sys.stdin.readline().split())

dp = [defaultdict(lambda:10**9+1) for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    dp[i][v] = min(w , dp[i].get(v, 10**9+1))
    for vb, wb in dp[i-1].items():
        dp[i][vb] = min(dp[i].get(vb, 10*9+1), wb)
        tmp = min(wb + w, dp[i].get(vb+ v, 10**9+1))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    #print(dp[i])

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
from collections import defaultdict
n, W = map(int, sys.stdin.readline().split())

dp = [defaultdict(lambda:10*9+1) for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    dp[i][v] = min(w , dp[i].get(v, 10*9+1))
    for vb, wb in dp[i-1].items():
        dp[i][vb] = min(dp[i].get(vb, 10*9+1), wb)
        tmp = min(wb + w, dp[i].get(vb+ v, 10*9+1))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    #print(dp[i])

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
from collections import defaultdict
n, W = map(int, sys.stdin.readline().split())

dp = [defaultdict(lambda:100001) for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    dp[i][v] = min(w , dp[i].get(v, 100001))
    for vb, wb in dp[i-1].items():
        dp[i][vb] = min(dp[i].get(vb, 100001), wb)
        tmp = min(wb + w, dp[i].get(vb+ v, 100001))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    #print(dp[i])

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
from collections import defaultdict
n, W = map(int, sys.stdin.readline().split())

dp = [defaultdict(lambda:100001) for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    for vb, wb in dp[i-1].items():
        dp[i][vb] = dp[i-1][vb]
        tmp = min(wb + w, dp[i].get(vb+ v, 100001), dp[i-1].get(vb+ v, 100001))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    dp[i][v] = min(dp[i][v], w)

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
from collections import OrderedDict
n, W = map(int, sys.stdin.readline().split())

dp = [OrderedDict() for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    for vb, wb in dp[i-1].items():
        dp[i][vb] = min(dp[i-1][vb], dp[i].get(vb, 100001))
        tmp = min(wb + w, dp[i].get(vb+ v, 100001))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    dp[i][v] = min(dp[i].get(v, 100001), w)

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

Solution code:
#EDP-E
import sys
from collections import defaultdict
n, W = map(int, sys.stdin.readline().split())

dp = [defaultdict(lambda:100001) for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    for vb, wb in dp[i-1].items():
        dp[i][vb] = dp[i-1][vb]
        tmp = min(wb + w, dp[i].get(vb+ v, 100001))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    dp[i][v] = min(dp[i][v], w)

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
stones=[None]*N
for i in range(N):
  w,v=map(int,input().split())
  stones[i]=[w,v]
  
import numpy as np
MAXW=10**9+1
dp=np.full(10**3,MAXW,dtype=int)
dp[0]=0
for i in range(len(stones)):
  w=stones[i][0]
  v=stones[i][1]
  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: Wrong Answer

Solution code:
#EDP-E
import sys
from collections import defaultdict
n, W = map(int, sys.stdin.readline().split())

dp = [defaultdict(lambda:100000001) for _ in range(n)]
w, v = map(int, sys.stdin.readline().split())
dp[0][v] = w

for i in range(1, n):
    w, v = map(int, sys.stdin.readline().split())
    for vb, wb in dp[i-1].items():
        dp[i][vb] = dp[i-1][vb]
        tmp = min(wb + w, dp[i-1].get(vb+ v, 100000001))
        if tmp <=W:
            dp[i][v+ vb] = tmp
    dp[i][v] = min(dp[i-1][v], w)

print(sorted(dp[-1].items())[-1][0])

Submission status: Wrong Answer

Solution code:
import sys

import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



def main():
    N, W = map(int, readline().split())
    WV = np.array([[int(x) for x in readline().split()] for _ in range(N)])
    U = np.sum(WV[:, 1])

    dp = np.full([U+1], U*2, np.int64)
    dp[0] = 0

    for w, v in WV:
        p_dp = dp.copy()
        np.minimum(dp[v:], p_dp[:-v] + w, out=dp[v:])

    # print(max(list(zip(*np.where(dp <= W)))))
    print(max(list(np.where(dp <= W)[0])))


main()

Submission status: Wrong Answer

Solution code:
import sys

import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# 数と価値で配列を作る
# 値は価値の最大値


def main():
    N, W = map(int, readline().split())
    WV = np.array([[int(x) for x in readline().split()] for _ in range(N)])
    U = np.sum(WV[:, 1])

    dp = np.full([U+1], U, np.int64)
    dp[0] = 0

    for w, v in WV:
        p_dp = dp.copy()
        np.minimum(dp[v:], p_dp[:-v] + w, out=dp[v:])

    # print(max(list(zip(*np.where(dp <= W)))))
    print(max(list(np.where(dp <= W)[0])))


main()