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:
rs  = lambda: input()
ri  = lambda: int(input())
rm  = lambda: map(int, input().split())
rai = lambda: [int(x) for x in input().split()]

N, W = rm()  
wgt = []
val = []      
for _ in range(N):
    a, b = rm()
    wgt.append(a)
    val.append(b)


#A naive recursive implementation of 0-1 Knapsack Problem 
  
# Returns the maximum value that can be put in a knapsack of 
# capacity W 
def knapSack(W , wt , val , n): 
  
    # Base Case 
    if n == 0 or W == 0 : 
        return 0
  
    # If weight of the nth item is more than Knapsack of capacity 
    # W, then this item cannot be included in the optimal solution 
    if (wt[n-1] > W): 
        return knapSack(W , wt , val , n-1) 
  
    # return the maximum of two cases: 
    # (1) nth item included 
    # (2) not included 
    else: 
        return max(val[n-1] + knapSack(W-wt[n-1] , wt , val , n-1), 
                   knapSack(W , wt , val , n-1)) 

print (knapSack(W , wgt , val , N))

Submission status: Time Limit Exceeded

Solution code:
ri  = lambda: int(input())
rm  = lambda: map(int, input().split())

N, W = rm()  
wgt = []
val = []      
for _ in range(N):
    a, b = rm()
    wgt.append(a)
    val.append(b)

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import collections, heapq, itertools, math
groupby = itertools.groupby
rs  = lambda: input()
ri  = lambda: int(input())
rm  = lambda: map(int, input().split())
rai = lambda: [int(x) for x in input().split()]

N, W = rm()  
wgt = []
val = []      
for _ in range(N):
    a, b = rm()
    wgt.append(a)
    val.append(b)

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
n,w=map(int,input().split())
dp=[[0]*w for _ in range(n)]
for i in range(n):
  a,b=map(int,input().split())
  for j in range(w):
    if i==0:
      if j>a-2:dp[i][j]=b
    elif j>a-2:
      if j==a-1:dp[i][j]=max(b,dp[i-1][j])
      else:dp[i][j]=max(dp[i-1][j-a]+b,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:
#D-Knapsack 1
def main():
    N,W=map(int,input().split())
    DP=[[0 for j in range(W+1)]for i in range(N+1)]
    for i in range(N):
        w,v=map(int,input().split())
        for j in range(1,W+1):
            if j>=w:
                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])
    
main()

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+1)]
for i in range(N):
    w,v=map(int,input().split())
    for j in range(1,W+1):
        if j>=w:
            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, m = map(int, input().split())
W, V = [], []
for i in range(n):
    w, v = map(int, input().split())
    W.append(w)
    V.append(v)
A = [0 for j in range(m+1)]
for i in range(n):
    B = []
    for j in range(W[i-1]):
        B.append(A[j])
    for j in range(W[i-1], m+1):
        B.append(max(A[j], A[j-W[i-1]]+V[i-1]))
    A = B
print(A[m])

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
W, V = [], []
for i in range(n):
    w, v = map(int, input().split())
    W.append(w)
    V.append(v)
F = [[0 for j in range(m+1)] for i in range(n+1)]
for i in range(1, n+1):
    for j in range(W[i-1]):
        F[i][j] = F[i-1][j]
    for j in range(W[i-1], m+1):
        F[i][j] = max(F[i-1][j], F[i-1][j-W[i-1]]+V[i-1])
print(F[n][m])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

wt  = [0] * (n+1)
val = [0] * (n+1)

for i in range(0,n):
    wt[i], val[i] = list(map(int,input().split()))

def knapSack(W, wt, val, n): 
    K = [[0 for x in range(W+1)] for x in range(n+1)] 
  
    # Build table K[][] in bottom up manner 
    for i in range(n+1): 
        for w in range(W+1): 
            if i==0 or w==0: 
                K[i][w] = 0
            elif wt[i-1] <= w: 
                K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]],  K[i-1][w]) 
            else: 
                K[i][w] = K[i-1][w] 
  
    return K[n][W] 
  
