Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[백준] 1715 카드 정렬하기 [Python, 파이썬] 본문
- 문제 : https://www.acmicpc.net/problem/1715
- 🔑 최소 힙
- 힙에서 가장 작은 수 두 개를 뽑는다. heappop 두 번
- 뽑아낸 두 수를 더해서 힙에 다시 넣고 합한 값만큼 정답을 올려준다.
from heapq import heappop, heappush
import sys
input = sys.stdin.readline
n = int(input())
heap = []
for _ in range(n):
heappush(heap, int(input()))
answer = 0
while len(heap) >= 2:
x = heappop(heap) + heappop(heap)
heappush(heap, x)
answer += x
print(answer)
'백준' 카테고리의 다른 글
[백준] 1922 네트워크 연결 [Python, 파이썬] (1) | 2024.01.03 |
---|---|
[백준] 1747 소수 & 팰린드롬 [Python, 파이썬] (0) | 2024.01.03 |
[백준] 1927 최소 힙 [Python, 파이썬] (0) | 2024.01.03 |
[백준] 19496 큰 수 만들기 [Python, 파이썬] (0) | 2023.12.24 |
[Python] schedule 모듈 (0) | 2023.12.18 |
Comments