Skip to content

Commit 28b873c

Browse files
committed
[BOJ] #1202. 보석도둑 / 골드2 / 그리디 / 60분 / 실패
1 parent 6665495 commit 28b873c

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
import heapq
3+
4+
input = sys.stdin.readline
5+
6+
# 입력 받기
7+
N, K = map(int, input().split()) # N: 보석 개수, K: 가방 개수
8+
jewelry = [] # (무게, 가격)
9+
bags = [] # 가방의 최대 무게
10+
11+
for _ in range(N):
12+
M, V = map(int, input().split())
13+
jewelry.append((M, V))
14+
15+
for _ in range(K):
16+
bags.append(int(input()))
17+
18+
# 보석을 무게 기준으로 정렬 (무게가 작은 순)
19+
jewelry.sort()
20+
21+
# 가방을 무게 기준으로 정렬 (무게가 작은 순)
22+
bags.sort()
23+
24+
# 우선순위 큐 (최대 힙)
25+
max_heap = []
26+
result = 0
27+
idx = 0
28+
29+
# 가방을 하나씩 처리
30+
for bag in bags:
31+
# 현재 가방이 수용할 수 있는 보석을 모두 추가 (무게 기준 정렬된 상태)
32+
while idx < N and jewelry[idx][0] <= bag:
33+
heapq.heappush(max_heap, -jewelry[idx][1]) # 최대 힙을 위해 음수 저장
34+
idx += 1
35+
36+
# 가장 가치가 높은 보석을 선택
37+
if max_heap:
38+
result += -heapq.heappop(max_heap)
39+
40+
print(result)

0 commit comments

Comments
 (0)