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:
n, w = map(int, input().split())

weights = [0]*n
values = [0]*n

for i in range(n):
    weights[i], values[i] = map(int, input().split())

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

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

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint
import numpy as np
n,w=map(int, input().split())
wv=[list(map(int,input().split()))  for i in  range(n)]

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

wv.sort(key=lambda x:x[1])
#print(wv)
for i in range(n):
  #print("i={}".format(i))    
  for wsum in range(1,w+1):
    #print("重さ：{}".format(wsum))
    if wsum >= wv[i][0]:
        if dp[i+1][wsum] < dp[i][wsum-wv[i][0]]+wv[i][1]:
          #print("選ぶ, (重さ：{}, 価値：{})".format(wv[i][0], wv[i][1]))
          dp[i+1][wsum] = dp[i][wsum-wv[i][0]]+wv[i][1]
    else:
      dp[i+1][wsum] = dp[i][wsum]
    #pprint(dp)


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

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint
import numpy as np
n,w=map(int, input().split())
wv=[list(map(int,input().split()))  for i in  range(n)]

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


for i in range(n):
      #print("重さ：{}".format(wsum))
  for wsum in range(1,w+1):
    #print("i={}".format(i))
    if wsum >= wv[i][0]:
        #print("選ぶ, (重さ：{}, 価値：{})".format(wl[i], vl[i]))
        dp[i][wsum] = max(dp[i-1][wsum], dp[i-1][wsum-wv[i][0]]+wv[i][1])
    else:
      dp[i][wsum] = dp[i-1][wsum]
    #pprint(dp)

dp=np.array(dp)
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint
import numpy as np
n,w=map(int, input().split())
wv=[list(map(int,input().split()))  for i in  range(n)]

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


for i in range(n):
      #print("重さ：{}".format(wsum))
  for wsum in range(1,w+1):
    #print("i={}".format(i))
    if wsum >= wv[i][0]:
        #print("選ぶ, (重さ：{}, 価値：{})".format(wl[i], vl[i]))
        dp[i+1][wsum] = max(dp[i+1][wsum], dp[i][wsum-wv[i][0]]+wv[i][1])
    else:
      dp[i+1][wsum] = dp[i][wsum]
    #pprint(dp)

dp=np.array(dp)
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint
import numpy as np
n,w=map(int, input().split())
wl,vl=[],[]
wsum=0
for i in range(n):
  wn,v=map(int, input().split())
  wl.append(wn)
  vl.append(v)
  wsum+=w
if wsum<w:
  w=wsum

dp=[[0]*(w+1) for i in range(n+1)]
wmin=min(wl)

for wsum in range(wmin,w+1):
  #print("重さ：{}".format(wsum))
  for i in range(n):
    #print("i={}".format(i))
    if wsum-wl[i]>=0:
      if dp[i+1][wsum] < dp[i][wsum-wl[i]]+vl[i]:
        #print("選ぶ, (重さ：{}, 価値：{})".format(wl[i], vl[i]))
        dp[i+1][wsum] = dp[i][wsum-wl[i]]+vl[i]
    else:
      dp[i+1][wsum] = dp[i][wsum]
    #pprint(dp)
dp=np.array(dp)
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint
import numpy as np
n,w=map(int, input().split())
wv=[list(map(int,input().split()))  for i in  range(n)]

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

for wsum in range(1,w+1):
  #print("重さ：{}".format(wsum))
  for i in range(n):
    #print("i={}".format(i))
    if wsum >= wv[i][0]:
        #print("選ぶ, (重さ：{}, 価値：{})".format(wl[i], vl[i]))
        dp[i+1][wsum] = max(dp[i+1][wsum], dp[i][wsum-wv[i][0]]+wv[i][1])
    else:
      dp[i+1][wsum] = dp[i][wsum]
    #pprint(dp)
dp=np.array(dp)
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint
import numpy as np
n,w=map(int, input().split())
wl,vl=[],[]
wsum=0
for i in range(n):
  wn,v=map(int, input().split())
  wl.append(wn)
  vl.append(v)
  wsum+=w
if wsum<w:
  w=wsum

dp=[[0]*(w+1) for i in range(n+1)]
wmin=min(wl)

