Skip to content

Commit 2906084

Browse files
committed
[BOJ] 1202 보석 도둑 (G2)
1 parent ffe195e commit 2906084

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

심수연/8주차/260218.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://www.acmicpc.net/problem/1202
2+
3+
import sys
4+
input = sys.stdin.readline
5+
import heapq
6+
7+
N, K = map(int, input().split())
8+
gemstone = []
9+
10+
for i in range(N):
11+
heapq.heappush(gemstone, list(map(int, input().split()))) # min heap - 가장 가벼운 보석이 gemstone[0]
12+
13+
bags = []
14+
for i in range(K):
15+
bags.append(int(input()))
16+
bags.sort() # 가방은 작은 것 부터 정렬
17+
18+
# 가방 작은 것부터, 가방에 들어가면서 가장 비싼 보석을 넣는 게 핵심
19+
20+
temp = []
21+
answer = 0
22+
for bag in bags:
23+
while gemstone and gemstone[0][0] <= bag: # 가장 가벼운 보석(gemstone[0])이 가방에 들어가면, (보석 동날 때까지 쫙 돌리기)
24+
heapq.heappush(temp, -heapq.heappop(gemstone)[1]) # 뽑아내서 temp에다가 넣기 (가장 비싼 보석 순으로 - max heap)
25+
26+
if temp: # temp가 있으면
27+
answer -= heapq.heappop(temp) # 비싼 보석 (max heap 으로 넣었으니까 다시 음수로 빼내서 원복) , bag마다 더해줘야하므로 -=
28+
elif not gemstone:
29+
break
30+
31+
print(answer)

0 commit comments

Comments
 (0)