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.



Sample input:
3 8
3 30
4 50
5 60


Sample output:
90

Submission status: Time Limit Exceeded

Solution code:
import sys

# inputを高速化する。
input = sys.stdin.readline

# 入力
N, W = map(int, input().split())
lst_weight_value = [list(map(int, input().split())) for _ in range(N)]
# print(lst_weight_value)

# def chmax(a, b):
#     if a >= b:
#         return a
#     else:
#         return b

# 重さwの品物の入れ方で達成できる価値の最大値
dp = [0] * (W + 1)

for i in range(N):
    weight, value = lst_weight_value[i]
    for w in range(W, -1, -1):
        _w = w + weight
        _v = dp[w] + value
        if _w > W:
            continue
        if _v <= dp[_w]:
            continue
        dp[_w] = _v

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

# inputを高速化する。
input = sys.stdin.readline

# 入力
N, W = map(int, input().split())
lst_weight = [0 for _ in range(N)]
lst_value = [0 for _ in range(N)]
for i in range(N):
    lst_weight[i], lst_value[i] = map(int, input().split())

def chmax(a, b):
    if a >= b:
        return a
    else:
        return b

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

for i in range(N):
    for sum_w in range(W+1):
        # i番目の品物を選ぶ
        # print(sum_w - lst_weight[i])
        if sum_w - lst_weight[i] >= 0:
            dp[i+1][sum_w] = chmax(dp[i+1][sum_w], dp[i][sum_w - lst_weight[i]] + lst_value[i])
        # i番目の品物を選ばない
        dp[i+1][sum_w] = chmax(dp[i+1][sum_w], dp[i][sum_w])
        # print(dp)

print(dp[N][W])

Submission status: Time Limit Exceeded

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

dp = [0]  * (W + 1)
items = []
for i in range(N):
  w, v = map(int, input().split())
  items.append((w, v))

for w, v in items:  
  for wk in range(W, w - 1, -1):
    tv = dp[wk - w] + v
    if tv > dp[wk]:
      dp[wk] = tv
res = max(dp)
print(res)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

print(max(dp))

Submission status: Time Limit Exceeded

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

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

print(max(dp))

Submission status: Time Limit Exceeded

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

l_w = []
l_v = []
dp = [[0 for i in range(W + 1)] for j in range(N)]
for i in range(N):
  w, v = map(int, input().split())
  l_w.append(w)
  l_v.append(v)

def knapsack(id, rem_w):
  if rem_w == 0 or id == N:
    return 0
  if dp[id][rem_w] != 0:
    return dp[id][rem_w]
  if l_w[id] > rem_w:
    dp[id][rem_w] = knapsack(id + 1, rem_w)
  elif l_w[id] <= rem_w:
    dp[id][rem_w] = max(knapsack(id + 1, rem_w), l_v[id] + knapsack(id + 1, rem_w - l_w[id]))
  return dp[id][rem_w]
print(knapsack(0, W))

Submission status: Time Limit Exceeded

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

def main():
	N, W = map(int, input().split(" "))
	objs = []
	for _ in range(N):
		w, v = map(int, input().split(" "))
		objs.append((w, v))
		
	DP = [[0 for _ in range(W+2)] for _ in range(N+1)]
	
	for i, val in enumerate(objs):
		w, v = val
		for j in range(1, W+1):
			DP[i+1][j] = max(DP[i+1][j], DP[i][j])
			if j - w >= 0:
				#print("{} {}".format(j+w, i+1))
				DP[i+1][j] = max(DP[i+1][j], DP[i][j-w] + v)
				
	print(DP[N][W])
	
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

def main():
	N, W = map(int, input().split(" "))
	objs = []
	for _ in range(N):
		w, v = map(int, input().split(" "))
		objs.append((w, v))
		
	DP = [[0 for _ in range(W+2)] for _ in range(N+1)]
	
	for i, val in enumerate(objs):
		w, v = val
		for j in range(1, W+1):
			DP[i+1][j] = max(DP[i+1][j], DP[i][j])
			if j - w >= 0:
				#print("{} {}".format(j+w, i+1))
				DP[i+1][j] = max(DP[i+1][j], DP[i][j-w] + v)
				
	ans = 0
	#print(DP)
	for i in range(W+1):
		for j in range(N+1):
			if ans < DP[j][i]:
				ans = DP[j][i]
				
	print(ans)
	
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

