We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 436a8c5 commit 6a587a4Copy full SHA for 6a587a4
2주차 Knapsack/김동주/extra/boj_12865-top_down.py
@@ -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