본문 바로가기
Algorithm/Greedy

[백준] 1202번 보석 도둑

by 등촌동 꼬북이 2020. 10. 20.

후.. 우선순위큐를 쓰지 않으면 시초가 오지게 뜬다..

 

시초 안뜨게 하려면 우순큐로 풀어야됨

 

우순큐 시간복잡도 개꿀.. 

 

import sys
import heapq

def greedy(jewel, bagData):
    ans = 0
    pickedJewel = []
    for _ in range(K):
        bagSize = heapq.heappop(bagData)
        while jewel and bagSize >= jewel[0][0]:
            w, v = heapq.heappop(jewel)
            heapq.heappush(pickedJewel, -v)
        if pickedJewel:
            ans -= heapq.heappop(pickedJewel)
        elif not jewel:
            break
    return ans

N, K = map(int, sys.stdin.readline().split())
M= []
Bag= []

for _ in range(N):
    heapq.heappush(M, list(map(int, sys.stdin.readline().split())))

for _ in range(K):
    heapq.heappush(Bag, int(sys.stdin.readline()))

print(greedy(M, Bag))

'Algorithm > Greedy' 카테고리의 다른 글

[백준] 14241번 슬라임 합치기  (0) 2020.10.20
[백준] 1946번 신입 사원  (0) 2020.10.20
[백준] 11509번 풍선 맞추기  (0) 2020.10.19
[백준] 1931번 회의실배정  (0) 2020.10.16
[백준] 14916 거스름돈  (0) 2020.10.16

댓글