n, cap = map(int, input().split())
val =[0]*(cap+1)
for _ in range(n):
    w, v = map(int, input().split())
    for wk in range(cap,w-1,-1):
        nv=val[wk-w]+v
        if val[wk]<nv:
            val[wk]=nv
print(val[-1])

Submission status: Time Limit Exceeded

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

n, cap = map(int, input().split())
val =[0]*(cap+1)
for _ in range(n):
    w, v = map(int, input().split())
    for wk in range(cap,w-1,-1):
        val[wk]=max(val[wk],val[wk-w]+v)
print(val[-1])

Submission status: Time Limit Exceeded

Solution code:
n, cap = map(int, input().split())
val =[0]*(cap+1)
for _ in range(n):
    w, v = map(int, input().split())
    for wk in range(cap,w-1,-1):
        val[wk]=max(val[wk],val[wk-w]+v)
print(val[-1])

Submission status: Time Limit Exceeded

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

n, cap = map(int, input().split())
v_w = {0: 0}
for _ in range(n):
    w, v = map(int, input().split())
    for wk, vk in sorted(v_w.items()):
        nw = w + wk
        if nw > cap: break
        if nw in v_w:
            v_w[nw] = max(v_w[nw], v + vk)
        else:
            v_w[nw] = v + vk
print(max(v_w.values()))

Submission status: Time Limit Exceeded

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

n, cap = map(int, input().split())
v_w = {0: 0}
for _ in range(n):
    w, v = map(int, input().split())
    v_w2 = v_w.copy()
    for wk, vk in v_w2.items():
        nw = w + wk
        if nw > cap: continue
        if nw in v_w2:
            v_w[nw] = max(v_w[nw], v + vk)
        else:
            v_w[nw] = v + vk
print(max(v_w.values()))

Submission status: Time Limit Exceeded

Solution code:
n, cap = map(int, input().split())
v_w = {0: 0}
for _ in range(n):
    w, v = map(int, input().split())
    v_w2 = v_w.copy()
    for wk, vk in v_w2.items():
        nw = w + wk
        if nw > cap: continue
        if nw in v_w2:
            v_w[nw] = max(v_w[nw], v + vk)
        else:
            v_w[nw] = v + vk
print(max(v_w.values()))

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
value=[[0 for _ in range(w+1)]for _ in range(n+1)]
for i in range(n):
    for j in range(w+1):
        lastw=j-wv[i][0]
        if lastw>=0:
            value[i+1][j]=max(value[i][j],value[i][lastw]+wv[i][1])
        else:
            value[i+1][j]=value[i][j]

print(value[n][w])

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
# wv.sort(key=lambda x:x[0])
# wv.sort(key=lambda x:-x[1])
# value[i][j] i番目までで重さがj以下となるように選んだときの価値の最大値
value=[[0 for _ in range(w+1)]for _ in range(n+1)]
for i in range(n):
    for j in range(w+1):
        if j-wv[i][0]>=0:
            value[i+1][j]=max(value[i][j],value[i][j-wv[i][0]]+wv[i][1])
        value[i+1][j]=max(value[i+1][j],value[i][j])

print(value[n][w])

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
wvs = [tuple(map(int, input().split())) for i in range(N)]
dp = [[0 for j in range(W+1)] for i in range(N)]

for i, wv in enumerate(wvs):
    for j in range(W+1):
        if i == 0:
            if wv[0] < j+1:
                dp[0][j] = wv[1]
        elif wv[0] < j+1:
            dp[i][j] = max(dp[i-1][j-wv[0]] + wv[1], dp[i-1][j])
        else:
            dp[i][j] = dp[i-1][j]

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

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
wvs = [tuple(map(int, input().split())) for i in range(N)]
dp = [[0 for j in range(W+1)] for i in range(N)]

for i, wv in enumerate(wvs):
    for j in range(W+1):
        if i == 0:
            if wv[0] < j+1:
                dp[0][j] = wv[1]
        elif wv[0] < j+1:
            dp[i][j] = max(dp[i-1][j-wv[0]] + wv[1], dp[i-1][j])
        else:
            dp[i][j] = dp[i-1][j]

print(max(dp[-1]))

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N, W = map(int, input().split())
    weight = [0 for i in range(N)]
    value = [0 for i in range(N)]
    for i in range(N):
        weight[i], value[i] = map(int, input().split())
    dp = [0 for w in range(W + 1)]

    for i in range(N):
        # wi = weight[i]
        # vi = value[i]
        for w in range(W, -1, -1):
            _w = w + weight[i]
            # _v = dp[w] + value[i]
            if _w > W: continue
            # if _v <= dp[_w]: continue
            dp[_w] = max(dp[_w], dp[w] + value[i])
    return dp[-1]

