Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

해야만 한다

[Python] 백준 1715. 카드 정렬하기 본문

Python/Algorithm

[Python] 백준 1715. 카드 정렬하기

쥬링999 2023. 10. 18. 14:14

 

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