Skip to content

Commit 97e3fce

Browse files
committed
[BOJ] 1715 카드 정렬하기 (G4)
1 parent d21685c commit 97e3fce

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

심수연/2주차/260106.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# https://www.acmicpc.net/problem/1715
2+
3+
import sys
4+
import heapq
5+
input = sys.stdin.readline
6+
7+
N = int(input())
8+
9+
# 우선순위 큐 (두 묶음의 합이 최소가 될 수 있도록 -> min heap)
10+
hq = []
11+
12+
for i in range(N):
13+
heapq.heappush(hq, int(input()))
14+
15+
answer = 0
16+
17+
if N == 1:
18+
print(answer)
19+
20+
else:
21+
while len(hq) > 1:
22+
A = heapq.heappop(hq) # 가장 작은 수 2개 뽑아 더하기
23+
B = heapq.heappop(hq)
24+
answer += A + B
25+
heapq.heappush(hq, A + B) # A+B값 hq에 다시 넣기
26+
27+
print(answer)

0 commit comments

Comments
 (0)