알고리즘

[백준 2798] 블랙잭 - combinations

kyeongjun-dev 2021. 12. 22. 11:39

코드

from itertools import combinations

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

arr = list(map(int, input().split()))

max_num = 0
for com in combinations(arr, 3):
    sum_com = sum(com)
    if sum_com <= m:
        max_num = max(max_num, sum(com))

print(max_num)

 

설명

  • 조합으로 3개의 카드를 골랐을 모든 경우의 수를 구한다
  • 모든 경우의 수 중, 3개의 카드의 합이 M을 넘지 않는 한에서 가장 큰 값으로 계속 갱신한다
  • 최종적으로 갱신된 최대 수를 출력