for wsum in range(wmin,w+1):
  #print("重さ：{}".format(wsum))
  for i in range(n):
    #print("i={}".format(i))
    if wsum >= wl[i]:
        #print("選ぶ, (重さ：{}, 価値：{})".format(wl[i], vl[i]))
        dp[i+1][wsum] = max(dp[i+1][wsum], dp[i][wsum-wl[i]]+vl[i])
    else:
      dp[i+1][wsum] = dp[i][wsum]
    #pprint(dp)
dp=np.array(dp)
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint
import numpy as np
n,w=map(int, input().split())
wl,vl=[],[]
wsum=0
for i in range(n):
  wn,v=map(int, input().split())
  wl.append(wn)
  vl.append(v)
  wsum+=w
if wsum<w:
  w=wsum

dp=[[0]*(w+1) for i in range(n+1)]
wmin=min(wl)

for wsum in range(wmin,w+1):
  #print("重さ：{}".format(wsum))
  for i in range(n):
    #print("i={}".format(i))
    if wsum-wl[i]>=0:
      if dp[i+1][wsum] < dp[i][wsum-wl[i]]+vl[i]:
        #print("選ぶ, (重さ：{}, 価値：{})".format(wl[i], vl[i]))
        dp[i+1][wsum] = dp[i][wsum-wl[i]]+vl[i]
    else:
      dp[i+1][wsum] = dp[i][wsum]
    #pprint(dp)
dp=np.array(dp)
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
N,W = map(int, input().split())
L = [0]*(W+1)
for i in range (N):
  X,Y = map(int, input().split())
  for j in range (W,X-1,-1):
    L[j] = max(L[j], L[j-X] + Y)
print(max(L))

Submission status: Time Limit Exceeded

Solution code:

#Atcoder#D - Knapsack 1
#{TLE -4 passed} due to python 
n,cap = list(map(int,input().split()))
dp = [0]*(cap+1)
for _ in range(n):    
    weight,value = list(map(int,input().split()))
    for j in range(cap-weight,-1,-1):
        dp[j+weight] = max(dp[j+weight],dp[j]+value)    
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:

#C - Vacation

n,cap = list(map(int,input().split()))
dp = [0]*(cap+1)
for _ in range(n):    
    weight,value = list(map(int,input().split()))
    new_dp = [0]*(cap+1)
    for j in range(1,cap+1):
        if weight <= j:
            new_dp[j] = max( value+dp[ j - weight ] , dp[j])
        else:
            new_dp[j] =  dp[j]
    dp = new_dp
    
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:

#C - Vacation

def solution(items,cap):
    n = len(items)
    dp = [0]*(cap+1) # 0 items
    for i in range(n):
        weight,value = items[i]
        new_dp = [0]*(cap+1)
        
        for j in range(1,cap+1):
            if weight <= j:
                new_dp[j] = max( value+dp[ j - weight ] , dp[j])
            else:
                new_dp[j] =  dp[j]
        dp = new_dp
    
    return dp[-1]

n,k = list(map(int,input().split()))
items = []
for _ in range(n):    
    item = list(map(int,input().split()))
    items.append(item)
res = solution(items,k)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


N, W = map(int, input().split())
#x = [tuple(map(int, input().split())) for _ in range(N)]

x = [None] * N
for i in range(N):
    x[i] = tuple(map(int, input().split()))
    
dp = [[0] * (W + 1) for _ in range(N + 1)]

for i, (w, v) in enumerate(x, 1):
    dpi = dp[i]
    dpim = dp[i-1]    
    for j in range(1, W + 1):
        if w <= j:
            a = dpim[j - w] + v
            if a > dpim[j]:
                dpi[j] = a
            else:
                dpi[j] = dpim[j]
        else:
            dpi[j] = dpim[j]

print(dp[-1][W])

Submission status: Time Limit Exceeded

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

for i, (w, v) in enumerate(x, 1):
    dpi = dp[i]
    dpim = dp[i-1]    
    for j in range(1, W + 1):
        if w <= j:
            a = dpim[j - w] + v
            if a > dpim[j]:
                dpi[j] = a
            else:
                dpi[j] = dpim[j]
        else:
            dpi[j] = dpim[j]

print(dp[-1][W])

Submission status: Time Limit Exceeded

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

