Skip to content

Commit 90d0b5d

Browse files
committed
[BOJ] #1202.보석도둑 / 골드3 / 90(X)
1 parent 4ca37cb commit 90d0b5d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
input = sys.stdin.readline
3+
import heapq
4+
5+
N, K = map(int, input().split()) # 보석 개수, 가방 개수
6+
jewels = [tuple(map(int, input().split())) for _ in range(N)] # (보석 무게, 보석 가격)
7+
bags = [int(input()) for _ in range(K)] # 가방 무게
8+
9+
# 오름차순 정렬
10+
jewels.sort()
11+
bags.sort()
12+
13+
max_heap = []
14+
total_v = 0
15+
j = 0
16+
17+
for bag in bags:
18+
# 현재 가방에 담을 수 있는 보석을 힙에 추가
19+
while j < N and jewels[j][0] <= bag:
20+
heapq.heappush(max_heap, -jewels[j][1]) # 가격을 음수로 저장
21+
j +=1
22+
# 현재 가방에서 가장 비싼 보석을 선택
23+
if max_heap:
24+
total_v += -heapq.heappop(max_heap) # 음수로 저장한 값 되돌리기
25+
26+
print(total_v)

0 commit comments

Comments
 (0)