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:
def d():
    n, w = list(map(int, input().split()))
    arr = []
    for _ in range(n):
        weight, value = list(map(int, input().split()))
        arr.append((weight, value))

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

    for weight in range(w+1):
        dp[0][weight] = 0 if weight < arr[0][0] else arr[0][1]

    for item in range(n):
        dp[item][0] = 0

    for item in range(1, n):
        for weight in range(1, w+1):
            take = 0
            ww, value = arr[item]
            if weight >= ww:
                take = value + dp[item -1][weight - ww]
            dp[item][weight] = max(dp[item-1][weight], take)
            
    return dp[-1][-1]

print(d())

Submission status: Time Limit Exceeded

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

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):
        if sum_w >= li[i][0]:
            dp[i+1][sum_w] = max(dp[i][sum_w-li[i][0]]+li[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:
n,w = map(int,input().split())
L = [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 j in range(w+1):
        if j >= L[i][0]:
            dp[i+1][j] = max(dp[i][j-L[i][0]]+L[i][1],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())
dp = [[0 for x in range(w+1)] for x in range(n+1)]
array = [[int(x) for x in input().split()] for x in range(n)]
for i in range(1,w+1):
    for j in range(1,n+1):
        if(array[j-1][0] > i):
            dp[j][i] = dp[j-1][i]
        else:
            if(dp[j-1][i] < dp[j-1][i-array[j-1][0]] + array[j-1][1]):
                dp[j][i] = dp[j-1][i-array[j-1][0]] + array[j-1][1]
            else:
                dp[j][i] = dp[j-1][i]
print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
n,W = map(int,input().split())
w,v = [0]*n, [0]*n
for i in range(n):
    w[i], v[i] = map(int,input().split())

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+1][j], dp[i][j-w[i]]+v[i])
        dp[i+1][j] = max(dp[i+1][j],dp[i][j])
print(dp[n][W])

Submission status: Time Limit Exceeded

Solution code:
# inputを高速化する。
import sys
input = sys.stdin.readline

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

dp = [0] * (W+1)

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

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
def knapsack(weights, values, N, C):
    space = [[0 for j in range(C + 1)] for i in range(N + 1)]
    
    for i in range(1, N + 1):
        for j in range(1, C + 1):
            if j < weights[i - 1]:
                space[i][j] = space[i - 1][j]
            else:
                space[i][j] = max(values[i - 1] + space[i - 1][j - weights[i - 1]], space[i - 1][j])

    return space[N][C]


N, C = list(map(int, input().strip().split()))

weights = []
values = []

for i in range(N):
    weight, value = list(map(int, input().strip().split()))
    weights.append(weight)
    values.append(value)

print(knapsack(weights, values, N, C))

Submission status: Time Limit Exceeded

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

ans = [[0]*(10**5+10) for i in range(N+10)]
  
for i in range(N):
  for j in range(W+1):
    if j >= lst[i][0]:
      ans[i+1][j] = max(ans[i][j-lst[i][0]] + lst[i][1], ans[i][j])
    else:
      ans[i+1][j] = ans[i][j]
  
print(ans[N][W])

Submission status: Time Limit Exceeded

Solution code:
n,w = map(int,input().split())
dp = [[0]*(w+1) for _ in range(n+1)]#[[0]*(w+1)] *(n+1)
for i in range(1,n+1):
    wi,vi = [int(i) for i in input().split()]
    #DPテーブルの更新
    for j in range(w+1):
        if j >= wi : #選べるかどうか
            dp[i][j] = max(dp[i-1][j] , dp[i-1][j-wi]+vi) #選ぶ場合
        else:
            dp[i][j] = max(dp[i-1][j] , dp[i][j]) #選ばない場合
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+1)]#[[0]*(w+1)] *(n+1)
for i in range(1,n+1):
    wi,vi = map(int,input().split())
    #DPテーブルの更新
    for j in range(w+1):
        if j >= wi : #選べるかどうか
            dp[i][j] = max(dp[i-1][j] , dp[i-1][j-wi]+vi) #選ぶ場合
        else:
            dp[i][j] = max(dp[i-1][j] , dp[i][j]) #選ばない場合
print(dp[n][w])

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()
weight = []
val = []
used = [False for i in range(N)] 
mx = float('-inf')
for _ in range(N):
    w, v = rm() 
    weight.append(w)
    val.append(v)