for i, (w, v) in enumerate(x, 1):
    dpi = dp[i]
    dpim = dp[i-1]    
    for j in range(1, W + 1):
        if w <= j:
            dpi[j] = max(dpim[j], dpim[j - w] + v)
        else:
            dpi[j] = dpim[j]

print(dp[-1][W])

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def main():
    N, W = map(int, input().split())
    x = [tuple(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):
            dpi = dp[i]
            dpim = dp[i-1]
            if x[i - 1][0] <= j:
                dpi[j] = max(dpim[j], dpim[j - x[i - 1][0]] + x[i - 1][1])
            else:
                dpi[j] = dpim[j]

    print(dp[-1][W])

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def main():
    N, W = map(int, input().split())
    x = [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):
            dpi = dp[i]
            dpim = dp[i-1]
            if x[i - 1][0] <= j:
                dpi[j] = max(dpim[j], dpim[j - x[i - 1][0]] + x[i - 1][1])
            else:
                dpi[j] = dpim[j]

    print(dp[-1][W])

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#import math
import numpy as np
#= int(input())
#= input()
N, W = map(int, input().split())
#= list(map(int, input().split()))
#= [input(), input()]

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

dp = [[0] * (W + 1) for _ in range(N + 1)]
for i, (w, v) in enumerate(x, start=1):
    dp_i = dp[i]
    dp_im = dp[i - 1]
    for j in range(1, W + 1):
        if j < w:
            dp_i[j] = dp_im[j]
        else:
            a = dp_im[j - w] + v
            b = dp_im[j]
            if a > b:
                dp_i[j] = a
            else:
                dp_i[j] = b
 
ans = dp[-1][-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
#import math
import numpy as np
#= int(input())
#= input()
N, W = map(int, input().split())
#= list(map(int, input().split()))
#= [input(), input()]

x = [list(map(int, sys.stdin.readline().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):
        dpi = dp[i]
        dpim = dp[i-1]
        if x[i - 1][0] <= j:
            dpi[j] = max(dpim[j], dpim[j - x[i - 1][0]] + x[i - 1][1])
        else:
            dpi[j] = dpim[j]

print(dp[-1][W])

Submission status: Time Limit Exceeded

Solution code:
#import
#import math
import numpy as np
#= int(input())
#= input()
N, W = map(int, input().split())
#= list(map(int, input().split()))
#= [input(), input()]

x = [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):
        dpi = dp[i]
        dpim = dp[i-1]
        if x[i - 1][0] <= j:
            dpi[j] = max(dpim[j], dpim[j - x[i - 1][0]] + x[i - 1][1])
        else:
            dpi[j] = dpim[j]

print(dp[-1][W])

Submission status: Time Limit Exceeded

Solution code:
#import
#import math
import numpy as np
#= int(input())
#= input()
N, W = map(int, input().split())
#= list(map(int, input().split()))
#= [input(), input()]

weight = [0] * N
value = [0] * N

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



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

for i in range(1, N + 1):
    for j in range(1, W + 1):

        if weight[i - 1] <= j:
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1])
        else:
            dp[i][j] = dp[i - 1][j]

print(dp[-1][W])

Submission status: Time Limit Exceeded

Solution code:
#import
#import math
import numpy as np
#= int(input())
#= input()
N, W = map(int, input().split())
#= list(map(int, input().split()))
#= [input(), input()]

x = [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 x[i - 1][0] <= j:
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - x[i - 1][0]] + x[i - 1][1])
        else:
            dp[i][j] = dp[i - 1][j]

print(dp[-1][W])

Submission status: Time Limit Exceeded

Solution code:
#import
#import math
import numpy as np
#= int(input())
#= input()
N, W = map(int, input().split())
#= list(map(int, input().split()))
#= [input(), input()]

x = [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):
        dp[i][j] = dp[i - 1][j]
        if x[i - 1][0] <= j:
            dp[i][j] = max(dp[i][j], dp[i-1][j-x[i-1][0]] + x[i-1][1])

print(dp[-1][W])

Submission status: Time Limit Exceeded

Solution code:
#import
#import math
import numpy as np
#= int(input())
#= input()
N, W = map(int, input().split())
#= list(map(int, input().split()))
#= [input(), input()]

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

dp = np.zeros((N+1, W+1), int)

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

print(dp[-1][W])

Submission status: Time Limit Exceeded

Solution code:
#import
#import math
#import numpy as np
#= int(input())
#= input()
N, W = map(int, input().split())
#= list(map(int, input().split()))
#= [input(), input()]

x = [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):
        dp[i][j] = dp[i - 1][j]
        if i != 0 and x[i - 1][0] <= j:
            dp[i][j] = max(dp[i][j], dp[i-1][j-x[i-1][0]] + x[i-1][1])

print(dp[-1][W])

Submission status: Time Limit Exceeded

Solution code:
n,w=[int(i) for i in input().split()]
dp=[0 for i in range(w+1)]
for i in range(n):
    weight,val=[int(i) for i in input().split()]

    for j in range(w-weight,-1,-1):
        dp[j+weight]=max(dp[j+weight],dp[j]+val)

ans=0
print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(n)]
dp=[[0 for _ in range(w+1)] for _ in range(n+1)]

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

