Maximize the Expression
InterviewTime limit1sMemory limit512 MB
Arrange N given positive integers with P addition and Q multiplication operators, in any order and with any parentheses, to maximize the result.
- Level
Medium5 of 10
- Topics
- Brute force, Backtracking, Greedy, Combinatorics
- Solved
- No attempts yet
Problem
There are positive integers and a total of operators, each either a multiplication operator or an addition operator. You may use as many parentheses as you like. In this expression, the multiplication operator and the addition operator have the same precedence.
Integers and operators must be arranged as shown below. The order of the integers may be changed.

For example, suppose there are the integers , , and one addition operator and one multiplication operator. Then an expression like the one below can be made.

For example, suppose there are the numbers , four addition operators, and one multiplication operator. Some of the ways to obtain the maximum value using parentheses are:
Use the operations well to make the value as large as possible.
Input
The first line gives , the number of positive integers to be used.
The next line gives the positive integers , separated by spaces.
The last line gives the number of addition operators and the number of multiplication operators , separated by a space.
Output
Print the maximum value among all possible results of the operations.