print(knapSack(W, wt, val, n))

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 _ in range(N + 1)]
for i in range(N):
    for sum_w in range(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])

Submission status: Time Limit Exceeded

Solution code:
n,w = map(int,input().split())
v = [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(w+1):
    if j < v[i-1][0]:
      dp[i][j] = dp[i-1][j]
    else:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-v[i-1][0]]+v[i-1][1])
print(dp[n][w])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
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 _ in range(N+1)]

def memo(i,j):
  if dp[i][j]:
    return dp[i][j]
  if i == N:
    dp[i][j] = 0
  elif w[i] > j:
    dp[i][j] = memo(i+1,j)
  else:
    dp[i][j] = max(memo(i+1,j),memo(i+1,j-w[i])+v[i])
  return dp[i][j] 
print(memo(0,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 _ in range(N+1)]

def memo(i,j):
  if dp[i][j]:
    return dp[i][j]
  if i == N:
    dp[i][j] = 0
  elif w[i] > j:
    dp[i][j] = memo(i+1,j)
  else:
    dp[i][j] = max(memo(i+1,j),memo(i+1,j-w[i])+v[i])
  return dp[i][j] 
print(memo(0,W))

Submission status: Time Limit Exceeded

Solution code:
class item(object):
    def __init__(self,name,Value,weight):
        self.name=name
        self.value=Value
        self.weight=weight
    def getweight(self):
        return self.weight
    def getname(self):
        return self.name
    def getvalue(self):
        return self.value
    def __str__(self):
        res="<"+str(self.name)+","+str(self.value)+","+str(self.weight)+">"
        return res
"""def sumV(itemlist):
    result=0
    for i in range(len(itemlist)):
        result+=itemlist[i].getvalue
    return result
def sumW(itemlist):
    result=0
    for i in range(len(itemlist)):
        result+=itemlist[i].getweight
    return result"""
def maxVal(toConsider,avail,result):
    if toConsider==[] or avail==0:
        result+=0
    elif toConsider[0].getweight()>avail:
        result+=maxVal(toConsider[1:],avail,result)
    else:
        withval=toConsider[0].getvalue()+maxVal(toConsider[1:],avail-toConsider[0].getweight(),result)
        #とるかとらないかを選ぶ
        withoutval=maxVal(toConsider[1:],avail,result)
        if withval>withoutval:
            result+=withval
        else:
            result+=withoutval
    return result
N,W=map(int,input().split())
result=0
items=[]
for i in range(N):
    w,v=map(int,input().split())
    items.append(item(str(i),v,w))
"""for i in range(len(items)):
    print(items[i])"""
print(maxVal(items,W,result))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N,W = map(int,input().split())
dp = np.zeros((N+2,W+2), dtype=int)

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

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

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
w = list()
v = list()
for _ 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(N):
    for j in range(W + 1):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i])
        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())
v = []
wt = []
for _ in range(n):
    x,y=map(int,input().split())
    wt.append(x)
    v.append(y)
a = [[0 for x in range(w+1)] for x in range(n+1)] 
for i in range(n+1):
    for j in range(w+1):
        if i == 0 or j==0:
            a[i][j]=0
        elif wt[i-1]<=j:
            a[i][j]=max(a[i-1][j],(v[i-1]+a[i-1][j-wt[i-1]]))
        else:
            a[i][j]=a[i-1][j]
print(a[n][w])

Submission status: Time Limit Exceeded

Solution code:
N,W=map(int,input().split())
L = [list(map(int, input().split())) for i in range(N)]
dp = [[0 for j in range(110000)] for i in range(110)]
for i in range(1,N+1):
  for j in range(W+1):
    dp[i][j]=dp[i-1][j]
    if j-L[i-1][0]>=0:
      dp[i][j]=max(dp[i][j],dp[i-1][j-L[i-1][0]]+L[i-1][1])
ans=dp[N][W]
print(ans)

Submission status: Time Limit Exceeded

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

