<๊ทธ๋ฆฌ๋> ๋ฐฑ์ค 11508๋ฒ. 2+1์ธ์ผ (์ค๋ฒ 4)
https://www.acmicpc.net/problem/11508
11508๋ฒ: 2+1 ์ธ์ผ
KSG ํธ์์ ์์๋ ๊ณผ์ผ์ฐ์ , ๋๋งํน์๊ตฌ๋ฅดํธ ๋ฑ์ ์ ์ ํ์ '2+1 ์ธ์ผ'ํ๋ ํ์ฌ๋ฅผ ํ๊ณ ์์ต๋๋ค. KSG ํธ์์ ์์ ์ ์ ํ 3๊ฐ๋ฅผ ํ ๋ฒ์ ์ฐ๋ค๋ฉด ๊ทธ์ค์์ ๊ฐ์ฅ ์ผ ๊ฒ์ ๋ฌด๋ฃ๋ก ์ง๋ถํ๊ณ ๋๋จธ์ง ๋
www.acmicpc.net
1์ฐจ ์๋ :
import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
cost = []
total = 0
for _ in range(n):
cost.append(int(input()))
# 2 3 4 4 6 9 10
# 10 9 6 4 4 3 2
while cost :
cost.sort(reverse=True)
if len(cost) < 3:
total += sum(cost)
break
total += cost.pop(0)
cost.pop()
total += cost.pop()
print(total)
"""
7
10
9
6
4
4
3
2
"""
์๊ฐ ์ด๊ณผ๊ฐ ๋ด๋ค. ๋ฐ๋ณต๋ฌธ ์์์ ๊ณ์ํด์ sortํด์ค์ ๊ทธ๋ฐ ๊ฒ ๊ฐ๋ค..
๋ค์ ๋ณด๋๊น ์๋ชป๋ ์ฝ๋์ธ ๋ฏ. ๋ฌธ์ ์ ์ ํ ์์๋ฅผ ์ฝ๊ณ ๋ ํท๊ฐ๋ ธ๋ค. ๋ฌธ์ ์์ 10,9,6,4,4,3,2๋ฅผ (10, 3, 2), (4, 6, 4), (9)๋ก ๋ฌถ์ด 13+10+9=32์์ ์ง๋ถํ๋ ๊ฒ์ผ๋ก ์ ํ ์์ด์ ์ด๋ ๊ฒ ๋ฌถ์ด๋๋ก ๊ตฌํํ์๋๋ฐ,
๋ค์ ์๊ฐํด๋ณด๋ 3๊ฐ์ ์ ํ ์ค ๊ฐ์ฅ ์ผ ์ ํ์ด ๋ฌด๋ฃ์ด๋ฏ๋ก ์ด 3๋ฒ์งธ ์ ํ์ด ์ต๋ํ ๋น์ธ์ผํ๋ค.
๊ทธ๋ผ (10,9,6) (4,4,3) (2) =>19+8+2 = 29์์ผ๋ก ์ด ๋ฐฉ๋ฒ์ด ๋ ์ต์๋น์ฉ์ด๋ค.
๋ญ์ง.. ๋ฌธ์ ๊ฐ ์๋ชป๋์ ํ๋ฅ ๋ณด๋ค ๋ด ์๊ฐ์ด ํ๋ ธ์ ํ๋ฅ ์ด ๋ ํด๊ฑฐ ๊ฐ์๋ฐ ์๋ฌธ์ด๋ค...
๋ฌดํผ ๋ค์ ์๊ฐํ ๋ฐฉ์๋๋ก ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์๋๋ฐ,
import sys
input = sys.stdin.readline
n = int(input())
cost = []
# 10 9 4 4 6 3 2
for _ in range(n):
cost.append(int(input()))
total = sum(cost)
cost.sort(reverse=True)
2
for i in range(n):
if i %3==2: #์ธ๋ฑ์ค๋ 0๋ถํฐ ์์ํ๋ฏ๋ก ๋๋จธ์ง 2๊ฐ 3,6,9..๋ฒ์งธ๋ฅผ ๋ปํจ
total -= cost[i]
print(total)
"""
7
10
9
6
4
4
3
2
"""
์ ๋ต์ด๋ค!
๋ฌธ์ ์์๋ฅผ ๋ฃ์ด๋ดค๋๋ฐ, 29์ ๋์จ๋ค!