#K43687. Maximum Result Expression

    ID: 27365 Type: Default 1000ms 256MiB

Maximum Result Expression

Maximum Result Expression

You are given an arithmetic expression consisting of non-negative integers and the operators + and - (without any spaces). You are allowed to add parentheses in any possible way to change the order of evaluation. Your task is to compute the maximum possible value of the expression by inserting parentheses optimally.

For example, consider the expression:

$$a_1 \; op_1 \; a_2 \; op_2 \; a_3 \; \cdots \; op_{n-1} \; a_n, $$

where each \( op_i \in \{+, -\} \) and \(a_i\) are non-negative integers. By choosing different ways of parenthesizing, the computed result may vary. You need to output the maximum value obtainable.

Note: The order of the numbers and the operators remains unchanged. Only the grouping (i.e. the parentheses) can be changed.

Examples:

  • For the expression 1+2-3+4, the maximum result is 4.
  • For the expression 10-20+30, the maximum result is 20.
  • For the expression 5+5-5-5, the maximum result is 10.

inputFormat

The input is read from standard input (stdin) and consists of multiple test cases. The first line contains an integer t (1 ≤ t ≤ 50) indicating the number of test cases. Each of the next t lines contains a non-empty string representing an arithmetic expression composed of non-negative integers and the operators + and - without any spaces.

outputFormat

For each test case, output a single integer — the maximum value obtainable by inserting parentheses optimally. Each result should be printed on a new line to standard output (stdout).

## sample
3
1+2-3+4
10-20+30
5+5-5-5
4

20 10

</p>