동캄의 코딩도장

백준 14241 [슬라임 합치기] 파이썬 본문

코테/BOJ

백준 14241 [슬라임 합치기] 파이썬

동 캄 2022. 9. 4. 23:51

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

 

14241번: 슬라임 합치기

영선이와 효빈이는 슬라임을 합치는 게임을 하고 있다. 두 사람은 두 슬라임을 골라서 하나로 합쳐야 한다. 게임은 슬라임이 하나 남았을 때 끝난다. 모든 슬라임은 양수 크기를 가지고 있다. 두

www.acmicpc.net

#백준 14241
import sys
import heapq
n=int(sys.stdin.readline())
lst=list(map(int,sys.stdin.readline().split()))
heapq.heapify(lst)
ans=0
for i in range(n-1):
    x=heapq.heappop(lst)
    y=heapq.heappop(lst)
    ans+=x*y
    heapq.heappush(lst,x+y)
print(ans)

그리디하게 해결하면 된다.