1715 - 카드 정렬하기
info
- 문제 보기: 1715 - 카드 정렬하기
- 소요 시간: 17분
- 풀이 언어:
python - 체감 난이도: 2️⃣
- 리뷰 횟수: ✅
풀이 키워드
스포주의
자료구조 힙
풀이 코드
info
- 메모리: 36292 KB
- 시간: 124 ms
import sys
import heapq
input = sys.stdin.readline
n = 0
heap = []
def solution():
global n, heap
n = int(input())
for i in range(n):
heapq.heappush(heap, int(input()))
ans = 0
# edge case
if n == 1:
print(0) # no need to compare
return
# else
while True:
lhs = heapq.heappop(heap)
try:
rhs = heapq.heappop(heap)
except:
break
# merge
k = lhs + rhs
heapq.heappush(heap, k)
ans += k
print(ans)
if __name__ == '__main__':
solution()