코딩테스트/BOJ

[백준][그리디] 15903. 카드 합체 놀이

박소민 2023. 8. 2. 09:44
15903. 카드 합체 놀이
 

15903번: 카드 합체 놀이

첫 번째 줄에 카드의 개수를 나타내는 수 n(2 ≤ n ≤ 1,000)과 카드 합체를 몇 번 하는지를 나타내는 수 m(0 ≤ m ≤ 15×n)이 주어진다. 두 번째 줄에 맨 처음 카드의 상태를 나타내는 n개의 자연수 a1,

www.acmicpc.net

 

  • 내 풀이
    • 만들 수 있는 가장 작은 수
    • 제일 작은 수 두개를 계속 더함 -> heap
import heapq
n,m=map(int,input().split())
score= list(map(int,input().split()))
heapq.heapify(score)

while m>0 and score:
    s1=heapq.heappop(score)
    s2=heapq.heappop(score)

    heapq.heappush(score, s1+s2)
    heapq.heappush(score, s1+s2)
    m-=1

if score:
    print(sum(score))
else:
    print(0)