We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d21685c commit 97e3fceCopy full SHA for 97e3fce
1 file changed
심수연/2주차/260106.py
@@ -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
0 commit comments