def backtrack(w,v):
    global mx
    if w > W: return
    mx = max(mx,v)
    for i in range(N):
        if not used[i]:
            used[i] = True
            backtrack(w + weight[i], v + val[i])
            used[i] = False
backtrack(0,0)
print(mx)

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict


# for #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase


def main():
    n,w = [int(j) for j in input().split()]
    wt = []
    v = []
    for i in range(n):
        x,y = [int(j) for j in input().split()]
        wt.append(x)
        v.append(y)
    # dp = [[0 for i in range(n)] for j in range(w)]
    dp = defaultdict(lambda: 0)
    for i in range(n):
        for j in range(1,w+1):
            dp[i, j] = dp[i-1, j]
            if j-wt[i]>=0:
                dp[i,j] = max(dp[i, j], dp[i-1, j-wt[i]]+v[i])

    print(dp[n-1,w])




# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n,w = [int(j) for j in raw_input().split()]
wt = []
v = []
for i in range(n):
    x,y = [int(j) for j in raw_input().split()]
    wt.append(x)
    v.append(y)
# dp = [[0 for i in range(n)] for j in range(w)]
dp = defaultdict(lambda: 0)
for i in range(n):
    for j in range(1,w+1):
        dp[i, j] = dp[i-1, j]
        if j-wt[i]>=0:
            dp[i,j] = max(dp[i, j], dp[i-1, j-wt[i]]+v[i])

print(dp[n-1,w])

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import sys
 
sys.setrecursionlimit(1000000)
 
N, W = list(map(int, input().split(" ")))
 
l = []
for i in range(N):
	w, v = list(map(int, input().split(" ")))
	l.append((w, v))
    
@lru_cache(maxsize=1000000)
def dp(i, score, remaining_weight):
	if (i < 0):
		return score
	if remaining_weight >= l[i][0]:
		# on / off
		return max([dp(i - 1, score + l[i][1], remaining_weight - l[i][0]), dp(i - 1, score, remaining_weight)])
	else:
		return dp(i-1, score, remaining_weight)
 
print(dp(N-1, 0, W))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import sys

sys.setrecursionlimit(1000000)
 
N, W = list(map(int, input().split(" ")))
 
l = []
for i in range(N):
	w, v = list(map(int, input().split(" ")))
	l.append((w, v))
    
@lru_cache(maxsize=1000000)
def dp(i, score, remaining_weight):
	if (i < 0):
		return score
	if remaining_weight >= l[i][0]:
		# on / off
		return max([dp(i - 1, score + l[i][1], remaining_weight - l[i][0]), dp(i - 1, score, remaining_weight)])
	else:
		return dp(i-1, score, remaining_weight)
 
print(dp(N-1, 0, W))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache


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

l = []
for i in range(N):
	w, v = list(map(int, input().split(" ")))
	l.append((w, v))
    
@lru_cache(maxsize=1000000)
def dp(i, score, remaining_weight):
	if (i < 0):
		return score
	if remaining_weight >= l[i][0]:
		# on / off
		return max([dp(i - 1, score + l[i][1], remaining_weight - l[i][0]), dp(i - 1, score, remaining_weight)])
	else:
		return dp(i-1, score, remaining_weight)

print(dp(N-1, 0, W))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache


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

l = []
for i in range(N):
	w, v = list(map(int, input().split(" ")))
	l.append((w, v))
    
@lru_cache(maxsize=1000)
def dp(i, score, remaining_weight):
	if (i < 0):
		return score
	if remaining_weight >= l[i][0]:
		# on / off
		return max([dp(i - 1, score + l[i][1], remaining_weight - l[i][0]), dp(i - 1, score, remaining_weight)])
	else:
		return dp(i-1, score, remaining_weight)

print(dp(N-1, 0, W))

Submission status: Time Limit Exceeded

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

l = []
for i in range(N):
	w, v = list(map(int, input().split(" ")))
	l.append((w, v))

def dp(i, score, remaining_weight):
	if (i < 0):
		return score
	if remaining_weight >= l[i][0]:
		# on / off
		return max([dp(i - 1, score + l[i][1], remaining_weight - l[i][0]), dp(i - 1, score, remaining_weight)])
	else:
		return dp(i-1, score, remaining_weight)

print(dp(N-1, 0, W))

Submission status: Time Limit Exceeded

Solution code:
def f(i,w,d):
  if i==n:
    return 0
  if (i,w) not in d:
    if a[i][0]>w:
      d[i,w]=f(i+1,w,d)
    else:
      d[i,w]=max(f(i+1,w,d),f(i+1,w-a[i][0],d)+a[i][1])
  return d[i,w]
