Under construction

Still being built. Things may change

Ambiguous Result

Interview

Time limit1sMemory limit128 MB

Summary
Given a parenthesis-free expression of numbers joined by + and *, restore parentheses to find the smallest and largest values it can evaluate to.
Level

Medium6 of 10

Topics
Dynamic programming, Intervals, Math, Implementation
Solved
No attempts yet

Problem

The ACM (Advanced Cosmos Monitor) recorded a set of messages transmitted by an alien race of Space Invaders. Unfortunately, the antenna used for recording only handles the lower frequencies that represent numbers and two arithmetic operators in the space-invader language, while all parentheses (which correspond to a high frequency) were lost.

Because numbers matter to these 8-bit creatures, we need to know the range of values each message could represent. Given such a parenthesis-free expression, write a program that determines the smallest and largest values it can evaluate to once parentheses are restored.

Input

The input contains several valid arithmetic expressions, one per line. Each expression consists only of non-negative integers xix_i (0≤xi≤1000 \le x_i \le 100) and the binary operators + and *. An expression starts with a number, then numbers and operators alternate, and it ends with a number. Each expression contains PP numbers (1≤P≤1001 \le P \le 100) and P−1P-1 operators. There are no parentheses, no other operators, and no unary operators.

The last expression is followed by a line containing the single word END.

Output

For each input line (excluding the final END), print one line with the minimum and maximum values, separated by a single space, that can be obtained by inserting parentheses so the expression stays valid and then evaluating it.

For example, 2+1*0 can be grouped as (2+1)*0, whose value 0 is the minimum, or as 2+(1*0), whose value 2 is the maximum.

It is guaranteed that, for every placement of parentheses, the value of each parenthesized sub-expression is less than 2632^{63}. Consequently, the maximum result is between 00 and 263−12^{63}-1, inclusive.

Examples1

  1. Example 1

    Input
    2+1*0
    3+2*5+1*7+16
    0
    END
    
    Expected output
    0 2
    36 690
    0 0