출처 : 백준

문제는 여기에


접근

연산자의 개수에 따라 나올 수 있는 경우의 수 -> 순열 (permutation)

리스트로 받는 연산자 구별 -> dict + lambda

연산자 우선순위 무시하고 연산 -> reduce

import itertools
from functools import reduce
import sys
import time

def insert_operation(length, input_num, input_oper):

    ops = {"0":(lambda x,y: x+y), "1":(lambda x,y: x-y), "2":(lambda x,y: x*y), "3":(lambda x, y: int(x / y) if x >= 0 else int(-(-x / y)))}
    oper_permutation = []
    result = []
    list(oper_permutation.extend(
        [str(index)] * value) for index, value in enumerate(input_oper) if value > 0)
    permutation = [list(x) for x in set(itertools.permutations(oper_permutation))]
    for i in permutation:
        result.append(reduce(lambda x,y: ops[i.pop()](x,y) , input_num))
    
    max_result = max(result)
    min_result = min(result)

    # print(str(max(result))+"\n"+str(min(result)))
    return max_result, min_result
    
# 입력 받기
n = int(input())
numbers = list(map(int, input().split()))
operator = list(map(int, input().split()))

a, b = insert_operation(n, numbers, operator)

# 출력
print(a)
print(b)