print(solve())

Submission status: Time Limit Exceeded

Solution code:
n, w_limit = map(int, input().split())
dp = [[0 for _ in range(w_limit+1)] for __ in range(n+1)]
for i in range(n):
  w, v = map(int, input().split())
  for w_sum in range(w_limit+1):
    if w_sum >= w:
      dp[i+1][w_sum] = dp[i][w_sum - w] + v
    dp[i+1][w_sum] = max(dp[i+1][w_sum], dp[i][w_sum])

print(dp[n][w_limit])

Submission status: Time Limit Exceeded

Solution code:
def solve():
  N, W = map(int, input().split())
  weight = [0 for i in range(N)]
  value = [0 for i in range(N)]
  for i in range(N):
    weight[i], value[i] = map(int, input().split())
  dp = [[0 for w in range(100100)] for i in range(110)]

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

  return dp[N][W]

print(solve())

Submission status: Time Limit Exceeded

Solution code:
n, w_total = [int(i) for i in input().split()]
w = [0] * n
v = [0] * n
dp = [[0 for i in range(w_total + 1)] for j in range(n)]

dp[0] = [0] * (w_total + 1)

for i in range(n):
    w, v = [int(j) for j in input().split()]

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

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    dp = [[0] * (W + 1) for _ in range(N+1)]
    for i in range(N-1, -1, -1):
        for j in range(W+1):
            if j < data[i][0]:
                dp[i][j] = dp[i+1][j]
            else:
                dp[i][j] = max(dp[i+1][j], dp[i+1][j - data[i][0]] + data[i][1])
    print(dp[0][W])
    pass

main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

N, W = map(int, input().split())
wv = [[0, 0]] + [[int(_) for _ in input().split()] for _ in range(N)]
dp = [[0]*(2*W) for _ in range(2*N)]

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

print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,input().split())
lis=[list(map(int,input().split())) for _ in range(n)]
dp=[[0 for i in range(w+1)] for j in range(n+1)]
for i in range(n):
    for j in range(w+1):
        if w-lis[i][0]>=0 and j-lis[i][0]>=0:
            dp[i+1][j]=max(dp[i][j],dp[i][j-lis[i][0]]+lis[i][1])
        else:
            dp[i+1][j]=dp[i][j]
print(max(dp[n]))

Submission status: Time Limit Exceeded

Solution code:
n,w=list(map(int,input().split()))
k=[]
for i in range(n):
    k.append(list(map(int,input().split())))
vn=max([k[i][1] for i in range(n)])*n
dp=[[0]+[float("inf") for i in range(vn)] for i in range(n+1)]
for i in range(1,n+1):
    for j in range(1,vn+1):
        if j<=k[i-1][1]:
            dp[i][j]=min(dp[i-1][j],k[i-1][0])
        else:
            dp[i][j]=min(dp[i-1][j],k[i-1][0]+dp[i-1][j-k[i-1][1]])
for i in range(vn,-1,-1):
    if dp[n][i]<=w:
        print(i)
        break

Submission status: Time Limit Exceeded

Solution code:
n,W = map(int, input().split())
l = [dict() for i in range(n+1)]
l[0][0] = 0
for i in range(1,n+1):
  w,v = map(int, input().split())
  l[i] = l[i-1].copy()
  l[i][w] = max(l[i].get(w,0),v)
  for x in l[i-1]:
    if x+w <= W:
      l[i][x+w] = max(l[i].get(x+w,0),l[i-1][x] + v)
print(max(l[n].values()))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
 # -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

def main():
	N, W = map(int, input().split(" "))
	#goods = []
	#for _ in range(N):
		#w, v = map(int, input().split(" "))
		#goods.append((w, v))
		
	DP = [[0] * (W+2) for _ in range(N+1)]
	for i in range(N):
		#w, v = goods[i]
		w, v = map(int, input().split(" "))
		for w_sum in range(W+1):
			DP[i+1][w_sum] = max(DP[i+1][w_sum], DP[i][w_sum])
			if w_sum - w >= 0:
				DP[i+1][w_sum] = max(DP[i+1][w_sum], DP[i][w_sum - w] + v)
		
	print(DP[N][W])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
 # -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

