chyam

[백준] 15903,python - 카드 합체 놀이 본문

백준

[백준] 15903,python - 카드 합체 놀이

chyam_eun 2025. 9. 10. 16:35

https://www.acmicpc.net/problem/15903

import sys,heapq
input= sys.stdin.readline

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

for i in range(m):
    x = heapq.heappop(heap) # 가장 작은수
    y = heapq.heappop(heap) # 다음으로 작은수
    heapq.heappush(heap,x+y) # 합을 두번 추가해줌
    heapq.heappush(heap,x+y)

print(sum(heap))