728x90
import sys
from itertools import combinations
def solution(n, m, cards):
total = 0
for card in combinations(cards, 3):
if sum(card) <= m:
total = max(sum(card), total)
return total
n, m = map(int, sys.stdin.readline().split())
cards = list(map(int, sys.stdin.readline().split()))
print(solution(n, m, cards))
모든 경우를 탐색해야해서 완전탐색 문제인데, 그 중 3개만을 더해야 하는 문제입니다. 그런데 5 6 7과 7 6 5는 순서는 다르지만 같은 것으로 인식하기 때문에 순서를 고려하지 않는 조합을 사용해야 하는 문제입니다. itertools, combinations를 사용해서 문제를 풀었습니다.
728x90
'[Coding Test] > [백준]' 카테고리의 다른 글
[백준] 2178: 미로 탐색 (0) | 2022.06.27 |
---|---|
[백준] 7568: 덩치 (0) | 2022.06.24 |
[백준] 2231: 분해합 (0) | 2022.06.24 |
[백준] 1018: 체스판 다시 칠하기 (0) | 2022.06.24 |
[백준] 2869 python(파이썬) : 달팽이는 올라가고 싶다 - 상세해설 (0) | 2022.05.20 |