프로그래밍/Algorithm

백준 카드 합체 놀이 파이썬

모지사바하 2021. 2. 24. 22:05

www.acmicpc.net/problem/15903

 

15903번: 카드 합체 놀이

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

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import heapq
 
n, m = map(int, input().split())
= list(map(int, input().split()))
heapq.heapify(a)
 
for i in range(m):
  first = heapq.heappop(a)
  second = heapq.heappop(a)
  heapq.heappush(a, first+second)
  heapq.heappush(a, first+second)
 
sum(a)
 
 
cs

너무 순한맛이라 설명생략.