[알고리즘/백준] 2798번 : 블랙잭(python)

유현민·2022년 2월 24일
0

알고리즘

목록 보기
21/253

3중 for문을 써서 풀었다...

def answer(M, a):
    result = 0
    for i in range(N):
        for j in range(i + 1, N):
            for k in range(j + 1, N):
                if M >= a[i] + a[j] + a[k] > result:
                    result = a[i] + a[j] + a[k]
    print(result)
if __name__ == '__main__':
N, M = map(int, input().split())
    a = list(map(int, input().split()))
    answer(M, a)

찾다보니 python에 itertools모듈의 combinations 함수가 있었다.
알아서 순열을 만들어 준다.

from itertools import combinations


def answer(M, a):
    result = 0
    for i in combinations(a, 3):
        if result < sum(i) <= M:
            result = sum(i)
    print(result)


if __name__ == '__main__':
    N, M = map(int, input().split())
    a = list(map(int, input().split()))
    answer(M, a)
profile
smilegate megaport infra

0개의 댓글