후보군을 만들어서 돌렸더니... 시간초과가 납니다. 그래서 dfs로...
https://www.acmicpc.net/problem/14888
import copy
n = int(input())
num = list(map(int, input().split()))
op = list(map(int, input().split()))
maximum = -10000000000
minimum = 1000000000
def calculate_total(total, num, i):
if i == 0:
return total + num
elif i == 1:
return total - num
elif i == 2:
return total * num
else:
return int(total / num)
def calculate(cnt, total, op):
global maximum, minimum
if cnt == n:
maximum = max(total, maximum)
minimum = min(total, minimum)
return
for i in range(4):
if (op[i] > 0):
temp_op = copy.deepcopy(op)
temp_op[i] -= 1
calculate(cnt + 1, calculate_total(total, num[cnt], i), temp_op)
calculate(1, num[0], op)
print(maximum)
print(minimum)