Koala - 10기/코딩테스트 준비 스터디

[백준/Python] 15903 카드 합체 놀이

beans3142 2023. 5. 6. 21:59
https://www.acmicpc.net/problem/15903

문제 분석

난이도

실버 1

분류

자료구조, 우선순위 큐

문제

문제 풀이

풀이

우선순위 큐를 이용해 풀 수 있는 문제이다.
앞에서 2개를 pop한 뒤 그 합을 2번 push해주고를 반복하면 된다.

소스코드

from sys import stdin
from heapq import heappop,heappush
input=stdin.readline
n,m=map(int,input().split())
cards=[]
for i in map(int,input().split()):
    heappush(cards,i)

for i in range(m):
    x,y=heappop(cards),heappop(cards)
    x=y=x+y
    heappush(cards,x)
    heappush(cards,y)
print(sum(cards))

후기