forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.py
More file actions
23 lines (18 loc) · 511 Bytes
/
4.py
File metadata and controls
23 lines (18 loc) · 511 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import heapq
n = int(input())
# 힙(Heap)에 초기 카드 묶음을 모두 삽입
heap = []
for i in range(n):
data = int(input())
heapq.heappush(heap, data)
result = 0
# 힙(Heap)에 원소가 1개 남을 때까지
while len(heap) != 1:
# 가장 작은 2개의 카드 묶음 꺼내기
one = heapq.heappop(heap)
two = heapq.heappop(heap)
# 카드 묶음을 합쳐서 다시 삽입
sum_value = one + two
result += sum_value
heapq.heappush(heap, sum_value)
print(result)