'''
3 8
3 30
4 50
5 60
'''

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

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

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

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

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

Submission status: Time Limit Exceeded

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

def main():
    N, W = map(int, input().split())
    wv = list(map(int, input().split()) for _ in range(N))
    
    dp = [0] * (W + 1)
    
    for w, v in wv:
        for i in range(W, w - 1, -1):
            dp[i] = max(dp[i], dp[i - w] + v)
    
    print(dp[-1])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def main():
    N, W = map(int, input().split())
    wv = list(map(int, input().split()) for _ in range(N))
    
    dp = [0] * (W + 1)
    
    for w, v in wv:
        for i in range(W, -1, -1):
            if i - w >= 0:
                dp[i] = max(dp[i], dp[i - w] + v)
    
    print(dp[-1])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,W = map(int,input().split())
ans = [[0,0]]
ans_fin = 0
for i in range(N):
    w,v = map(int,input().split())
    for j in range(len(ans)):
        if ans[j][0] + w <= W:
            ans.append([ans[j][0]+w,ans[j][1]+v])
            ans_fin = max(ans_fin,ans[j][1]+v)
print(ans_fin)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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


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

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N, W = map(int, input().split())
    items = [tuple(map(int, input().split())) for _ in range(N)]

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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
 
N, W = map(int,input().split())
H = list(list(map(int,input().split())) for _ in range(N))
#sub problem: max cost from 0 to ith day
memo = {}

def rec(num_of_item, left_w):
  if (num_of_item, left_w) in memo:
    return memo[(num_of_item, left_w)]
  if num_of_item == N:
    return 0
  w, v = H[num_of_item]
  if left_w - w < 0:
    memo[(num_of_item, left_w)] = rec(num_of_item + 1, left_w)
    return memo[(num_of_item, left_w)]
  memo[(num_of_item, left_w)] = max(v + rec(num_of_item + 1, left_w - w), rec(num_of_item + 1, left_w))
  return memo[(num_of_item, left_w)]
print(rec(0, W))

Submission status: Time Limit Exceeded

Solution code:
N,W = list(map(int,input().split()))
w_l = []
v_l = []
for i in range(N):
    w,v = list(map(int,input().split()))
    w_l.append(w)
    v_l.append(v)
def knapsack(N,W,w_l,v_l):
    if (N==0 or W==0):
        return 0
    elif(w_l[N-1]>W):
        return knapsack(N-1,W,w_l,v_l)
    
    else:
        return max(knapsack(N-1,W,w_l,v_l), v_l[N-1]+knapsack(N-1,W-w_l[N-1],w_l,v_l))
ans = knapsack(N,W,w_l,v_l)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,W = list(map(int,input().split()))
w_l = []
v_l = []
for i in range(N):
    w,v = list(map(int,input().split()))
    w_l.append(w)
    v_l.append(v)
def knapsack(N,W,w_l,v_l):
    if (N==0 or W==0):
        return 0
    elif(w_l[N-1]>W):
        return knapsack(N-1,W,w_l,v_l)
    
    return max(knapsack(N-1,W,w_l,v_l), v_l[N-1]+knapsack(N-1,W-w_l[N-1],w_l,v_l))
ans = knapsack(N,W,w_l,v_l)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
  num,capacity,*wv =map(int,open(0).read().split())
  weights,values = wv[::2], wv[1::2]
  DP=[-1]*(capacity+1)
  DP[0]=0
  for weight,value in zip(weights, values):
    left=capacity-weight
    DP[weight:]=[max(DP[i]+value,DP[i+weight]) for i in range(left,-1,-1)][::-1]
  print(max(DP))
      
if __name__=="__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
N,W=map(int,input().split())
w = [0]
v = [0]
for i in range(N):
    WW,VV = input().split()
    w.append(int(WW))
    v.append(int(VV))
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(1,W+1):
        DP[i][j] = max(DP[i-1][j], DP[i][j-1])
        if j - w[i] >= 0:
            DP[i][j] = max(DP[i][j],DP[i-1][j - w[i]] + v[i])