n,w=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(n)]
print f(0,w,{})

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(n)]
z=[[0]*(w+1)]
for i in xrange(n):
    z.append(z[-1][:])
    for j in xrange(a[i][0],w+1):
		z[i+1][j]=max(z[i+1][j],z[i][j-a[i][0]]+a[i][1])
print max(z[-1])

Submission status: Time Limit Exceeded

Solution code:
import copy
n, max_w = map(int, input().split())

dp = [0]*(max_w+1)
for _ in range(n):
    dp_copy = copy.copy(dp)
    w, v = map(int, input().split())
    for i in range(w, max_w+1):
        if i >= w:
            dp_copy[i] = max(dp[i-w] + v, dp[i])
    dp = copy.copy(dp_copy)

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
import copy
n, max_w = map(int, input().split())

dp = [0]*(max_w+1)
for _ in range(n):
    dp_copy = copy.copy(dp)
    w, v = map(int, input().split())
    for i in range(max_w+1):
        if i >= w:
            dp_copy[i] = max(dp[i-w] + v, dp[i])
    dp = copy.copy(dp_copy)
    
print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def resolve():
    n, w = map(int, input().split())
    goods = np.array([list(map(int, input().split())) for _ in range(n)])

    dp = np.ndarray((n, w))
    dp.fill(-1)

    print(knapsack(n - 1, w, goods, dp))

def knapsack(i, j, goods, dp):
    """残りi個の品物から、重さがj以下になるように選んだときの価値の最大値をreturnする関数"""
    if i < 0:
        # もう品物は残っていない
        res = 0

    elif dp[i - 1, j - 1] != -1:
        return dp[i - 1, j - 1]

    elif j < goods[i][0]:
        # 品物が重すぎて入らない
        res = knapsack(i - 1, j, goods, dp)
        dp[i - 1, j - 1] = res
    else:
        # 品物を入れる場合と入れない場合とで、価値が大きい方をreturn
        res = max(knapsack(i - 1, j - goods[i][0], goods, dp) + goods[i][1], knapsack(i - 1, j, goods, dp))
        dp[i - 1, j - 1] = res

    return res
  
resolve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def resolve():
    n, w = map(int, input().split())
    goods = np.array([list(map(int, input().split())) for _ in range(n)])

    dp = np.zeros(1)
    print(knapsack(n - 1, w, goods, dp))



def knapsack(i, j, goods, dp):
    """残りi個の品物から、重さがj以下になるように選んだときの価値の最大値をreturnする関数"""
    if i < 0:
        # もう品物は残っていない
        return 0
    if j < goods[i][0]:
        # 品物が重すぎて入らない
        return knapsack(i - 1, j, goods, dp)
    else:
        # 品物を入れる場合と入れない場合とで、価値が大きい方をreturn 
        return max(knapsack(i - 1, j - goods[i][0], goods, dp) + goods[i][1], knapsack(i - 1, j, goods, dp))

resolve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n,w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
#  w ,v
ans = [0] * (w + 1)
ans[a[0][0]] = a[0][1]

for i in range(1, n):
    t = [j for j in ans]
    g,v = a[i][0], a[i][1]


    t = [0]*g+list(np.array(ans[:w+1-g])+v)
    ans = np.maximum(ans,t)

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n,w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
#  w ,v
ans = [0] * (w + 1)
ans[a[0][0]] = a[0][1]

for i in range(1, n):
    t = [j for j in ans]
    g,v = a[i][0], a[i][1]

    for j in range(w-g,0-1,-1): #weight counter
        ans[j + g] = max(ans[j + g], ans[j] + v)

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

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

# for i in range(N+1):
#     print(dp[i])

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys

N, W = map(int, sys.stdin.buffer.readline().split())
   
dp = [-1] * (W+1)
dp[0] = 0

for i in range(1, N+1):
    w, v = map(int, sys.stdin.buffer.readline().split())
    for j in range(W-w, -1, -1):
        if dp[j] == -1:
            continue
        if dp[j+w] < dp[j]+v:
            dp[j+w] = dp[j] + v

# for i in range(N+1):
#     print(dp[i])

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
import numpy as np

N, W = map(int, sys.stdin.buffer.readline().split())
   
dp = np.ones(W+1, dtype=int) * -1
dp[0] = 0

