https://www.acmicpc.net/problem/2798
2798๋ฒ: ๋ธ๋์ญ
์ฒซ์งธ ์ค์ ์นด๋์ ๊ฐ์ N(3 ≤ N ≤ 100)๊ณผ M(10 ≤ M ≤ 300,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค์๋ ์นด๋์ ์ฐ์ฌ ์๋ ์๊ฐ ์ฃผ์ด์ง๋ฉฐ, ์ด ๊ฐ์ 100,000์ ๋์ง ์๋ ์์ ์ ์์ด๋ค. ํฉ์ด M์ ๋์ง ์๋ ์นด๋ 3์ฅ
www.acmicpc.net
์ฒ์ ํ์ด
N, M = map(int, input().split())
li = list(map(int, input().split()))
li.sort()
n = 3
sum = 0
for i in range(N):
if n>0 and sum + li[N-1-i] <= M :
sum += li[N-1-i]
n -=1
print(sum)
์ผ์ค for๋ฌธ(์ฐธ๊ณ )
N, M = map(int,(input().split()))
d = list(map(int, input().split())) # ์นด๋ ๋ฆฌ์คํธ
result = 0
Max = 0
for i in range(N-2): # 3์ค for๋ฌธ์ ๋๋ฉด์ ๊ฒน์น์ง ์๊ฒ ๋ฒ์๋ฅผ ์ง์
for j in range(i+1,N-1):
for k in range(j+1,N):
if d[i]+d[j]+d[k] > M: # 3๊ฐ์ ๊ฐ ๋ํ๊ฒ์ด M๋ณด๋ค ํฌ๋ค๋ฉด ๋์ด๊ฐ
continue
else:
result = d[i]+d[j]+d[k]
if Max <= result: # M๊ณผ ๊ฐ์ฅ ์ ์ฌํ ๊ฐ์ ๊ฐ์ฅ ํฐ๊ฐ์ด๊ธฐ ๋๋ฌธ์ ๋น๊ตํด์ ํฐ๊ฐ์ ์ ์ฅ
Max = result
print(Max)
์ผ์ค for๋ฌธ(๋ด๊ฐ ์์ฑ)
N,M = map(int, input().split())
card = list(map(int, input().split()))
max =0
for i in range(0,N-2):
for j in range(i+1, N-1):
for k in range(j+1, N):
sum = card[i] + card[j] + card[k]
if sum<=M and max<sum:
max = card[i] + card[j] + card[k]
print(max)
ํจํค์ง ํ์ด
from itertools import combinations
n, m = map(int,input().split())
card = list(map(int, input().split()))
com = list(combinations(card, 3))
total = 0
for i in com:
if sum(i) <= m:
total = max(total, sum(i))
print(total)
๋ฐ์ํ
'๐ Python > ๐ CodingTest' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์ด์ฌ ์ฝ๋์ ๊ธฐ์ด 100์ ] ๋ฌธ๋ฒ ๋ณต์ต, ์์ฃผ ํ๋ฆฌ๋ ์ ํ (0) | 2021.09.28 |
---|---|
๋ฐฑ์ค ๋ถํดํฉ (0) | 2021.09.24 |
๋ฐฑ์ค ๋ฉ์น (0) | 2021.09.23 |
๋ฐฑ์ค ํฌ๋ก์ํฐ์ ์ํ๋ฒณ (0) | 2021.09.23 |
์๋ ํ์ด / ๋ฐฑ์ค ๋์ (0) | 2021.09.22 |