๐Ÿ—๏ธ Algorithm/๐ŸŸฉ ๋ฐฑ์ค€

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold4] 1715๋ฒˆ_์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ

Dbswnstjd 2023. 3. 28. 00:18

๋ฌธ์ œ

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

 

1715๋ฒˆ: ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ

์ •๋ ฌ๋œ ๋‘ ๋ฌถ์Œ์˜ ์ˆซ์ž ์นด๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. ๊ฐ ๋ฌถ์Œ์˜ ์นด๋“œ์˜ ์ˆ˜๋ฅผ A, B๋ผ ํ•˜๋ฉด ๋ณดํ†ต ๋‘ ๋ฌถ์Œ์„ ํ•ฉ์ณ์„œ ํ•˜๋‚˜๋กœ ๋งŒ๋“œ๋Š” ๋ฐ์—๋Š” A+B ๋ฒˆ์˜ ๋น„๊ต๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผํ…Œ๋ฉด, 20์žฅ์˜ ์ˆซ์ž ์นด๋“œ ๋ฌถ์Œ๊ณผ 30์žฅ

www.acmicpc.net

ํ’€์ด

# ๋ฐฑ์ค€ 1715๋ฒˆ ๋ฌธ์ œ - ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ
import heapq
import sys
input = sys.stdin.readline

n = int(input())
cards = []
result = 0

for i in range(n):
    heapq.heappush(cards, int(input()))

if len(cards) == 1:
    print(0)
else:
    while len(cards) > 1:
        plus = heapq.heappop(cards) + heapq.heappop(cards)
        result += plus
        heapq.heappush(cards, plus)
    
    print(result)

ํž™ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€๋ฉด ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ด๋‹ค.

ํž™ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ณด์ง€ ์•Š์•„์„œ ์ฒ˜์Œ์— ์ƒ๊ฐํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค.

ํž™ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๋Š” ๊ธ€์„ ์“ฐ๋„๋ก ํ•ด์•ผ๊ฒ ๋‹ค.