문제
나의 풀이
1. 실패: 세밀하게 계산 못한 실수..
def card_sort(arr):
if len(arr) == 1:
return 0
check = arr[0]
tail = arr[1:]
left = [x for x in tail if x <= check]
right = [x for x in tail if x > check]
return card_sort(left) + [check] + card_sort(right)
n = int(input())
array = [int(input()) for _ in range(n)]
card = card_sort(array)
answer = card[0] * (n - 1)
for i in range(1, n):
answer += card[i] * (n - i)
print(answer)
2. 힙
import heapq
q = []
n = int(input())
for i in range(n):
heapq.heappush(q, (int(input())))
result = 0
while len(q) > 1:
now1 = heapq.heappop(q)
now2 = heapq.heappop(q)
heapq.heappush(q, (now1 + now2))
result += now1 + now2
print(result)