def main():
	N, W = map(int, input().split(" "))
	goods = []
	for _ in range(N):
		w, v = map(int, input().split(" "))
		goods.append((w, v))
		
	DP = [[0] * (W+2) for _ in range(N+1)]
	for i in range(N):
		w, v = goods[i]
		for w_sum in range(W+1):
			DP[i+1][w_sum] = max(DP[i+1][w_sum], DP[i][w_sum])
			if w_sum - w >= 0:
				DP[i+1][w_sum] = max(DP[i+1][w_sum], DP[i][w_sum - w] + v)
		
	print(DP[N][W])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

def main():
	N, W = map(int, input().split(" "))
	goods = []
	for _ in range(N):
		w, v = map(int, input().split(" "))
		goods.append((w, v))
		
	DP = [[0 for _ in range(W+2)] for _ in range(N+1)]
	for i in range(N):
		w, v = goods[i]
		for w_sum in range(W+1):
			DP[i+1][w_sum] = max(DP[i+1][w_sum], DP[i][w_sum])
			if w_sum - w >= 0:
				DP[i+1][w_sum] = max(DP[i+1][w_sum], DP[i][w_sum - w] + v)
		
	print(DP[N][W])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

def main():
	N, W = map(int, input().split(" "))
	goods = []
	for _ in range(N):
		w, v = map(int, input().split(" "))
		goods.append((w, v))
		
	ans = 0
	for i in range(2 ** N):
		val = 0
		wal = 0
		flg = True
		for j in range(N):
			if i >> j & 1 == 1:
				w, v = goods[j]
				val += v
				wal += w
				if W < wal:
					flg = False
					break
		if flg == False:
			continue
		if ans < val:
			ans = val
		
	print(ans)
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

def main():
	N, W = map(int, input().split(" "))
	goods = []
	for _ in range(N):
		w, v = map(int, input().split(" "))
		goods.append((w, v))
		
	ans = 0
	for i in range(2 ** N):
		val = 0
		wal = 0
		for j in range(N):
			if i >> j & 1 == 1:
				w, v = goods[j]
				val += v
				wal += w
		if W < wal:
			continue
		if ans < val:
			ans = val
		
	print(ans)
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

dp = [[-1 for _ in range(max_w + 1)] for _ in range(N + 1)]
def dfs(i, w):
    """i以降の商品からw以下となるように選んだ時の価値の総和"""
    if dp[i][w] >= 0:
        return dp[i][w]
    if i == N:
        ret = 0
    elif w < W[i]:
        ret = dfs(i + 1, w)
    else:
        ret = max(dfs(i + 1, w), dfs(i + 1, w - W[i]) + V[i])
    dp[i][w] = ret
    return ret

if __name__ == '__main__':
    print(dfs(0, max_w))

Submission status: Time Limit Exceeded

Solution code:
N, W = [int(i) for i in input().split()]
data_ls = list()
for i in range(N):
    data_ls.append([int(i) for i in input().split()])
memo = list()
for i in range(N + 1):
    memo.append([0] * (W + 1))
for i in range(N):
    w, v = data_ls[i]
    for j in range(W+1):
        if j >= w:
            memo[i + 1][j] = max(memo[i][j], memo[i][j-w] + v, memo[i+1][j-1])
        else:
            memo[i + 1][j] = max(memo[i][j], memo[i+1][j-1])
