Skip to content

Commit 520f5fd

Browse files
authored
Update 4.py
1 parent 05b78ee commit 520f5fd

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

14/4.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import heapq
2+
23
n = int(input())
4+
5+
# 힙(Heap) 자료구조에 초기 카드 묶음을 삽입
36
heap = []
47
for i in range(n):
58
data = int(input())
69
heapq.heappush(heap, data)
10+
711
result = 0
12+
13+
# 힙(Heap)에 원소가 1개 남을 때까지
814
while len(heap) != 1:
15+
# 가장 작은 2개의 카드 묶음 꺼내기
916
one = heapq.heappop(heap)
1017
two = heapq.heappop(heap)
18+
# 카드 묶음을 합쳐서 다시 삽입
1119
sum_value = one + two
1220
result += sum_value
1321
heapq.heappush(heap, sum_value)
22+
1423
print(result)

0 commit comments

Comments
 (0)