| ps | |
|---|---|
| 링크 | acmicpc.net/… |
| 출처 | BOJ |
| 문제 번호 | 1715 |
| 문제명 | 카드 정렬하기 |
| 레벨 | 골드 4 |
| 분류 |
그리디, 우선순위큐 |
| 시간복잡도 | O(nlogn) |
| 인풋사이즈 | n<=100,000 |
| 사용한 언어 | Python |
| 제출기록 | 33692KB / 196ms |
| 최고기록 | 124ms |
| 해결날짜 | 2021/12/13 |
| 태그 | |
"""Solution code for "BOJ 1715. 카드 정렬하기".
- Problem link: https://www.acmicpc.net/problem/1715
- Solution link: http://www.teferi.net/ps/problems/boj/1715
Tags: [Priority queue]
"""
import heapq
import sys
def main():
N = int(sys.stdin.readline())
size_heap = [int(sys.stdin.readline()) for _ in range(N)]
heapq.heapify(size_heap)
answer = 0
for _ in range(N - 1):
s1 = heapq.heappop(size_heap)
s2 = heapq.heappop(size_heap)
answer += s1 + s2
heapq.heappush(size_heap, s1 + s2)
print(answer)
if __name__ == '__main__':
main()