ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

2109๋ฒˆ: ์ˆœํšŒ๊ฐ•์—ฐ

ํ•œ ์ €๋ช…ํ•œ ํ•™์ž์—๊ฒŒ n(0≤n≤10,000)๊ฐœ์˜ ๋Œ€ํ•™์—์„œ ๊ฐ•์—ฐ ์š”์ฒญ์„ ํ•ด ์™”๋‹ค. ๊ฐ ๋Œ€ํ•™์—์„œ๋Š” d(1≤d≤10,000)์ผ ์•ˆ์— ์™€์„œ ๊ฐ•์—ฐ์„ ํ•ด ์ฃผ๋ฉด p(1≤p≤10,000)๋งŒํผ์˜ ๊ฐ•์—ฐ๋ฃŒ๋ฅผ ์ง€๋ถˆํ•˜๊ฒ ๋‹ค๊ณ  ์•Œ๋ ค์™”๋‹ค. ๊ฐ ๋Œ€ํ•™์—๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๊ฐ•์—ฐ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์— ๋”ฐ๋ผ, ์‹œ๊ฐ„๊ณผ ์ผ์ˆ˜๊ฐ€ ์ •๋ณด๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” `heapq`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž…๋ ฅ๋œ ์ •๋ณด(๊ฐ•์˜๋ฃŒ, ์ผ์ •)๋ฅผ ์ผ์ • ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , heap์— ์žˆ๋Š” ๊ฐ’์˜ ์ˆ˜๋ฅผ ์ผ์ •์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
import heapq


if __name__ == '__main__':
    PAY, DAY = 0, 1
    answer = 0
    n = int(stdin.readline())
    info = [list(map(int, stdin.readline().split())) for _ in range(n)]
    info.sort(key=lambda x: x[1])

    heap = []

    for i in range(n):
        answer += info[i][PAY]
        heapq.heappush(heap, info[i][PAY])

        if len(heap) > info[i][DAY]:
            answer -= heapq.heappop(heap)

    print(answer)
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€