for i in range(1, N+1):
    w, v = map(int, sys.stdin.buffer.readline().split())
    for j in range(W-w, -1, -1):
        if dp[j] == -1:
            continue
        if dp[j+w] < dp[j]+v:
            dp[j+w] = dp[j] + v

# for i in range(N+1):
#     print(dp[i])

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
def knapsack(sizes, vals, volume):
    n = len(sizes)

    dp = [[0] * (volume+1) for _ in range(n)] # j is 0...volume

    for i in range(n):
        for j in range(volume+1):
            dp[i][j] = max(dp[i][j], dp[i-1][j])
            if j - sizes[i] >= 0:
                dp[i][j] = max(dp[i][j], dp[i-1][j-sizes[i]] + vals[i])
            dp[i][j] = max(dp[i][j], dp[i-1][j])

    return dp[-1][-1]


if __name__ == "__main__":
    n, volume = [int(x) for x in input().split()]
    sizes, vals = [], []
    for _ in range(n):
        s, v = [int(x) for x in input().split()]
        sizes.append(s)
        vals.append(v)
    print(knapsack(sizes, vals, volume))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
import numpy as np

N, W = map(int, sys.stdin.buffer.readline().split())
   
dp = np.zeros((N+1, W+1), dtype=int)

for i in range(1, N+1):
    w, v = map(int, sys.stdin.buffer.readline().split())
    for j in range(W+1):
        if j-w >= 0:
            dp[i,j] = max(dp[i,j], dp[i-1,j-w]+v)
        dp[i,j] = max(dp[i,j], dp[i-1,j])

# for i in range(N+1):
#     print(dp[i])

print(dp[N,W])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
import numpy as np

N, W = map(int, sys.stdin.buffer.readline().split())
w, v = np.zeros(N+10, dtype=int), np.zeros(N+10, dtype=int)

for i in range(1, N+1):
    w[i], v[i] = map(int, sys.stdin.buffer.readline().split())
   
dp = np.zeros((N+1, W+1), dtype=int)

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

# for i in range(N+1):
#     print(dp[i])

print(dp[N,W])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys

N, W = map(int, sys.stdin.buffer.readline().split())
w, v = [0 for _ in range(N+10)], [0 for _ in range(N+10)]

for i in range(1, N+1):
    w[i], v[i] = map(int, sys.stdin.buffer.readline().split())
   
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-w[i] >= 0:
            dp[i][j] = max(dp[i][j], dp[i-1][j-w[i]]+v[i])
        dp[i][j] = max(dp[i][j], dp[i-1][j])

# for i in range(N+1):
#     print(dp[i])

print(dp[N][W])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, max_weight = map(int, input().split())
WV = np.array([input().split() for _ in range(n)], np.int64)

W, V = zip(*WV)
dp = np.zeros((n + 1, max_weight + 1), np.int64)

# O(NW)
for i in range(n):
    for w in range(1, max_weight + 1):
        if w >= W[i]:
            dp[i + 1, w] = max(dp[i, w], dp[i, w - W[i]] + V[i])
        else:
            dp[i + 1, w] = dp[i, w]

print(dp[n, max_weight])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, max_weight = map(int, input().split())
WV = np.array([input().split() for _ in range(n)], np.int64)

W, V = zip(*WV)
dp = np.zeros((n, max_weight + 1), np.int64)

for i in range(n):
    for w in range(1, max_weight + 1):
        if w >= W[i]:
            dp[i, w] = max(dp[i - 1, w], dp[i - 1, w - W[i]] + V[i])
        else:
            dp[i, w] = dp[i - 1, w]

print(dp[n - 1, max_weight])

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, W = map(int, input().split())
    w = [None for _ in range(N)]
    v = [None for _ in range(N)]
    for i in range(N):
        w[i], v[i] = map(int, input().split())
    dp = [[0 for _ in range(W+1)] for _ in range(N)]
    for i in range(0, N):
        for j in range(W+1):
            if j-w[i] >= 0:
                dp[i][j] = max(dp[i-1][j-w[i]]+v[i], dp[i-1][j])
            else:
                dp[i][j] = dp[i-1][j]
    print(dp[-1][-1])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(n)]
z=[[0]*(w+1) for _ in xrange(n+1)]
for i in xrange(n):
    for j in xrange(w+1):
        z[i+1][j]=z[i][j]
        if a[i][0]<=j:
            z[i+1][j]=max(z[i+1][j],z[i][j-a[i][0]]+a[i][1])
