import sys
input = sys.stdin.readline
import heapq
n, m = map(int, input().split())
cards = [i for i in map(int, input().split())]
heapq.heapify(cards)
for _ in range(m):
x = heapq.heappop(cards)
y = heapq.heappop(cards)
heapq.heappush(cards, x+y)
heapq.heappush(cards, x+y)
print(sum(cards))
우선 순위 큐를 이용하면 쉽게 풀 수 있는 문제였다!
'Algorithm > BaekJoon' 카테고리의 다른 글
[백준/Python] 2573번 - 빙산 (0) | 2023.09.06 |
---|---|
[백준/Python] 2170 - 선 긋기 (0) | 2023.09.02 |
[백준/Python] 1874번 - 스택 수열 (0) | 2023.09.02 |
[백준/Python] 2493번 - 탑 (0) | 2023.09.01 |
[백준/Python] 10828번 - 스택 (1) | 2023.09.01 |