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 arithmetic expression. This expression consists of numbers (0-9) separated 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
gormint7777: 2018-09-28 17:47:59

sharif ullah's comment is a real helper _/\_

Last edit: 2018-09-28 17:49:18
sharif ullah: 2018-05-21 21:43:32

so,like people i would like to say,,,,it nothing but a recursive problem ,,more formally a matrxi chain multiplication problem... if u dont get idea to solve it THINK ABOUT THE problem ,,,scretch how MCM works,,,,TRUST me Though u think about a girl whole day or month she may be reject ur love!!!! but thinking about programming problem never frustrate u....

Last edit: 2018-05-21 21:48:54
aakashjaiswal: 2018-01-26 13:34:34

AC in one go :D
Top down dp working fine.Just a variation of MCM.

santanu_kar: 2018-01-05 13:12:46

don't forget the '\n' after printing the output. got 3 wrong answers for thsi

sfialok98: 2017-08-29 15:11:56

This costed me 1 WA
i/p->
1
8*8*8*8*8*8*8*8*8*8*8*8*8*8*8*8*8*8
o/p-> 18014398509481984 18014398509481984

Last edit: 2017-08-29 15:12:29
karthik_vg: 2017-05-27 15:17:57

simple mcm problem ;)

yash_18: 2016-10-19 21:48:55

good implementation of MCM ... :)

siddharth9820: 2016-10-02 14:07:27

O(n^3), just be careful with the data types.

Nallagatla Manikanta: 2016-08-12 09:26:40

accepted in one go :)

mkfeuhrer: 2016-07-11 20:52:38

learnt something new !! nice MCM:-)


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