๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coding Test/Algorithms

[Baekjoon] 11399. ATM/Python - Silver4

by The Future Engineer, Lucy 2024. 12. 20.
728x90
๋ฐ˜์‘ํ˜•

โ“๋ฌธ์ œ

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

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 32412 KB, ์‹œ๊ฐ„: 36 ms

๋ถ„๋ฅ˜

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ •๋ ฌ

๋ฌธ์ œ ์„ค๋ช…

์ธํ•˜์€ํ–‰์—๋Š” ATM์ด 1๋Œ€๋ฐ–์— ์—†๋‹ค. ์ง€๊ธˆ ์ด ATM์•ž์— N๋ช…์˜ ์‚ฌ๋žŒ๋“ค์ด ์ค„์„ ์„œ์žˆ๋‹ค. ์‚ฌ๋žŒ์€ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ์œผ๋ฉฐ, i๋ฒˆ ์‚ฌ๋žŒ์ด ๋ˆ์„ ์ธ์ถœํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ Pi๋ถ„์ด๋‹ค.

์‚ฌ๋žŒ๋“ค์ด ์ค„์„ ์„œ๋Š” ์ˆœ์„œ์— ๋”ฐ๋ผ์„œ, ๋ˆ์„ ์ธ์ถœํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„์˜ ํ•ฉ์ด ๋‹ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ด 5๋ช…์ด ์žˆ๊ณ , P1 = 3, P2 = 1, P3 = 4, P4 = 3, P5 = 2 ์ธ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž. [1, 2, 3, 4, 5] ์ˆœ์„œ๋กœ ์ค„์„ ์„ ๋‹ค๋ฉด, 1๋ฒˆ ์‚ฌ๋žŒ์€ 3๋ถ„๋งŒ์— ๋ˆ์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋‹ค. 2๋ฒˆ ์‚ฌ๋žŒ์€ 1๋ฒˆ ์‚ฌ๋žŒ์ด ๋ˆ์„ ๋ฝ‘์„ ๋•Œ ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, 3+1 = 4๋ถ„์ด ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค. 3๋ฒˆ ์‚ฌ๋žŒ์€ 1๋ฒˆ, 2๋ฒˆ ์‚ฌ๋žŒ์ด ๋ˆ์„ ๋ฝ‘์„ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด 3+1+4 = 8๋ถ„์ด ํ•„์š”ํ•˜๊ฒŒ ๋œ๋‹ค. 4๋ฒˆ ์‚ฌ๋žŒ์€ 3+1+4+3 = 11๋ถ„, 5๋ฒˆ ์‚ฌ๋žŒ์€ 3+1+4+3+2 = 13๋ถ„์ด ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ์ด ๊ฒฝ์šฐ์— ๊ฐ ์‚ฌ๋žŒ์ด ๋ˆ์„ ์ธ์ถœํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„์˜ ํ•ฉ์€ 3+4+8+11+13 = 39๋ถ„์ด ๋œ๋‹ค.

์ค„์„ [2, 5, 1, 4, 3] ์ˆœ์„œ๋กœ ์ค„์„ ์„œ๋ฉด, 2๋ฒˆ ์‚ฌ๋žŒ์€ 1๋ถ„๋งŒ์—, 5๋ฒˆ ์‚ฌ๋žŒ์€ 1+2 = 3๋ถ„, 1๋ฒˆ ์‚ฌ๋žŒ์€ 1+2+3 = 6๋ถ„, 4๋ฒˆ ์‚ฌ๋žŒ์€ 1+2+3+3 = 9๋ถ„, 3๋ฒˆ ์‚ฌ๋žŒ์€ 1+2+3+3+4 = 13๋ถ„์ด ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ๊ฐ ์‚ฌ๋žŒ์ด ๋ˆ์„ ์ธ์ถœํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„์˜ ํ•ฉ์€ 1+3+6+9+13 = 32๋ถ„์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•๋ณด๋‹ค ๋” ํ•„์š”ํ•œ ์‹œ๊ฐ„์˜ ํ•ฉ์„ ์ตœ์†Œ๋กœ ๋งŒ๋“ค ์ˆ˜๋Š” ์—†๋‹ค.

์ค„์„ ์„œ ์žˆ๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜ N๊ณผ ๊ฐ ์‚ฌ๋žŒ์ด ๋ˆ์„ ์ธ์ถœํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ Pi๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ ์‚ฌ๋žŒ์ด ๋ˆ์„ ์ธ์ถœํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„์˜ ํ•ฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

โœ๐Ÿปํ’€์ด

 ์‹œ๊ฐ„์˜ ํ•ฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ ๋ˆ์„ ์ธ์ถœํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ๋žŒ๋ถ€ํ„ฐ ํ•˜๋ฉด ์ตœ์ข…์ ์œผ๋กœ ๊ทธ ๊ฐ’์ด ํ•ฉ์˜ ์ตœ์†Ÿ๊ฐ’์ด ๋œ๋‹ค.

๐Ÿ’ป์ฝ”๋“œ

import sys

input = sys.stdin.readline

n = int(input())
p = list(map(int, input().split()))
p.sort()

answer = [p[0]]
for i in range(1, n):
    answer.append(answer[i-1] + p[i])

print(sum(answer))

๐Ÿ“ํ›„๊ธฐ

์ฒ˜์Œ์—๋Š” ์ด๊ฑฐ ๊ทธ๋ƒฅ ์ •๋ ฌ๋ฌธ์ œ ์•„๋‹Œ๊ฐ€ ํ–ˆ์ง€๋งŒ ์ตœ์ ์˜ ํ•ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ˆ๊นŒ ๊ทธ๋ฆฌ๋””๋„ ๋งž๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

728x90
๋ฐ˜์‘ํ˜•