print(memo[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
n, w = map(int, input().split(' '))
dp = [0] * (w + 1)
for i in range(n):
    weight, value = map(int, input().split(' '))
    for weight_already in range(w - weight, -1, -1):
        dp[weight_already + weight] = max(dp[weight_already + weight], dp[weight_already] + value)
    
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
p = []
for it in range(N):
    p.append(list(map(int,input().split())))
    
dp = [[0 for i in range(W+1)] for j in range(N+1)]
i = 0
for w, v in p:
    i += 1
    for w_limit in range(1, W+1):
        if w_limit < w:
            dp[i][w_limit]=dp[i-1][w_limit]
            continue
        if (dp[i-1][w_limit-w] + v) > dp[i-1][w_limit]:
            dp[i][w_limit] = dp[i-1][w_limit-w] + v
        else:
            dp[i][w_limit]=dp[i-1][w_limit]
print(max(max(dp)))

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
p = []
for it in range(N):
    p.append(list(map(int,input().split())))
    
import numpy as np
dp = np.zeros((N+1,W+1), dtype=np.int64)

i = 0
for w, v in p:
    i += 1
    for w_limit in range(1, W+1):
        if w_limit < w:
            dp[i,w_limit]=dp[i-1,w_limit]
            continue
        if (dp[i-1, w_limit-w] + v) > dp[i-1, w_limit]:
            dp[i, w_limit] = dp[i-1, w_limit-w] + v
        else:
            dp[i,w_limit]=dp[i-1,w_limit]
print(dp.max())

Submission status: Time Limit Exceeded

Solution code:
#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n, W = map(int, input().split())
    w, v = [], []

    for _ in range(n):
        wi, vi = map(int, input().split())
        w.append(wi)
        v.append(vi)

    dp = []
    for _ in range(W + 1):
        dp.append([])
        for _ in range(n):
            dp[-1].append(0)

    ans = 0
    for weight in range(0, W + 1):
        for i in range(n):
            res = 0
            if i - 1 >= 0:
                res = max(res, dp[weight][i - 1])
            if weight >= w[i]:
                res = max(res, (dp[weight - w[i]][i - 1] if i - 1 >= 0 else 0) + v[i])
            dp[weight][i] = res
            ans = max(ans, res)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,W=map(int,input().split())
B=[list(map(int,input().split())) for i in range(N)]

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


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

Submission status: Time Limit Exceeded

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

def d_knapsack1(W,wt,val,n): 
    dp=[[0 for x in xrange(W+1)] for x in xrange(n+1)]   
    for i in xrange(n+1): 
        for w in xrange(W+1): 
            if i==0 or w==0: 
                dp[i][w] = 0
            elif wt[i-1]<=w:
                dp[i][w]=max(val[i-1]+dp[i-1][w-wt[i-1]],dp[i-1][w])
            else:
                dp[i][w]=dp[i-1][w]
    print dp[n][W] 

n,w=map(int,raw_input().split())
wt,val=[],[]
for i in xrange(n):
    wei,va=map(int,raw_input().split())
    wt.append(wei)
    val.append(va)
d_knapsack1(w,wt,val,n)

Submission status: Time Limit Exceeded

Solution code:


def Knapsack(num_items, cap):
    if (num_items, cap) in already.keys():
        return already[(num_items, cap)]
    
    if num_items <= 0 or cap <= 0:
        return 0
    
    if w[num_items - 1] > cap:
        already[(num_items, cap)] = Knapsack(num_items - 1, cap)
        return already[(num_items, cap)]
    curr_value, curr_wt = v[num_items - 1], w[num_items - 1]
    
    add = 0
    if (num_items - 1, cap - curr_wt) in already.keys():
        add = already[(num_items - 1, cap - curr_wt)]
    else:
        add = Knapsack(num_items - 1, cap - curr_wt)
        already[(num_items - 1, cap - curr_wt)] = add
        
    
    take =  curr_value + add
    
    if (num_items - 1, cap) in already.keys():
        add = already[(num_items - 1, cap)]
    else:
        add = Knapsack(num_items - 1, cap)
        already[(num_items - 1, cap)] = add
        
    dont = add 
    already[(num_items, cap)] = max(take, dont)
    return  already[(num_items, cap)]

already = dict()
num_items, cap = map(int, input().split())
w, v = [], []
for _ in range(num_items):
    x, y = map(int, input().split())
    w.append(x)
    v.append(y)
print(Knapsack(num_items, cap))

Submission status: Time Limit Exceeded

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

def d_knapsack1(W,wt,val,n): 
    dp=[[0 for x in xrange(W+1)] for x in xrange(n+1)]   
    for i in xrange(n+1): 
        for w in xrange(W+1): 
            if i==0 or w==0: 
                dp[i][w]=0
            elif wt[i-1]>w:
                dp[i][w]=dp[i-1][w]
            else:
                dp[i][w]=max(val[i-1]+dp[i-1][w-wt[i-1]],dp[i-1][w])
    print dp[n][W] 

n,w=map(int,raw_input().split())
wt,val=[],[]
for i in xrange(n):
    wei,va=map(int,raw_input().split())
    wt.append(wei)
    val.append(va)
d_knapsack1(w,wt,val,n)

Submission status: Time Limit Exceeded

Solution code:


def Knapsack(num_items, cap):
    if (num_items, cap) in already.keys():
        return already[(num_items, cap)]
    
    if num_items <= 0 or cap <= 0:
        return 0
    
    if w[num_items - 1] > cap:
        already[(num_items, cap)] = Knapsack(num_items - 1, cap)
        return already[(num_items, cap)]
    curr_value, curr_wt = v[num_items - 1], w[num_items - 1]
    
    take =  curr_value + Knapsack(num_items - 1, cap - curr_wt)
    dont = Knapsack(num_items - 1, cap)
    already[(num_items, cap)] = max(take, dont)
    return  already[(num_items, cap)]

already = dict()
num_items, cap = map(int, input().split())
w, v = [], []
for _ in range(num_items):
    x, y = map(int, input().split())
    w.append(x)
    v.append(y)
print(Knapsack(num_items, cap))

Submission status: Time Limit Exceeded

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

import sys
input = sys.stdin.readline

N, W = [int(x) for x in input().split()]
w = [0] * N
v = [0] * N

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

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

print(max(dp[N]))

Submission status: Time Limit Exceeded

Solution code:


def Knapsack(num_items, cap):
    if (num_items, cap) in already.keys():
        return already[(num_items, cap)]
    
    if num_items <= 0 or cap <= 0:
        return 0
    
    if w[num_items - 1] > cap:
        return Knapsack(num_items - 1, cap)
    curr_value, curr_wt = v[num_items - 1], w[num_items - 1]
    
    take =  curr_value + Knapsack(num_items - 1, cap - curr_wt)
    dont = Knapsack(num_items - 1, cap)
    already[(num_items, cap)] = max(take, dont)
    return  already[(num_items, cap)]

already = dict()
num_items, cap = map(int, input().split())
w, v = [], []
for _ in range(num_items):
    x, y = map(int, input().split())
    w.append(x)
    v.append(y)
print(Knapsack(num_items, cap))

Submission status: Time Limit Exceeded

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
#
import sys


N,W = map(int, sys.stdin.readline().rstrip().split())
wv = []
for _ in range(N):
  wv.append(list(map(int, sys.stdin.readline().rstrip().split())))

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

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

print(max(dp[N]))

Submission status: Time Limit Exceeded

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

N, W = [int(x) for x in input().split()]
w = [0] * N
v = [0] * N

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

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

print(max(dp[N]))

Submission status: Time Limit Exceeded

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

N, W = [int(x) for x in input().split()]
w = [0] * N
v = [0] * N

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

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

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

print(max(dp[N]))

Submission status: Time Limit Exceeded

Solution code:
n,w = map(int, input().split())
max_value = [[0]*(w+1) for _ in range(n+1)]
for i in range(1, n+1):
    weight, value = map(int, input().split())
    for j in range(1, weight):
        max_value[i][j] = max_value[i-1][j]
    for j in range(weight, w+1):
        max_value[i][j] = max(max_value[i-1][j], max_value[i-1][j-weight] + value)

print(max_value[n][w])

Submission status: Time Limit Exceeded

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

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

max_value = [[0]*(w+1) for _ in range(n+1)]
for i in range(1, n+1):
    weight, value = item[i-1]
    for j in range(1, weight):
        max_value[i][j] = max(max_value[i-1][j], max_value[i][j-1])
    for j in range(weight, w+1):
        max_value[i][j] = max(max_value[i-1][j], max_value[i][j-1], max_value[i-1][j-weight] + value)

print(max_value[n][w])

Submission status: Time Limit Exceeded

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

item = []
for i in range(n):
    weight, value = map(int, input().split())
    item.append((weight, value))
item.sort()

max_value = [[0]*(w+1) for _ in range(n+1)]
for i in range(1, n+1):
    weight, value = item[i-1]
    for j in range(1, w+1):
        max_value[i][j] = max(max_value[i-1][j], max_value[i][j-1], max_value[i-1][j-weight] + value if j-weight >= 0 else 0)


print(max_value[n][w])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, W = map(int, input().split())
w = []
v = []
for _ in range(N):
	a, b = map(int, input().split())
	w.append(a)
	v.append(b)
dp = np.zeros((N+1, W+1))
# print(dp)
for i in range(N):
	for sum_w in range(W+1):
		if sum_w < w[i]:
			dp[i+1, sum_w] = dp[i, sum_w]
		else:
			dp[i+1, sum_w] = np.maximum(dp[i, sum_w], dp[i, sum_w-w[i]]+v[i])
print(int(dp[N, W]))

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
w = []
v = []
for _ in range(N):
	a, b = map(int, input().split())
	w.append(a)
	v.append(b)
dp = [[0]*(W+1) for _ in range(N+1)]

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

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
w = []
v = []
for _ in range(N):
	a, b = map(int, input().split())
	w.append(a)
	v.append(b)
dp = [[0]*(W+1) for _ in range(N+1)]
# print(abc)
for i in range(N):
	for sum_w in range(0,W+1):
		if sum_w - w[i] >= 0:
			dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w-w[i]]+v[i])
		dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w])
print(dp[N][W])