Submission status: Time Limit Exceeded

Solution code:
(N, W), *D = [list(map(int, o.split())) for o in open(0)]
w, v = 0, 1
dp = [[0]*-~W for _ in [0]*-~N]
for i in range(N):
  for j in range(W + 1):
    dp[i + 1][j] = max(dp[i][j], (dp[i][j - D[i][w]] + D[i][v])*(j >= D[i][w]))
print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
n, W = map(int, input().split())
w, v = [], []
for i in range(n):
    w_, v_ = map(int, input().split())
    w.append(w_)
    v.append(v_)
dp = [[0] * (W + 1) for i in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, W + 1):
        if j < w[i - 1]:
            dp[i][j] = dp[i - 1][j]
        else:
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1])
print(dp[n][W])

Submission status: Time Limit Exceeded

Solution code:
n, maxW = map(int, input().split())
W, V = [], []
for _ in range(n):
    w, v = map(int, input().split())
    W.append(w)
    V.append(v)
dp = [[0] * (maxW+1) for _ in range(n+1)]
for i in range(1, n+1):
    for j in range(maxW, -1, -1):
        if j + W[i-1] > maxW:
            dp[i][j] = dp[i-1][j]
            continue
        dp[i][j] = max(dp[i-1][j + W[i-1]] + V[i-1], dp[i-1][j])
print(dp[n][0])

Submission status: Time Limit Exceeded

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

dp=[]
for y in range(n+1):
    l=[]
    if y==0:dp.append([0]*(w+1));continue
    for x in range(w+1):
        if x==0:l.append(0);continue
        
        now_weight=x
        up_value=dp[-1][now_weight]

        index_weight=a[y-1][0]
        index_value=a[y-1][1]

        blank_weight=max(0,now_weight-index_weight)
        blank_max_value=dp[-1][blank_weight]
        

        t_value=up_value
        if now_weight>=index_weight:t_value=max(blank_max_value+index_value,up_value) 
        
        l.append(t_value)
        
    dp.append(l)

print(dp[n][w])

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

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

