우선순위 큐를 이용해 풀 수 있는 문제이다. 앞에서 2개를 pop한 뒤 그 합을 2번 push해주고를 반복하면 된다.
소스코드
from sys import stdin
from heapq import heappop,heappush
input=stdin.readline
n,m=map(int,input().split())
cards=[]
for i in map(int,input().split()):
heappush(cards,i)
for i in range(m):
x,y=heappop(cards),heappop(cards)
x=y=x+y
heappush(cards,x)
heappush(cards,y)
print(sum(cards))