Skip to content

Commit 00eff6b

Browse files
committed
[Gold V] Title: 평범한 배낭, Time: 5308 ms, Memory: 135400 KB -BaekjoonHub
1 parent 561aad3 commit 00eff6b

4 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Gold V] 평범한 배낭 - 12865
2+
3+
[문제 링크](https://www.acmicpc.net/problem/12865)
4+
5+
### 성능 요약
6+
7+
메모리: 135400 KB, 시간: 5308 ms
8+
9+
### 분류
10+
11+
다이나믹 프로그래밍, 배낭 문제
12+
13+
### 제출 일자
14+
15+
2026년 4월 1일 22:55:16
16+
17+
### 문제 설명
18+
19+
<p>이 문제는 아주 평범한 배낭에 관한 문제이다.</p>
20+
21+
<p><span style="line-height:1.6em">한 달 후면 국가의 부름을 받게 되는 준서는 여행을 가려고 한다. 세상과의 단절을 슬퍼하며 최대한 즐기기 위한 여행이기 때문에, 가지고 다닐 배낭 또한 최대한 가치 있게 싸려고 한다.</span></p>
22+
23+
<p><span style="line-height:1.6em">준서가 여행에 필요하다고 생각하는 N개의 물건이 있다. 각 물건은 무게 W와 가치 V를 가지는데, 해당 물건을 배낭에 넣어서 가면 준서가 V만큼 즐길 수 있다. 아직 행군을 해본 적이 없는 준서는 최대 K만큼의 무게만을 넣을 수 있는 배낭만 들고 다닐 수 있다. 준서가 최대한 즐거운 여행을 하기 위해 배낭에 넣을 수 있는 물건들의 가치의 최댓값을 알려주자.</span></p>
24+
25+
### 입력
26+
27+
<p>첫 줄에 물품의 수 N(1 ≤ N ≤ 100)과 준서가 버틸 수 있는 무게 K(1 ≤ K ≤ 100,000)가 주어진다. 두 번째 줄부터 N개의 줄에 거쳐 각 물건의 무게 W(1 ≤ W ≤ 100,000)와 해당 물건의 가치 V(0 ≤ V ≤ 1,000)가 주어진다.</p>
28+
29+
<p>입력으로 주어지는 모든 수는 정수이다.</p>
30+
31+
### 출력
32+
33+
<p>한 줄에 배낭에 넣을 수 있는 물건들의 가치합의 최댓값을 출력한다.</p>
34+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4 7
2+
6 13
3+
4 8
4+
3 6
5+
5 12
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
14
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
3+
4+
n_items, max_weight = map(int, sys.stdin.readline().split())
5+
items = []
6+
for _ in range(n_items):
7+
weight, value = map(int, sys.stdin.readline().split())
8+
items.append((weight, value))
9+
10+
11+
cache = [[-1 for _ in range(max_weight + 1)] for _ in range(n_items)]
12+
13+
14+
def dp(i, total_weight):
15+
if i == n_items:
16+
return 0
17+
if cache[i][total_weight] == -1:
18+
weight, value = items[i]
19+
if total_weight + weight > max_weight:
20+
cache[i][total_weight] = dp(i + 1, total_weight)
21+
else:
22+
cache[i][total_weight] = max(
23+
dp(i + 1, total_weight),
24+
value + dp(i + 1, total_weight + weight),
25+
)
26+
return cache[i][total_weight]
27+
28+
29+
print(dp(0, 0))

0 commit comments

Comments
 (0)