print(DP[N][W])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys


def main():
    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())
    dp = np.zeros((N + 1, W + 1), dtype=np.int64)
    for i in range(N):
        for r_w in range(W + 1):
            if (r_w >= w[i]):
                dp[i + 1][r_w] = max(dp[i][r_w - w[i]] + v[i], dp[i][r_w])
            else:
                dp[i + 1][r_w] = dp[i][r_w]
    print(dp[N][W])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys


def DP(i, X, w, v):
    global memo
    if (X < 0):
        return -10 ** 9
    if (memo[i, X] != -10 ** 9):
        return memo[i, X]
    result = max(DP(i + 1, X, w, v),
                 DP(i + 1, X - w[i], w, v) + v[i])
    memo[i, X] = result
    return result


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())
MINUS_INF = -10 ** 9
memo = np.full((N + 1, W + 1), MINUS_INF, dtype=np.int64)
memo[N, :] = 0
answer = DP(0, W, w, v)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def DP(i, X, w, v):
    global memo
    if (X < 0):
        return -10 ** 9
    if (memo[i, X] != -10 ** 9):
        return memo[i, X]
    result = max(DP(i + 1, X, w, v),
                 DP(i + 1, X - w[i], w, v) + v[i])
    memo[i, X] = result
    return result


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())
MINUS_INF = -10 ** 9
memo = np.full((N + 1, W + 1), MINUS_INF, dtype='int64')
memo[N, :] = 0
answer = DP(0, W, w, v)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
def zeros(h,*w): return [[0] * w[0]  for _ in range(h)] if w else [[0] * h  for _ in range(h)]

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

dp = zeros(n+1,W+1)

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

ans = max( dp[n] )

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#EDPC D

#dp[ i ][ sum_w ] := i-1 番目までの品物から重さが 
# sum_w以下となるときの、価値の総和の最大値

#dpのindexはややこしくなりがち

N,W=map(int,input().split())
w,v=[0],[0]#w,vのindexと品物の番号を合わせるため、頭に0を追加

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

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

print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
N, L = map(int, input().split())
W = []
V = []
for _ in range(N):
    w, v = map(int, input().split())
    W.append(w)
    V.append(v)


dp = [[-1] * (L+100) for _ in range(N+10)]


def knap(i, j):
    if dp[i][j] >= 0:
        return dp[i][j]

    if i == N:
        dp[i][j] = 0
    elif j < W[i]:
        dp[i][j] = knap(i+1, j)
    else:
        dp[i][j] = max(knap(i+1, j), knap(i+1, j - W[i]) + V[i])

    return dp[i][j]


print(knap(0, L))

Submission status: Time Limit Exceeded

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

sys.setrecursionlimit(10**8)


def knap(i, j):
    if i == N:
        return 0
    elif j < W[i]:
        return knap(i+1, j)
    else:
        return max(knap(i+1, j), knap(i+1, j - W[i]) + V[i])


print(knap(0, L))

Submission status: Time Limit Exceeded

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

#output
import numpy as np
dp = np.zeros((N+1, W+1), int)

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

print(dp[-1, -1])

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np

input = sys.stdin.readline
N, weights = map(int, input().split())

dp = [0] * (weights + 1)
# dp = np.zeros(weights + 1, dtype=np.int64)

while N:
    w, v = map(int, input().split())
    # np.maximum(dp[:-w] + v, dp[w:], out=dp[w:])
    for i in range(weights, -1, -1):
        if i - w >= 0:
            dp[i] = max(dp[i], dp[i - w] + v)
    # print(dp)
    N -= 1
print(dp[-1])

Submission status: Time Limit Exceeded

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

dp = [0] * (weights + 1)

while N:
    w, v = map(int, input().split())
    for i in range(weights, -1, -1):
        if i - w >= 0:
            dp[i] = max(dp[i], dp[i - w] + v)
    # print(dp)
    N -= 1
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, islice
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')

