Skip to content

Commit 6a587a4

Browse files
committed
AC(4708ms): #knapsack #dp(top-down) O(NK)
1 parent 436a8c5 commit 6a587a4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 평범한 배낭
2+
3+
from functools import cache
4+
import sys
5+
6+
7+
N, K = map(int, sys.stdin.readline().split())
8+
W = [0] * N
9+
V = [0] * N
10+
for i in range(N):
11+
W[i], V[i] = map(int, sys.stdin.readline().split())
12+
13+
14+
@cache
15+
def knapsack(i: int = 0, k: int = K) -> int:
16+
if k < 0:
17+
return -sys.maxsize
18+
if i == N:
19+
return 0
20+
return max(
21+
knapsack(i+1, k),
22+
knapsack(i+1, k-W[i]) + V[i],
23+
)
24+
25+
print(knapsack())

0 commit comments

Comments
 (0)