Skip to content

Commit 5a20a19

Browse files
committed
[BOJ] #12865.평범한 배낭 / 골드5 / 85(∆)
1 parent 5c62512 commit 5a20a19

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N, K = map(int, input().split()) # 물품의 개수, 버틸 수 있는 무게
5+
6+
items = []
7+
for _ in range(N):
8+
W, V = map(int, input().split())
9+
items.append((W, V)) # (물건의 개수, 물건의 가치)
10+
11+
def backpack(N, K, items):
12+
dp = [0] * (K+1) # 배낭 크기만큼 DP 테이블 초기화
13+
14+
for weight, value in items:
15+
for w in range(K, weight-1, -1): # 역순으로 반복
16+
dp[w] = max(dp[w], dp[w-weight] + value)
17+
18+
return dp[K]
19+
20+
print(backpack(N, K, items))

0 commit comments

Comments
 (0)