N, W = MAP()
w, v = ZIP(N)

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

for i in range(N):
	for j in range(W+1):
		if j-w[i] >= 0:
			dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]]+v[i])
		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())
weight = []
value = []
for i in range(N):
    w, v = map(int, input().split())
    weight.append(w)
    value.append(v)
dp = [[0 for _ in range(W+1)] for _ in range(N+1)]
for i in range(N):
    for j in range(W+1):
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        if j - weight[i] >= 0:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j-weight[i]]+value[i])
print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
import sys
 
input = sys.stdin.readline
 
N, W = map(int, input().split())
 
dpi = [[0 for i in range(W+1)] for j in range(N+1)]
 
for i in range(1, N+1):
  w, v = map(int, input().split())
  
  for j in range(W+1):
    if j < w:
      dpi[i][j] = dpi[i-1][j]
    else:
      dpi[i][j] = max(dpi[i-1][j-w] + v, dpi[i-1][j])
print(dpi[N][W])

Submission status: Time Limit Exceeded

Solution code:
N,W = map(int,input().split())
wv =[[0,0]]
dp = []
Counting = []
for _ in range(N):
    wv.append(list(map(int,input().split())))
for _ in range(N+1):
    dp.append([0]*(W+1))
    Counting.append([0]*(W+1))
for i in range(1,N+1):
    for sum_w in range(W+1):
        if Counting[i-1][sum_w] > sum_w:
            continue
        elif sum_w-wv[i][0]<0:
            dp[i][sum_w] =  dp[i-1][sum_w]
            Counting[i][sum_w]  = Counting[i-1][sum_w]
        else:
            if dp[i-1][sum_w] < dp[i-1][sum_w-wv[i][0]]+wv[i][1]:
                Counting[i][sum_w]  = Counting[i-1][sum_w-wv[i][0]]  + wv[i][0]
            else:
                Counting[i][sum_w]  = Counting[i-1][sum_w]
            dp[i][sum_w] = max(dp[i-1][sum_w],dp[i-1][sum_w-wv[i][0]]+wv[i][1])
print(dp[-1][-1])

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

W, V = [0], [0]
for _ in range(N):
    w, v = map(int, input().split())
    W.append(w)
    V.append(v)
# print('W', W)
# print('V', V)
# dp[i][sum_w] := i-1 番目までの品物から重さが sum_w を超えないように選んだときの、価値の総和の最大値
# dp[i + 1][sum_w], dp[i][sum_w - w[i]] + v[i]

dp = [[0] * (_W + 2) for _ in range(N + 3)]
for i in range(1, N + 1):
    w, v = W[i], V[i]
    for sum_w in range(_W + 1):
        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
        if sum_w - w < 0:
            continue
        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - w] + v)

import numpy as np
# print('dp')
print(np.array(dp).max())

Submission status: Time Limit Exceeded

Solution code:
N,w = [int(x) for x in input().split()]
ll = []
for i in range(N):
    abc = [int(x) for x in input().split()]
    ll.append(abc)
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 ll[i-1][0] <= j:
            DP[i][j] = max(DP[i-1][j-ll[i-1][0]]+ll[i-1][1],DP[i-1][j])
        else:
            DP[i][j] = DP[i-1][j]
print(DP[N][w])

Submission status: Time Limit Exceeded

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

weight = [None] * N
value = [None] * N
dp = [[None] * (W+1) for _ in range(N+1)]

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

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

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

ans = dp[N][W]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,W=map(int,input().split())
w = [0]
v = [0]
for i in range(N):
    WW,VV = input().split()
    w.append(int(WW))
    v.append(int(VV))
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(1,W+1):
        DP[i][j] = max(DP[i-1][j], DP[i][j-1])
        if j - w[i] >= 0:
            S = DP[i-1][j - w[i]] + v[i]
            if S >= DP[i][j]:
                DP[i][j] = S
print(DP[N][W])

Submission status: Time Limit Exceeded

