์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step11] 2798 : ๋ธ”๋ž™์žญ (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 18. 16:56

www.acmicpc.net/problem/2798

 

2798๋ฒˆ: ๋ธ”๋ž™์žญ

์ฒซ์งธ ์ค„์— ์นด๋“œ์˜ ๊ฐœ์ˆ˜ N(3 ≤ N ≤ 100)๊ณผ M(10 ≤ M ≤ 300,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์นด๋“œ์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, ์ด ๊ฐ’์€ 100,000์„ ๋„˜์ง€ ์•Š๋Š” ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. ํ•ฉ์ด M์„ ๋„˜์ง€ ์•Š๋Š” ์นด๋“œ 3์žฅ

www.acmicpc.net


N๊ฐœ์˜ ์นด๋“œ ์ค‘ M์„ ๋„˜์ง€ ์•Š๋Š” ์„ธ ๊ฐœ์˜ ํ•ฉ์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

1. i๋Š” 0๋ถ€ํ„ฐ n-2๊นŒ์ง€, j๋Š” i+1๋ถ€ํ„ฐ n-1๊นŒ์ง€, l์€ j+1๋ถ€ํ„ฐ n๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ๊ณจ๋ผ ๋”ํ•œ๋‹ค

( i๋ฅผ ๊ณ ๋ฅด๋ฉด j๋Š” i ์ด์ „์˜ ์นด๋“œ๋ฅผ ๊ณ ๋ฅผ ํ•„์š”๊ฐ€ ์—†๊ณ 

  i๊ฐ€ n-2์ผ๋•Œ n-1๊ณผ n์€ ๊ฐ๊ฐ j์™€ l์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— n-2๊นŒ์ง€๋งŒ ๊ณ ๋ฅด๋ฉด ๋œ๋‹ค )

2. i + j + l ์ด total๋ณด๋‹ค ํฌ๊ณ  m๊ณผ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์„๋•Œ๋งŒ total๊ณผ ๊ต์ฒดํ•œ๋‹ค 

# 2798.py

n, m = map(int, input().split())
card = list(map(int, input().split()))

total = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        for l in range(j+1, n):
            a = card[i] + card[j] + card[l]
            if m >= a > total:
                total = a

print(total)