해야만 한다
[Python] 백준 1715. 카드 정렬하기 본문
https://www.acmicpc.net/problem/1715
풀이
힙큐를 이용하여 최소 2개를 pop(heappop)하고 그 값을 누적하면서 힙큐에 다시 넣는다.
코드
import heapq, sys
input = sys.stdin.readline
n = int(input())
if n == 1:
print(0)
exit()
pq = []
for i in range(n):
heapq.heappush(pq,int(input()))
res = 0
while len(pq) >= 2:
temp = 0
temp += heapq.heappop(pq)
temp += heapq.heappop(pq)
res += temp
heapq.heappush(pq, temp)
print(res)
'Python > Algorithm' 카테고리의 다른 글
[Python] 백준 15686. 치킨배달 (0) | 2023.10.18 |
---|---|
[Python] 백준 12919. A와 B 2 (0) | 2023.10.18 |
[Python] 백준 1103. 게임 (0) | 2023.10.18 |
[Python] 백준 16953. A → B (0) | 2023.10.18 |
[Python] 백준 1238. 파티 (0) | 2023.10.18 |