for i in range(N):
    for sum_w in range(1,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(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
import numba as nb
n, w = input("").strip().split(' ')
n, w = int(n), int(w)
weights = [0]*n
values = [0]*n
for i in range(0, n):
  wi, ni = input("").strip().split(' ')
  values[i] = int(ni)
  weights[i] = int(wi)

#mem = [{} for i in range(0, n)]
@nb.jit
def best(i, rem_weight):
  #if rem_weight in mem[i]:
  #  return mem[i][rem_weight]
  if rem_weight <= 0:
  	return 0
  if i < 0:
   	return 0
  result = 0
  if weights[i] <= rem_weight:
    result = max(result,
                 values[i] + best(i-1, rem_weight - weights[i]))
  result = max(result,
               best(i-1, rem_weight))
  #mem[i][rem_weight] = result
  return result
print(best(n-1, w))

Submission status: Time Limit Exceeded

Solution code:
import numba as nb
n, w = input("").strip().split(' ')
n, w = int(n), int(w)
weights = [0]*n
values = [0]*n
for i in range(0, n):
  wi, ni = input("").strip().split(' ')
  values[i] = int(ni)
  weights[i] = int(wi)

mem = [{} for i in range(0, n)]
@nb.jit
def best(i, rem_weight):
  if rem_weight in mem[i]:
    return mem[i][rem_weight]
  if rem_weight <= 0:
  	return 0
  if i < 0:
   	return 0
  result = 0
  if weights[i] <= rem_weight:
    result = max(result,
                 values[i] + best(i-1, rem_weight - weights[i]))
  result = max(result,
               best(i-1, rem_weight))
  mem[i][rem_weight] = result
  return result
print(best(n-1, w))

Submission status: Time Limit Exceeded

Solution code:
n, w = input("").strip().split(' ')
n, w = int(n), int(w)
weights = [0]*n
values = [0]*n
for i in range(0, n):
  wi, ni = input("").strip().split(' ')
  values[i] = int(ni)
  weights[i] = int(wi)

mem = [{} for i in range(0, n)]
def best(i, rem_weight):
  if rem_weight in mem[i]:
    return mem[i][rem_weight]
  if rem_weight <= 0:
  	return 0
  if i < 0:
   	return 0
  result = 0
  if weights[i] <= rem_weight:
    result = max(result,
                 values[i] + best(i-1, rem_weight - weights[i]))
  result = max(result,
               best(i-1, rem_weight))
  mem[i][rem_weight] = result
  return result
print(best(n-1, w))

Submission status: Time Limit Exceeded

Solution code:
n, w = input("").strip().split(' ')
n, w = int(n), int(w)
weights = [0]*n
values = [0]*n
for i in range(0, n):
  wi, ni = input("").strip().split(' ')
  values[i] = int(ni)
  weights[i] = int(wi)

mem = {}
def best(i, rem_weight):
  key = (i, rem_weight)
  if key in mem:
    return mem[key]
  if rem_weight <= 0:
  	return 0
  if i < 0:
   	return 0
  result = 0
  if weights[i] <= rem_weight:
    result = max(result,
                 values[i] + best(i-1, rem_weight - weights[i]))
  result = max(result,
               best(i-1, rem_weight))
  mem[key] = result
  return result
print(best(n-1, w))

Submission status: Time Limit Exceeded

Solution code:
n, w = input("").strip().split(' ')
n, w = int(n), int(w)
weights = [0]*n
values = [0]*n
for i in range(0, n):
  wi, ni = input("").strip().split(' ')
  values[i] = int(ni)
  weights[i] = int(wi)

def best(i, rem_weight):
  if rem_weight <= 0:
  	return 0
  if i < 0:
   	return 0
  result = 0
  if weights[i] <= rem_weight:
    result = max(result,
                 values[i] + best(i-1, rem_weight - weights[i]))
  return max(result,
               best(i-1, rem_weight))
print(best(n-1, w))

Submission status: Time Limit Exceeded

Solution code:
first = input("").strip()
n, w = first.split(' ')
n, w = int(n), int(w)
weights = [0]*n
values = [0]*n
for i in range(0, n):
  line = input("").strip()
  wi, ni = line.split(' ')
  values[i] = int(ni)
  weights[i] = int(wi)

def best(weights, values, i, rem_weight):
  if rem_weight <= 0:
  	return 0
  if i < 0:
   	return 0
  result = 0
  if weights[i] <= rem_weight:
    result = max(result,
                 values[i] + best(weights, values, i-1, rem_weight - weights[i]))
  result = max(result,
               best(weights, values, i-1, rem_weight))
  return result
print(best(weights, values, n-1, w))

Submission status: Time Limit Exceeded

Solution code:
def bits(val):
  arr = []
  while val > 0:
    arr.append(val & 1)
    val >>= 1
  return arr
def select_vals(arr, b):
  acc = 0
  for i, v in enumerate(b):
    if v:
      acc += arr[i]
  return acc  
      	
first = input("").strip()
n, w = first.split(' ')
n, w = int(n), int(w)
weights = [0]*n
values = [0]*n
for i in range(0, n):
  line = input("").strip()
  wi, ni = line.split(' ')
  values[i] = int(ni)
  weights[i] = int(wi)

def best(weights, values, rem_weight):
  if rem_weight <= 0:
  	return 0
  if len(weights) == 0:
   	return 0
  result = 0
  if weights[0] <= rem_weight:
    result = max(result,
                 values[0] + best(weights[1:], values[1:], rem_weight - weights[0]))
  result = max(result,
               best(weights[1:], values[1:], rem_weight))
  return result
print(best(weights, values, w))

#for index in range(0, 2**n):
#  acc = bits(index)
#  #print("{} -> {}".format(index, acc))
#  weight = select_vals(weights, acc)
#  value = select_vals(values, acc)
#  if weight <= w:
#    best_value = max(best_value, value)
#print(best_value)

Submission status: Time Limit Exceeded

Solution code:
def bits(val):
  arr = []
  while val > 0:
    arr.append(val & 1)
    val >>= 1
  return arr
def select_vals(arr, b):
  acc = 0
  for i, v in enumerate(b):
    if v:
      acc += arr[i]
  return acc  
      	
first = input("").strip()
n, w = first.split(' ')
n, w = int(n), int(w)
weights = [0]*n
values = [0]*n
for i in range(0, n):
  line = input("").strip()
  wi, ni = line.split(' ')
  values[i] = int(ni)
  weights[i] = int(wi)
#print("weights: {}".format(weights))
#print("vals: {}".format(values))

best_value = 0

for index in range(0, 2**n):
  acc = bits(index)
  #print("{} -> {}".format(index, acc))
  weight = select_vals(weights, acc)
  value = select_vals(values, acc)
  if weight <= w:
    best_value = max(best_value, value)
print(best_value)

Submission status: Time Limit Exceeded

Solution code:
def bits(val):
  arr = []
  while val > 0:
    arr.append(val & 1)
    val >>= 1
  return arr
def select_vals(arr, b):
  acc = 0
  for i, v in enumerate(b):
    if v:
      acc += arr[i]
  return acc  
      	
first = input("").strip()
n, w = first.split(' ')
n, w = int(n), int(w)
weights = []
values = []
for i in range(0, n):
  line = input("").strip()
  wi, ni = line.split(' ')
  values.append(int(ni))
  weights.append(int(wi))
#print("weights: {}".format(weights))
#print("vals: {}".format(values))

best_value = 0

for index in range(0, 2**n):
  acc = bits(index)
  #print("{} -> {}".format(index, acc))
  weight = select_vals(weights, acc)
  value = select_vals(values, acc)
  if weight <= w:
    best_value = max(best_value, value)
print(best_value)

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().rstrip().split(' '))
weight_lst = [0 for i in range(W+1)]

for _ in range(N):
  w, v = map(int, input().rstrip().split(' '))
  weight_lst2 = weight_lst.copy()
  for i in range(w, W+1):
    weight_lst2[i] = max(weight_lst[i-w]+v, weight_lst[i])
  weight_lst = weight_lst2
print(weight_lst[-1])

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().rstrip().split(' '))
weight_lst = [0 for i in range(W+1)]

