Largest and smallest value of a card expression
Statement
A math club plays a game of laying out cards to build an expression.
There are N number cards, laid out on the desk in a single row from left to right. This order is never changed until the game ends. There are also N-1 operator cards of four kinds, plus, minus, times, and divide, and the count of each kind is fixed.
One operator card goes into each of the N-1 gaps that sit between two neighboring number cards. Every operator card on hand has to be used, with none left over. Cards of the same kind are not told apart, so only which kind goes into which gap matters.
Once all the cards are in place they form one expression. The expression is evaluated from the left, one operator at a time. The rule that times and divide are evaluated first is not used. A division drops the part of the quotient below the decimal point. When the number being divided is negative, divide its absolute value first and then put a minus sign on that quotient. Every number written on a number card is 1 or more, so a division by 0 never happens.
Write a program that considers every way of inserting the operator cards and computes the largest and the smallest value of the expressions that can be built.
For example, suppose N=3 and the number cards are laid out as in the table below.
| Position | 1 | 2 | 3 |
|---|---|---|---|
| Number card | 40 | 99 | 34 |
Suppose the operator cards are 1 plus card and 1 minus card. There are two gaps and two cards, so the two expressions below can be built.
| Expression | Steps | Value |
|---|---|---|
| 40 + 99 - 34 | 40 plus 99 is 139, 139 minus 34 is 105 | 105 |
| 40 - 99 + 34 | 40 minus 99 is -59, -59 plus 34 is -25 | -25 |
The largest value in this example is therefore 105 and the smallest value is -25. If only one expression can be built, the largest value and the smallest value are the same.
Input and Output Examples
Example 1
Input
3 40 99 34 1 1 0 0
Output
105 -25
Example 2
Input
2 91 66 0 0 1 0
Output
6006 6006
Example 3
Input
6 1 2 3 4 5 6 2 1 1 1
Output
54 -24
Input
First the number of number cards N is given as a natural number. N is a natural number from 2 to 11.
Then the numbers written on the N number cards are given in order from the left on one line, separated by spaces. Each number is a natural number from 1 to 100.
Then the counts of the operator cards are given as four numbers on one line, separated by spaces. In order they are the counts of the plus cards, the minus cards, the times cards, and the divide cards. The four numbers always add up to N-1, and a kind whose count is 0 may appear.
Output
Print the largest value of the expressions that can be built on the first line, and the smallest value on the next line. Each of the two lines holds a single number and nothing else. When a value is negative, print it with a minus sign.
Requirements
- Do not change the order of the number cards
- Insert every operator card given, leaving none over
- Evaluate the expression from the left, one operator at a time, and do not evaluate times and divide first
- A division drops the part of the quotient below the decimal point, and a negative number is divided by absolute value first and then given a minus sign
- Print the largest value on the first line and the smallest value on the next line
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 16 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 16 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 16 cases to solve this.
2 91 66 0 0 1 0
6006 6006
3 40 99 34 1 1 0 0
105 -25
6 1 2 3 4 5 6 2 1 1 1
54 -24
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.