Baobao has just found a strange sequence {<,>, < , >} of length in his pocket. As can see, each element <> in the sequence are an ordered pair, where the first Element The pair is the left parenthesis ' (' or the right parenthesis ') ', and th e second element in the pair are 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.
is a dp,dp[i][j] indicates the maximum value of the opening parenthesis to the first J.
You can then update it.
But he did not arrive at the same treatment as he had been.
Madeleine,
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=1e3+ the;CharS[n];intN,p[n],nxt[n];Long LongSum[n],v[n];Long Longmx[2][n];intMain () {intT; while(SCANF ("%d", &t)! =EOF) { while(t--) {scanf ("%d",&N); Long Longans=0; inttot=0, f=0; scanf ("%s", s+1); for(intI=1; i<=n;++i) scanf ("%lld", v+i); for(inti=n;i>=1;-I.)if(s[i]=='(') p[++tot]=i; for(intI=1; i<=n;++i) sum[i]=sum[i-1]+ (s[i]==')'? V[i]:0); memset (NXT,0,sizeof(NXT)); for(intI=1; i<=n;++i)if(s[i]==')') { if(f) nxt[f]=i; F=i; } for(intI=1; i<=n;++i) mx[1][i]=mx[0][i]=0; mx[1][0]=mx[0][0]=-(1ll<< -); intCur=0; for(intI=1; i<=tot;++i) { for(intj=p[i]+1; j<=n;++j)if(s[j]==')') mx[cur][j]=v[p[i]]* (Sum[j]-sum[p[i]) +mx[cur^1][j]; for(intJ=N;J>=P[I];--J)if(s[j]==')') mx[cur][j]=Max (mx[cur][nxt[j]],mx[cur][j]); for(intj=p[i]-1; j>=1;--j)if(s[j]==')') Mx[cur][j]=max (mx[cur^1][j],mx[cur][nxt[j]]); for(intj=1; j<=n;++j)if(s[j]==')') ans=Max (ans,mx[cur][j]); Cur^=1; } printf ("%lld\n", ans); } }}
D:sequence swapping