for _ in range(N):
  w, v = map(int, input().rstrip().split(' '))
  weight_lst2 = []
  for i in range(W+1):
    if i < w:
      weight_lst2.append(weight_lst[i])
    else:
      judge = max(weight_lst[i], weight_lst[i-w]+v)
      weight_lst2.append(judge)
  weight_lst = weight_lst2
print(weight_lst[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
n,w=map(int,sys.stdin.readline().split())
weight=[0]*n
value=[0]*n
total=w
for i in range(n):
    x,y=map(int,sys.stdin.readline().split())
    weight[i]=x
    value[i]=y
l=len(weight)
dp = np.zeros(shape=(n + 1, w + 1), dtype=np.int64)
for i in range(l):
    for w in range(total+1):
        if weight[i]<=w:
            dp[i][w]=max(value[i]+dp[i-1][w-weight[i]],dp[i-1][w])
        else:
            dp[i][w] = dp[i-1][w]
print(dp[l-1][total])

Submission status: Time Limit Exceeded

Solution code:
import sys
n,w=map(int,sys.stdin.readline().split())
weight=[]
value=[]
total=w
for i in range(n):
    x,y=map(int,sys.stdin.readline().split())
    weight.append(x)
    value.append(y)
l=len(weight)
dp=[[0]*(w+1) for j in range(l)]
for i in range(l):
    for w in range(total+1):
        if weight[i]<=w:
            dp[i][w]=max(value[i]+dp[i-1][w-weight[i]],dp[i-1][w])
        else:
            dp[i][w] = dp[i-1][w]
print(dp[l-1][total])

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,input().split())
weight=[]
value=[]
total=w
for i in range(n):
    x,y=map(int,input().split())
    weight.append(x)
    value.append(y)
l=len(weight)
dp=[[0]*(w+1) for j in range(l)]
for i in range(l):
    for w in range(total+1):
        if weight[i]<=w:
            dp[i][w]=max(value[i]+dp[i-1][w-weight[i]],dp[i-1][w])
        else:
            dp[i][w] = dp[i-1][w]
print(dp[l-1][total])

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, w = [int(x) for x in input().split()]
    itens = []
    dp = [[-1]*w for i in range(n)]
    
    for _ in range(n):
        itens.append([int(x) for x in input().split()])#item([peso, valor])

    print(knapsack(n, w, itens, dp))

def knapsack(n, w, itens, dp):

    if n == 0 or w == 0:
        return 0

    if dp[n-1][w-1] != -1:
        return dp[n-1][w-1]

    if itens[n-1][0] > w:
        dp[n-1][w-1] = knapsack(n-1, w, itens, dp)
        return dp[n-1][w-1]

    else:
        dp[n-1][w-1] = max(itens[n-1][1] + knapsack(n-1, w - itens[n-1][0], itens, dp),
                           knapsack(n-1, w, itens, dp))

        return dp[n-1][w-1]

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
arr=[]
n,w=map(int,input().split())
for _ in range(n):
	arr.append(tuple(map(int,input().split())))
dp=[-1]*((n+1)*(w+1))
def f(pos,W):
	if(pos==n):
		return 0
	if dp[pos*(w+1)+W]!=-1:
		return dp[pos*(w+1)+W]
	res=f(pos+1,W)
	if(arr[pos][0]<=W):
		res=max(res,f(pos+1,W-arr[pos][0])+arr[pos][1])
	dp[pos*(w+1)+W]=res
	return res
print(f(0,w))

Submission status: Time Limit Exceeded

Solution code:
arr=[]
n,w=map(int,input().split())
for _ in range(n):
	arr.append(tuple(map(int,input().split())))
dp=[-1]*((n+1)*(w+1))
def f(pos,W):
	if(pos==n):
		return 0
	if dp[pos*(w+1)+W]!=-1:
		return dp[pos*(w+1)+W]
	res=f(pos+1,W)
	if(arr[pos][0]<=W):
		res=max(res,f(pos+1,W-arr[pos][0])+arr[pos][1])
	dp[pos*(w+1)+W]=res
	return res
print(f(0,w))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
arr=[]
n,w=map(int,input().split())
for _ in range(n):
	arr.append(tuple(map(int,input().split())))
@lru_cache(maxsize=10000000)
def f(pos,W):
	if(pos==n):
		return 0
	res=f(pos+1,W)
	if(arr[pos][0]<=W):
		res=max(res,f(pos+1,W-arr[pos][0])+arr[pos][1])
	return res
print(f(0,w))

Submission status: Time Limit Exceeded

Solution code:
# absotutely TLE
arr=[]
n,w=map(int,input().split())
for _ in range(n):
	arr.append(tuple(map(int,input().split())))
def f(pos,W):
	if(pos==n):
		return 0
	res=f(pos+1,W)
	if(arr[pos][0]<=W):
		res=max(res,f(pos+1,W-arr[pos][0])+arr[pos][1])
	return res
print(f(0,w))

Submission status: Time Limit Exceeded

Solution code:
n,W=map(int,input().split())
wt=[]
value=[]
for i in range(n):
    x,y=map(int,input().split())
    value.append(y)
    wt.append(x)
dp = [0] * (W + 1)
for i in range(n):
    for j in range(W, wt[i]-1, -1):
        dp[j] = max(dp[j], value[i] + dp[j - wt[i]])
print(dp[W])

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,input().split())
weight=[]
value=[]
total=w
for i in range(n):
    x,y=map(int,input().split())
    weight.append(x)
    value.append(y)
l=len(weight)
dp=[[0 for i in range(total+1)] for i in range(l)]
for i in range(l):
    for w in range(total+1):
        if weight[i]<=w:
            dp[i][w]=max(value[i]+dp[i-1][w-weight[i]],dp[i-1][w])
        else:
            dp[i][w] = dp[i-1][w]
print(dp[l-1][total])

Submission status: Time Limit Exceeded

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

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