Solution code:
N,W=map(int,input().split())
w = [0]
v = [0]
for i in range(N):
    WW,VV = input().split()
    w.append(int(WW))
    v.append(int(VV))
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(1,W+1):
        if DP[i-1][j] >= DP[i][j-1]:
            DP[i][j] = DP[i-1][j]
        else:
            DP[i][j] = DP[i][j-1]
        if j - w[i] >= 0:
            S = DP[i-1][j - w[i]] + v[i]
            if S >= DP[i][j]:
                DP[i][j] = S
print(DP[N][W])

Submission status: Time Limit Exceeded

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

for w,v in MV:
    for tw in range(W,w-1,-1):
        dp[tw]=max(dp[tw],dp[tw-w]+v)
        #print(dp,tw)

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
N,W=map(int,input().split())
w = [0]
v = [0]
for i in range(N):
    WW,VV = input().split()
    w.append(int(WW))
    v.append(int(VV))
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(1,W+1):
        if j - w[i] >=0:
            if DP[i-1][j] >= DP[i][j-1]:
                DP[i][j] = DP[i-1][j]
            else:
                DP[i][j] = DP[i][j-1]
            S = DP[i-1][j - w[i]] + v[i]
            if S >= DP[i][j]:
                DP[i][j] = S
        else:
            if DP[i-1][j] >= DP[i][j-1]:
                DP[i][j] = DP[i-1][j]
            else:
                DP[i][j] = DP[i][j-1]
print(DP[N][W])

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
dp = [0] * (W+1)

for _ in range(N):
    w, v = map(int, input().split())
    for dp_w in reversed(range(W+1)):
        dp_v = dp[dp_w]
        if (dp_w == 0 or dp_v > 0) and dp_w + w <= W:
            dp[dp_w + w] = max(dp[dp_w + w], dp[dp_w] + v)

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
n,W = mi()
a = [tuple(int(i) for i in input().split()) for _ in range(n)]

dp = [0]*(W+1)
for w,v in a:
    for j in range(w,W+1)[::-1]:
        dp[j] = max(dp[j], dp[j-w] + v)
print(dp[W])

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 for _ in range(w + 1)] for _ in range(n + 1)]

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,input().split())
weight=[]
value=[]
for _ in range(n):
    ww,vv=map(int,input().split())
    weight.append(ww)
    value.append(vv)
dp=[[0 for i in range(w+1)] for j in range(n+1)]
for i in range(0,n):
    for w_sum in range(w+1):
        if w_sum >= weight[i]:
            dp[i+1][w_sum]=max(dp[i+1][w_sum],dp[i][w_sum-weight[i]]+value[i])
        dp[i+1][w_sum]=max(dp[i+1][w_sum],dp[i][w_sum])
print(dp[n][w])

Submission status: Time Limit Exceeded

Solution code:
#ナップザック問題
import numpy as np

N,W=map(int,input().split())
wv = np.array([list(map(int, input().split())) for i in range(0, N)])  


dp=np.zeros((N+1,W+1),dtype=int)
for n in range(0, N):  
    for w in range(0, W + 1):  
        if w < wv[n, 0]:  
            dp[n+1, w] = dp[n, w]  
        else:  
            dp[n+1, w] = max(dp[n, w], dp[n, w - wv[n, 0]] + wv[n, 1])  
print(dp[N,W])

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np
from sys import stdin,stdout

input = stdin.readline
print = stdout.write

vi = [0]
wi = [0]

n,W=map(int,input().split())
for i in range(n):
    a,b = map(int,input().split())
    vi.append(b)
    wi.append(a)
#print(str(vi),'\n',str(wi))
    #dp = np.zeros((n+1,MW+1),dtype=np.int64)
dp = [[0]*(W+1) for i in range(n+1)]
    
for j in range(1,n+1):
    for k in range(1,W+1):
        if(wi[j]>k) :
            dp[j][k] = dp[j-1][k]
        else:
            dp[j][k] = max(dp[j-1][k],dp[j-1][k-wi[j]]+vi[j])

print(str(dp[n][W]))