Given up to 11 numbers and counts of the four arithmetic operators, place the operators between adjacent numbers, evaluate left to right without precedence, and report the maximum and minimum results.
Medium6Brute forceBacktrackingImplementationMathNo attempts yetTime limit2sMemory limit512 MBYou are given a sequence of N numbers A1,A2,…,AN, along with N−1 operators to insert between adjacent numbers. The operators are addition (+), subtraction (−), multiplication (×), and division (÷) only.
Insert one operator between each pair of adjacent numbers to build an expression. The order of the given numbers must not change.
For example, if the sequence is 1, 2, 3, 4, 5, 6 and the operators are two additions, one subtraction, one multiplication, and one division, there are 60 possible expressions. Four of them are:
An expression is evaluated left to right, ignoring operator precedence. Division keeps only the quotient. When a negative number is divided by a positive number, divide the two absolute values and attach a minus sign to the quotient. Under these rules the four expressions above evaluate as follows:
Given N numbers and N−1 operators, write a program that finds the maximum and the minimum result among the expressions you can build.
The first line contains the count of numbers N. (2≤N≤11)
The second line contains A1,A2,…,AN separated by spaces. (1≤Ai≤100)
The third line contains four integers whose sum is N−1. In order, they are the number of additions, the number of subtractions, the number of multiplications, and the number of divisions.
Print the maximum result on the first line and the minimum result on the second line.
Only inputs for which every placement of the operators yields a result of at least −109 and at most 109 are given. Every intermediate result during the left to right evaluation is also at least −109 and at most 109.
When the sequence is 1, 2, 3, 4, 5, 6 and the operators are two additions, one subtraction, one multiplication, and one division, the maximum and the minimum come from these expressions.