[BOJ] 15903 카드합체놀이 (python)

Baedonguri·2022년 5월 7일
0
post-thumbnail

https://www.acmicpc.net/problem/15903

제출 코드

프로그래머스의 더맵게 문제와 비슷한 유형의 문제이다.
최소힙을 이용해 힙에서 가장 작은 값 두개를 빼준 뒤
그 합을 구하고, 다시 힙에 넣어준다.

from sys import stdin
from heapq import heapify, heappop, heappush

input = stdin.readline
n, m = map(int, input().split())
arr = list(map(int, input().split()))

heapify(arr)

for i in range(m):
    hap = heappop(arr) + heappop(arr)
    for j in range(2): 
        heappush(arr, hap)
print(sum(arr))
profile
Software Engineer

1개의 댓글

comment-user-thumbnail
2022년 12월 15일

잘 봤습니다 ㅎㅎ 감사해요 :)

답글 달기