print max(z[-1])

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(n)]
z=[[0]*(w+1)]
for i in xrange(n):
    z.append(z[-1][:])
    for j in xrange(w+1):
        if a[i][0]<=j:
            z[i+1][j]=max(z[i+1][j],z[i][j-a[i][0]]+a[i][1])
print max(z[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, W = map(int, readline().split())
things = []
for i in range(N):
    weight, value = map(int, readline().split())
    things.append((weight, value))


def solve():
    dp = [0] * (W + 1)
    w0, v0 = things[0]
    for i in range(w0, W + 1):
        dp[i] = v0
    for i in range(1, N):
        dn = list(dp)
        weight, value = things[i]
        for j in range(weight, W + 1):
            dn[j] = max(dn[j], dp[j - weight] + value)
        dp = dn
    return dp[W]


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

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()
cost=[[0]*(w+1) for _ in range(n+1)]
for i in range(1,n+1):
    for j in range(w+1):
        if wv[i-1][0] <= j:
            cost[i][j]=max(wv[i-1][1]+cost[i-1][j-wv[i-1][0]],cost[i-1][j])
        else:
            cost[i][j]=cost[i-1][j]
print(cost[n][w])

Submission status: Time Limit Exceeded

Solution code:
n, w = map(int, input().split())
x = []
y = []

for i in range(n):
  a,b = map(int, input().split())
  x.append(b)
  y.append(a)

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

for i in range(n):
  for j in range(w + 1):
    if(j >= y[i]):
      dp[i + 1][j] = max(dp[i][j], dp[i][j - y[i]] + x[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())
lis = [list(map(int, input().split())) for i in range(n)] #lis[count] = [weight, value]

dp = [[0 for i in range(w+1)] for i in range(n+1)] #dp[count][weight] = max_value
for count in range(n):
    item = lis[count]
    for weight in range(w+1):
        if weight < item[0]:
            dp[count+1][weight] = dp[count][weight]
        else:
            dp[count+1][weight] = max( dp[count][weight-item[0]] + item[1], dp[count][weight])

print(dp[n][w])

Submission status: Time Limit Exceeded

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

def main():
    ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

    N, W = ds_in()

    dp = [[0] * (W+1) for i in range(N+1)]
# dp[index][weight]:=valueとしてdp[n]での最大を求める.
    for i in range(N):
    # pre = values.copy()
        w, v = ds_in()  # [weight, value]
        for j in range(W+1):
            if (j-w>=0):
                dp[i+1][j] = max(dp[i][j], dp[i][j-w]+v)
            else:
                dp[i+1][j] = dp[i][j]
    ans = 0
    for i in range(W+1):
        ans = max(ans, dp[N][i])
    print(ans)
    return 0
main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

N, W = ds_in()

dp = [[0] * (W+1) for i in range(N+1)]
# dp[index][weight]:=valueとしてdp[n]での最大を求める.
for i in range(N):
    # pre = values.copy()
    w, v = ds_in()  # [weight, value]
    for j in range(W+1):
        if (j-w>=0):
            dp[i+1][j] = max(dp[i][j], dp[i][j-w]+v)
        else:
            dp[i+1][j] = dp[i][j]
ans = 0
for i in range(W+1):
    ans = max(ans, dp[N][i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np

ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

N, W = ds_in()

dp = np.zeros((N+1, W+1), dtype=int)  # 重さi以下の最大価値
# dp[index][weight]:=valueとしてdp[n]での最大を求める.
for i in range(N):
    # pre = values.copy()
    w, v = ds_in()  # [weight, value]
    for j in range(W+1):
        if (j-w>=0):
            dp[i+1][j] = np.maximum(dp[i][j], dp[i][j-w]+v)
        else:
            dp[i+1][j] = dp[i][j]
        # values[w] = np.maximum(values[wv[0]] + wv[1], values[wv[0]:])

ans = 0
for i in range(W+1):
    ans = max(ans, dp[N][i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

# import os
# path = os.getcwd()
# path = os.path.join(path,"input.txt")
# sys.stdin = open(path)

input = sys.stdin.readline
N,W  = map(int,input().split())
dp = np.zeros((N+1,W+1))
w,v = [],[]
for _ in range(N):
    w_,v_ = map(int,input().split())
    w.append(w_)
    v.append(v_)

for row in range(N):
    dp[row+1,:w[row]] = dp[row,:w[row]]
    for col in range(w[row],W+1):
        dp[row+1,col] = max(v[row]+dp[row,col-w[row]],dp[row,col])

ans = int(max(dp[N]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

# import os
# path = os.getcwd()
# path = os.path.join(path,"input.txt")
# sys.stdin = open(path)

input = sys.stdin.readline
N,W  = map(int,input().split())
dp = np.zeros((N+1,W+1))
w,v = [],[]
for _ in range(N):
    w_,v_ = map(int,input().split())
    w.append(w_)
    v.append(v_)

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

ans = int(max(dp[N]))
print(ans)

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]*100010 for i in range(N+10)]
for i in range(N):
    for now_w in range(W+1):
        if (now_w >= w[i]):
            dp[i+1][now_w] = max(dp[i][now_w-w[i]] + v[i], dp[i][now_w])
        else:
            dp[i+1][now_w] = dp[i][now_w]

print(dp[N][W])

Submission status: Time Limit Exceeded

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

N, W = list(map(int, input().split()))
Z = []
for i in range(N):
    Z.append(list(map(int, input().split())))

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

for i, z in enumerate(Z):
    #print(i)
    for w in range(W+1):
        if w -z[0] >= 0:
            dp[i+1][w] = max(dp[i][w-z[0]]+z[1], dp[i][w])
        else:
            dp[i+1][w] = dp[i][w]
    #print(dp)

print(max(dp[-1]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

def solve():
    n,w = map(int,input().split())
    w_list,v_list = [0]*n,[0]*n
    for i in range(n):
        w_list[i],v_list[i] = map(int,input().split())
    dp = [[0]*(w+5) for i in range(n+5)]
    for i in range(n):
        for j in range(w+1):
            if j-w_list[i]>=0:
                dp[i][j] = max(dp[i-1][j],
                               dp[i-1][j-w_list[i]]+v_list[i])
            else:
                dp[i][j] = dp[i-1][j]
    print(dp[n-1][w])
    return 0

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
# D
n, W = map(int, input().split())
w = []
v = []
for i in range(n):
    ww, vv = map(int, input().split())
    w.append(ww)
    v.append(vv)
    
dp = [[0]*(W+1) for _ in range(n+1)]
for i in range(n):
    for j in range(W+1):
        if j < w[i]:
            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[n][W])

Submission status: Time Limit Exceeded

Solution code:
def solve(cost,weight,n,m):
    dp=[[0]*(m+1) for i in range(n+1)]
    for i in range(1,n+1):
        for j in range(1,m+1):
            if(weight[i-1]>j):
                dp[i][j]=dp[i-1][j]
            else:
                dp[i][j]=max(dp[i-1][j],cost[i-1]+dp[i-1][j-weight[i-1]])

    # for i in range(len(dp)):
    #     print(dp[i])
    return dp[-1][-1]


n,m=map(int,input().split())
cost=[]
weight=[]
for _ in range(n):
    a,b=map(int,input().split())
    weight.append(a)
    cost.append(b)
dp=[[0]*n for i in range(n)]

print(solve(cost,weight,n,m))

Submission status: Time Limit Exceeded

Solution code:
#DP D
def main():
    import sys
    N, limit = map(int, sys.stdin.readline().split())
    L = tuple([tuple(map(int, sys.stdin.readline().split())) for _ in range(N)])
    dp = [[0 for _ in range(limit+1)] for _ in range(N)]
    dp[0][L[0][0]] = L[0][1] #初期値
    for i in range(1, N):
        dp[i] = dp[i-1].copy() #前回をコピー(初)
        w, v = L[i][0], L[i][1]
        dp[i][w] = max(v, dp[i][w])
        for j in range(limit+1-w):
            if dp[i-1][j] >0:
                dp[i][w+j] = max(v+ dp[i-1][j], dp[i][w+j])
            
    
    
    print(max(dp[-1]))



if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def solve(cost,weight,n,m):

    #Base case
    if(m<=0 or n<=0):
        return 0

    res=0

    if(weight[n-1]>m):
        return solve(cost,weight,n-1,m)

    #Recursive case
    op1=solve(cost,weight,n-1,m-weight[n-1])+cost[n-1]
    op2=solve(cost,weight,n-1,m)

    return max(op1,op2)



n,m=map(int,input().split())
cost=[]
weight=[]
for _ in range(n):
    a,b=map(int,input().split())
    weight.append(a)
    cost.append(b)
print(solve(cost,weight,n,m))