목차

카드 정렬하기

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()