백분 1202번 보석 도둑

highway92·2021년 10월 11일
0

백준

목록 보기
21/27

문제출처 : https://www.acmicpc.net/problem/1202

풀이과정

  1. 입력을 받은 후 bag과 gem을 정렬해준다.

  2. w 보다 작은 경우 heappush 해준다.

  3. for 문이 끝나면 price를 반환한다.


import sys
import heapq

input = sys.stdin.readline

n, k =map(int,input().split())
gem = []
bag = []
price = 0
for _ in range(n):
    gem.append(list(map(int,input().split())))

for _ in range(k):
    bag.append(int(input()))

bag = sorted(bag)
gem = sorted(gem)

temp = []
for w in bag:
    while gem and gem[0][0] <= w:
        heapq.heappush(temp, -gem[0][1])
        heapq.heappop(gem)

    if temp:
        price += heapq.heappop(temp)

print(-price)
profile
웹 개발자로 활동하고 있습니다.

0개의 댓글