나의 개발일지

[백준] 15903 카드 합체 놀이 [Python, 파이썬] 본문

백준

[백준] 15903 카드 합체 놀이 [Python, 파이썬]

YoonJuHan 2023. 11. 24. 09:12
  • 문제 : https://www.acmicpc.net/problem/15903
  • 🔑 그리디, 우선 순위 큐
    • 카드들을 heap에 담고 가장 작은 카드를 두 개 뽑아낸다.
    • 뽑아낸 카드 두 개를 합쳐서 두 번 집어넣는다.
    • 이 과정을 m번 반복하고 힙에 남은 카드들의 전체 합계를 구한다.
    • 끝 ✨

 

from heapq import heapify, heappop, heappush

n, m = map(int, input().split())
card = list(map(int, input().split()))
heapify(card)

for _ in range(m):
    x = heappop(card)
    y = heappop(card)
    heappush(card, x+y)
    heappush(card, x+y)

print(sum(card))
Comments