목차

컵라면

ps
링크acmicpc.net/…
출처BOJ
문제 번호1781
문제명컵라면
레벨골드 2
분류

그리디

시간복잡도O(nlogn)
인풋사이즈n<=200,000
사용한 언어Python
제출기록65840KB / 768ms
최고기록440ms
해결날짜2021/12/14

풀이

코드

"""Solution code for "BOJ 1781. 컵라면".

- Problem link: https://www.acmicpc.net/problem/1781
- Solution link: http://www.teferi.net/ps/problems/boj/1781


Tags: [Greedy] [Priority queue]
"""

import heapq
import sys


def main():
    N = int(sys.stdin.readline())
    problems = []
    for _ in range(N):
        problem = [int(x) for x in sys.stdin.readline().split()]
        problems.append(problem)

    problems.sort()
    heap = []
    answer = 0
    max_deadline = problems[-1][0]
    for deadline in range(max_deadline, 0, -1):
        while problems and problems[-1][0] >= deadline:
            heapq.heappush(heap, -problems.pop()[1])
        if heap:
            answer += -heapq.heappop(heap)

    print(answer)


if __name__ == '__main__':
    main()