Vanya and Brackets
| Time Limit: 1000MS |
|
Memory Limit: 262144KB |
|
64bit IO Format: %i64d &%i64u |
Description
Vanya is doing he maths homework. He has a expression of form, where x1, x2, ..., xn is digits from C8>1 to 9, ampersand represents either a plus '+ ' or the multiplication sign ' * '. Vanya needs to add one pair of brackets in this expression so that is maximize the value of the resulting expression.
Input
The first line contains expression s (1≤| S| ≤5001, | S| is odd), it odd positions only contain digits from 1 to 9, and even positions only contain signs + and *.
The number of signs * doesn ' t exceed.
Output
The first line print the maximum possible value of an expression.
Sample Input
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
Hint
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 =.
Note to the third sample test. (3 * 4) * 5 = (also many other variants is valid, for instance, (3) * 4 * 5 =.).
Source
Codeforces Round #308 (Div. 2) Problem Solving: violence blind, attention int overflow, silly forced to write the stack int ... Hey, learn.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4vector<int>Pos;5Stack<ll>num;6stack<Char>op;7LL Calc (Const string&str) {8 while(!num.empty ()) Num.pop ();9 while(!op.empty ()) Op.pop ();Ten for(inti =0, Slen = Str.length (); i < Slen; ++i) { One if(Str[i] = =')') { A while(Op.top ()! ='(') { -LL tmp =num.top (); - Num.pop (); the if(Op.top () = ='*') Num.top () *=tmp; - Else if(Op.top () = ='+') Num.top () + =tmp; - Op.pop (); - } + Op.pop (); - Continue; + } A if(IsDigit (Str[i])) Num.push (Str[i]-'0'); at Else if(Str[i] = ='+'&&!op.empty () && op.top () = ='*') { - while(!op.empty () && op.top () = ='*') { -LL tmp =num.top (); - Num.pop (); -Num.top () *=tmp; - Op.pop (); in } - Op.push (Str[i]); to}ElseOp.push (Str[i]); + } - while(!Op.empty ()) { theLL tmp =num.top (); * Num.pop (); $ if(Op.top () = ='*') Num.top () *=tmp;Panax Notoginseng Else if(Op.top () = ='+') Num.top () + =tmp; - Op.pop (); the } + returnnum.top (); A } the intMain () { + stringstr; -Cin>>str; $Pos.push_back (-1); $ intSlen =str.length (); - for(inti =1; i < Slen; i + =2) - if(Str[i] = ='*') Pos.push_back (i); the Pos.push_back (slen); -Slen =pos.size ();WuyiLL ret =int_min; the for(inti =0; i+1< Slen; ++i) - for(intj = i+1; J < Slen; ++j) { Wu strings =str; -S.insert (pos[i]+1,1,'('); AboutS.insert (pos[j]+1,1,')'); $RET =Max (Ret,calc (s)); - } -cout<<ret<<Endl; - return 0; A}View Code
codeforces-552e Vanya and Brackets