์ฒด๊ฐ Level : โ
โ
โ
Review: ๋ธ๋ฃจํฌํฌ์ค๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ฐ์ ธ๋ณด๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. (์์ ํ์) |
๐ก์ ํ๋ ์๊ฐ ์์ N์ฅ์ ์นด๋ ์ค์์ 3์ฅ์ ์นด๋๋ฅผ ๊ณจ๋ผ์ผ ํ๋ค. ๋ธ๋์ญ ๋ณํ ๊ฒ์์ด๊ธฐ ๋๋ฌธ์, ํ๋ ์ด์ด๊ฐ ๊ณ ๋ฅธ ์นด๋์ ํฉ์ M์ ๋์ง ์์ผ๋ฉด์ M๊ณผ ์ต๋ํ ๊ฐ๊น๊ฒ ๋ง๋ค์ด์ผ ํ๋ค. N์ฅ์ ์นด๋์ ์จ์ ธ ์๋ ์ซ์๊ฐ ์ฃผ์ด์ก์ ๋, M์ ๋์ง ์์ผ๋ฉด์ M์ ์ต๋ํ ๊ฐ๊น์ด ์นด๋ 3์ฅ์ ํฉ์ ๊ตฌํด ์ถ๋ ฅ
๐ฌ 3์ค for ๋ฌธ์ ํตํด์ ์กฐ๊ฑด์ ๋ถํฉํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅ
[์ฝ๋ ์ฐธ๊ณ ]
N, M = map(int, input().split()) #์นด๋ ๊ฐ์์, ์ฃผ์ด์ง ์ arr = list(map(int, input().split())) #๋ฐ์์จ๊ฐ ๋ฆฌ์คํธ์ ๋ฃ๊ธฐ result = 0 #๊ฒฐ๊ณผ๊ฐ for i in range(N): for j in range(i+1, N): for k in range(j+1, N): if arr[i]+arr[j]+arr[k] > M: continue else: result = max(result, arr[i]+arr[j]+arr[k]) print(result)
[ํ์ด์ฌ : ์กฐํฉ ํจ์]
๐ฌ itertools : ๋ฐ๋ณต ๊ฐ๋ฅํ(iterable) ๊ฐ์ฒด๋ฅผ ์ฒ๋ฆฌํ๋ ๋ค์ํ ์ ์ฉํ ํจ์๋ค์ ์ ๊ณตํฉ๋๋ค. ์ด ๋ชจ๋์ ์กฐํฉ(combinations), ์์ด(permutations), ๊ณฑ์งํฉ(product) ๋ฑ ์ฌ๋ฌ ๊ฐ์ง ์ดํฐ๋ ์ดํฐ ๋น๋ฉ ๋ธ๋ก์ ์ ๊ณต
import sys import itertools N, M = map(int, sys.stdin.readline().split()) card_list = list(itertools.combinations(map(int, sys.stdin.readline().split()), 3)) tmp = [] total = 0 for i in range(len(card_list)): total = sum(card_list[i]) if total <= M: tmp.append(total) print(max(tmp))