LISA - Pocket Money

no tags 

Young people spend a lot of money on things like sweets, music CDs, mobile phones and so on. But most young girls/boys have one problem: Their pocket money is not enough for all these jolly things. Little Lisa Listig is one of these poor girls with a small pocket money budget. Last month her pocket money lasted only one week. So she decided to enter into negotiations with her father. Her father Tomm - a mathematician - had an incredibly ingenious idea: He wrote down some fancy digits with operators (+,*) in between them on a sheet of paper and allowed Lisa to insert brackets. Then he declared that the result of that arithmetic expression is Lisa's new pocket money. Now it's Lisa's task to maximize her pocket money. As her father was surprised what a huge sum of money Lisa got for her result, he decided to minimize the result of the expression for his son Manfred. Now it's your task to calculate the results obtained by Lisa and her father.

Input

The first line of input contains the number of testcases k ( k< 5000 ). Each of the following k lines consists of an arithemtic expression. This expression consists of numbers (0-9) seperated by one of the two operators '*' and '+'. There are no spaces between the characters. Each line contains less than 100 characters.

Output

For each expression output the result obtained by Lisa and the result obtained by her father separated by one space. The results of the calculations are smaller than 264.

Example

Input:
1
1+2*3+4*5

Output:
105 27

Two possible expressions for the first testcase:

105 = (1+2)*(3+4)*5
27  = 1+2*3+4*5

hide comments
nondescript: 2013-10-24 00:43:05

beautiful DP. thanks!

Avaneesh Rastogi: 2013-10-18 22:42:43

The answer for 2+1*0*1+1 is >>
(2+(1*0))*(1+1)=4
It seems very easy to believe the answer to be 3 as follows >>
2+(1*0*1)+1=3

dont give up easily!

Last edit: 2013-10-25 20:49:55
samuel: 2013-07-10 02:49:41

Nice DP!!

technophyle: 2013-05-31 00:31:13

Hint: find analogue...:)

Aradhya: 2013-05-26 06:42:41

why time limit is set 32 sec o.O ? 1 sec is more than enough.. i used cin cout strings.. etc still 0.63

Haijun Deng: 2013-05-25 09:50:50

Still Got WA with following cases:
2
2+1*0*1+1
2+1*0*1+1
3 0
17 0

anuj kumar jain: 2013-04-14 01:43:21

recursive solution costed me many TLEs.... but same logic when implemented by looping got accepted....:)))

Santiago Palacio: 2013-01-07 03:25:20

Time limit is WAY too large.

Shubham: 2012-12-18 20:24:48

if there were no 0, would greedy solution work?

rohitjv: 2012-12-11 09:41:58

<No spoilers in the comments>

Last edit: 2012-12-12 07:48:43

Added by:Simon
Date:2005-05-17
Time limit:32s
Source limit:8082B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL GOSU JS-RHINO
Resource:Ulm Algorithm Course SoSe 2005