Adding parentheses to shrink the value
Statement
Jaei and Seonu play a game of writing down an expression and handing it over. Jaei writes one expression on a sheet of paper and hands it over, and Seonu has to make its value as small as possible by adding parentheses.
The expressions Jaei writes hold only plus signs and minus signs. Seonu cannot change the order of the numbers in the expression, cannot alter a number, and cannot change an operator either. All Seonu can do is add parentheses. Any number of pairs of parentheses may go in, or none at all, but the result still has to be a valid expression.
For example, suppose Jaei wrote 55-50+40 and handed it over. The ways Seonu can add parentheses, and the value each one gives, are the following.
| Expression Seonu made | Value |
|---|---|
| 55-50+40 | 45 |
| 55-(50+40) | -35 |
Without parentheses the value is 45, and tying the last two numbers together gives -35. The smallest value that can be made from this expression is -35.
Some expressions do not get smaller however parentheses go in. Suppose Jaei wrote 11826-35672-3642.
| Expression Seonu made | Value |
|---|---|
| 11826-35672-3642 | -27488 |
| 11826-(35672-3642) | -21794 |
In this case -27488, with no parentheses at all, is the smallest.
Now look at a longer expression. Suppose Jaei wrote 17783+64711-44284+60526-25944-93096. Cutting the expression at the minus signs gives the following four chunks.
| Chunk | Expression | Value |
|---|---|---|
| First | 17783+64711 | 82494 |
| Second | 44284+60526 | 104810 |
| Third | 25944 | 25944 |
| Fourth | 93096 | 93096 |
A chunk that follows a minus sign can be tied together with parentheses and taken away whole. Taking all the other chunks away from the first chunk gives 82494 - 104810 - 25944 - 93096 = -141356, and that value is the smallest that can be made from this expression.
Given the expression Jaei wrote, write a program that finds the smallest value Seonu can make by adding parentheses.
Input and output examples
Example 1
Input
55-50+40
Output
-35
Example 2
Input
11826-35672-3642
Output
-27488
Example 3
Input
00009-00009
Output
0
Input
The first line gives the expression Jaei wrote. The expression consists only of digits, plus signs and minus signs, and its length is less than or equal to 50.
A digit stands at the very start and at the very end of the expression. Two or more operators never stand in a row.
A number in the expression is an integer of 0 or more, and it can start with 0. For example, 00009 means 9.
An expression with no operator at all, holding just one number, is given as well.
Output
Print the smallest value Seonu can make by adding parentheses on the first line.
Print one number and then a newline. When the value is negative, print a minus sign in front of it. Do not add any other character or space.
Requirements
- Read the input from standard input and write the answer to standard output
- The order of the numbers cannot be changed and an operator cannot be altered. Parentheses are the only thing that can go in
- Adding no parentheses at all is one of the ways. Some expressions do not get smaller when parentheses go in
- A number can start with 0. Read 00009 as 9
- An expression can hold just one number with no operator at all. The answer is then that number itself
- The answer can be negative
- Grading compares the output with the expected value down to the last character
- 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.
- There are 15 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 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
55-50+40
-35
00009-00009
0
11826-35672-3642
-27488
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.