Codeforces 552 E Vanya and Brackets

Source: Internet
Author: User

Test instructions is this, give an operator only + and *, the number is between 1 to 9 of the calculation, you want to add a pair of parentheses, so that the result of the calculation as large as possible, to ensure a maximum of 15 multiplication sign.

Obviously, to let the parentheses can affect the result of the original operation, it is necessary to affect the multiplication, then the addition of the pair of parentheses must have at least one with multiplication sign is adjacent, just multiplication sign the number is very small, then directly enumerate the position of the parentheses, each time to calculate the current solution update ans can be.

#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;typedef long long ll;ll x[3000];bool s[3000];ll work (int l,int r,int l1,int R1) {ll ans=0;for (int i=l;i<=r;i++) {if (I==L1) i=r1;if (s[i]) ans+=x[i];else{ll t=1;for (int j=i;j<=r;j++) {if (J==L1) j =r1;t*=x[j];if (s[j]| | J==R) {i=j;break;}} ans+=t;}} return ans;} int main () {int n=0,m=0;for (int i=0;;; i++) {char C=getchar (), if (c== ' \ n ') break;if (i&1) s[m++]= (c== ' + '); elsex[n++]=c-' 0 ';} ll Ans=work (0,n-1,-1,-1); for (int. i=0;i<m;i++) if (!s[i]) {for (int j=0;j<=i;j++) {ll t=work (j,i,-1,-1); Swap (t,x[i ]); Ans=max (Ans,work (0,n-1,j,i)); swap (t,x[i]);} for (int j=i+1;j<n;j++) {ll t=work (i+1,j,-1,-1); swap (t,x[j]); Ans=max (Ans,work (0,n-1,i+1,j)); swap(T,x[j]);}} Cout<<ans;}

Time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Vanya is doing he maths homework. He has a expression of form, where x1,? x 2,?...,? x N is digits from1To9, and sign represents either a plus '+' or the multiplication sign' * '. Vanya needs to add OnePair of brackets in this expression so that is maximize the value of the resulting expression.


The first line contains expressions(1?≤?| S|? ≤?5001,| S|is odd), it odd positions only contain digits from1To9, and even positions only contain signs?+?and?*?.

The number of signs ? * doesn ' t exceed 15.


The first line print the maximum possible value of an expression.

Sample Test (s) input

Note to the first sample test. 3?+?5?*? (7?+?8)? *?4?=?303.

Note to the second sample test. (2?+?3)? *?5?=?25.

Note to the third sample test. (3?*?4)? *?5?=?60 (also many other variants is valid, for instance, (3)? *?4?*?5?=?60).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 552 E Vanya and Brackets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.