백준 14888 - 연산자 끼워넣기 (파이썬)

Updated:

Answer

n = int(input())
num_list = list(map(int, input().split()))
a, b, c, d = map(int, input().split())

max_num = -1000000000
min_num = 1000000000

def dfs(num, x, a, b, c, d):
    global max_num, min_num, n

    if x == n:
        max_num = max(max_num, num)
        min_num = min(min_num, num)
        return
    else:
        if a:
            dfs(num + num_list[x], x + 1, a - 1, b, c, d)
        if b:
            dfs(num - num_list[x], x + 1, a, b - 1, c, d)
        if c:
            dfs(num * num_list[x], x + 1, a, b, c - 1, d)
        if d:
            dfs(int(num / num_list[x]), x + 1, a, b, c, d - 1)


dfs(num_list[0], 1, a, b, c, d)
print(max_num, min_num, sep = '\n')

Categories:

Updated: