728x90
https://www.acmicpc.net/problem/1715
1715번: 카드 정렬하기
정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장
www.acmicpc.net
import sys
import heapq
input = sys.stdin.readline
heap = []
n = int(input())
for _ in range(n):
heapq.heappush(heap, int(input()))
answer = 0
while len(heap)!=1:
f = heapq.heappop(heap)
s = heapq.heappop(heap)
sum = f + s
answer += sum
heapq.heappush(heap, sum)
print(answer)
728x90
'Algorithm > 백준' 카테고리의 다른 글
[백준] 10872 팩토리얼 (0) | 2023.08.08 |
---|---|
[백준] 1927 최소 힙 (0) | 2023.07.11 |
[백준] 11279 최대 힙 (0) | 2023.07.11 |
[백준] 9375 패션왕 신해빈 (0) | 2023.07.11 |
[백준] 1264 카드2 (0) | 2023.07.06 |