/
14888_1.py
59 lines (49 loc) · 1.54 KB
/
14888_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from itertools import permutations
from collections import deque
import copy
import sys
def solve(n, num_list, operation_count_list):
op = ['+', '-', '*', "//"]
operation_list = []
max = -sys.maxsize - 1
min = sys.maxsize
for i in range(4):
operation = op[i]
count = operation_count_list[i]
temp = [operation]*count
operation_list.extend(temp)
# Deduplication
case_list = set(permutations(operation_list, n-1))
for case in case_list:
temp_list = deque(copy.deepcopy(num_list))
idx = -1
result = temp_list.popleft()
while temp_list:
idx += 1
next_num = temp_list.popleft()
current_op = case[idx]
if current_op == '+':
result += next_num
elif current_op == '-':
result -= next_num
elif current_op == '*':
result *= next_num
else:
if result < 0:
result = -(result)
result //= next_num
result = -(result)
else:
result //= next_num
if result < min:
min = result
if max < result:
max = result
return max, min
if __name__ == "__main__":
n = int(input().strip())
num_list = deque(list(map(int, input().strip().split())))
operation_count_list = deque(list(map(int, input().strip().split())))
max, min = solve(n, num_list, operation_count_list)
print(max)
print(min)