Sequence swapping Time limit:1 Second Memory limit:65536 KB
Baobao has just found a strange sequence {<, >, < , >, , >} of Length in his Pocket. As can see, each element <, > in the sequence are an Ord Ered pair, where the first element in the pair is the left parenthesis ' (' or the rig HT parenthesis ') ', and the second Element in the pair is an integer.
As Baobao is bored, he decides to play with the sequence. At the beginning, Baobao ' s score are set to 0. Each time Baobao can select a integer , swap the -th element and the-th element in the sequence, and Increas E His score by , if and only if , ' (' and ') '.
Baobao is allowed to perform the swapping any number of times (including Zero times). What ' s the maximum possible score Baobao can get?
Input
There is multiple test cases. The first line of the input contains an integer , indicating the number of the test cases. for each test case:
The first line contains an integer (), indicating the length of the sequence.
The second line contains a string () consisting of ' (' and ') '. the-th character in the string indicates , the which the meaning is described above.
The third line contains integers (). Their meanings is described above.
It's guaranteed the sum of all test cases would not exceed .
Output
For each test case output one line containing one integer, indicating the maximum possible score Baobao can get.
Sample Input
46) ()) () 1 3 5-1 3 26) ()) () 1 3 5-100 3 23 ()) 1-1-13 ())-1-1-1
Sample Output
242102
Hint
For the first sample test case, the optimal strategy are to select in order.
For the second sample test case, the optimal strategy are to select in order.
Puzzle : A pair of parentheses exchange is equivalent to a left parenthesis to the right, a right parenthesis to the left one bit, all of the interchange in the relative position of the left parenthesis, and the same right parenthesis. From the right, you can enumerate the positions where each opening parenthesis moves, and the left parenthesis moves to a point where the bonus points equal to the maximum score of the point at which the parentheses precede the points. Code: #include<bits/stdc++.h>using namespacestd; #definell Long LongConst intmaxn=1003; CharT[MAXN]; ll A[MAXN],DP[MAXN][MAXN]; intMain () {intT;SCANF ("%d",&T); while(t--) {memset (DP,0,sizeof(DP)); intN;SCANF ("%d",&N); scanf ("%s", t+1); for(intI=1; i<=n;i++) scanf ("%lld",&A[i]); intnow=0; ll Ma=0; for(inti=n;i>=1; i--) { if(t[i]=='(') { now++; for(intj=i+1; j<=n;j++) { if(t[j]=='(') dp[now][j]=dp[now][j-1]; Elsedp[now][j]=a[i]*a[j]+dp[now][j-1]; Ma=max (dp[now][j]+dp[now-1][j],ma); } for(intj=i;j<=n;j++) dp[now][j]+=dp[now-1][j]; for(intJ=n;j>1; j--) dp[now][j-1]=max (dp[now][j-1],dp[now][j]); }} printf ("%lld\n", MA); } return 0; }
The 15th session of Zhejiang Province race D Sequence swapping (DP)