๋ฌธ์
https://www.acmicpc.net/problem/15903
ํ์ด
# ๋ฐฑ์ค 15903๋ฒ ๋ฌธ์ - ์นด๋ ํฉ์ฒด ๋์ด
import sys
import heapq
input = sys.stdin.readline
n, m = map(int, input().split())
cards = list(map(int, input().split()))
heapq.heapify(cards)
for i in range(m):
value = heapq.heappop(cards) + heapq.heappop(cards)
for _ in range(2):
heapq.heappush(cards, value)
answer = 0
while cards:
answer += heapq.heappop(cards)
print(answer)
Heap ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํด ์ฐ์ ์์ ํ๋ฅผ ๋ง๋ ๋ค.
ํ์ด์ฌ์ Heap์ ์ต์ํ์ด๋ฏ๋ก ๋ฐ๋ก ๊ตฌํ์ ํด์ค ํ์๋ ์๋ค. [์ต๋ ํ์ ์ฌ์ฉํ๋ ค๋ฉด -1 ์ ์ฌ์ฉํด ์ ๋ ฌ]
ํ์์ pop์ 2๋ฒ ํ๊ณ ๋ํ ๊ฐ๋ค์ ๋ค์ ํ์ 2๋ฒ ๋ฃ์ด์ค๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Silver2] 2477๋ฒ_์ฐธ์ธ๋ฐญ (0) | 2023.06.16 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Silver2] 3085๋ฒ_์ฌํ ๊ฒ์ (0) | 2023.06.14 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver3] 2346๋ฒ_ํ์ ํฐ๋จ๋ฆฌ๊ธฐ (0) | 2023.05.29 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 1339๋ฒ_๋จ์ด ์ํ (0) | 2023.05.15 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 13219๋ฒ_Trains (0) | 2023.05.14 |