본문 바로가기

Programming/Algorithm

[Python] 백준 2798번 : 블랙잭


n, M = list(map(int, input().split(' ')))
data = list(map(int, input().split(' ')))
length = len(data)

approx = 0
for i in range(0, length - 2):
    if data[i] >=  M:
            continue
    for j in range(i + 1, length -1):
        if data[i] + data[j] >=  M:
            continue
        for k in range(j + 1, length):
            if data[k] + data[i] + data[j] <= M:
                sum = data[k] + data[i] + data[j]
                if abs(M - approx) >= abs(M - sum):
                    approx = sum
print(approx)