MapleStory Finger Point

๐Ÿƒ‍โ™‚๏ธBaekjoon/Python

#2798 - python ๋ธ”๋ž™์žญ

HYEJU01 2024. 7. 1. 19:17
์ฒด๊ฐ 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))