BaoBao has just found a strange sequence {<s1,v1>, <s2,v2>, ... <sn,vn>} of length n in his pocket. As you can see, each element <si,vi> in the sequence is an ordered pair, where the first element in the pair is the left parenthesis '(' or the right 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 is set to 0. Each time BaoBao can select an integer k swap the k-th element and the (k+1)-th element in the sequence, and increase his score by (vk*v(k+1)), if and only if 1<=k<n, sk='(' and s(k+1)= ')'.
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 are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n(1<=n<=10^3), indicating the length of the sequence.
The second line contains a string s(|s|=n) consisting of '(' and ')'. The i-th character in the string indicates si, of which the meaning is described above.
The third line contains n integers v1,v2,......,vn (-10^3<=vi<=10^3). Their meanings are described above.
It's guaranteed that the sum of n of all test cases will not exceed 10^4
Output
For each test case output one line containing one integer, indicating the maximum possible score BaoBao can get.
Sample Input
4 6 )())() 1 3 5 -1 3 2 6 )())() 1 3 5 -100 3 2 3 ()) 1 -1 -1 3 ()) -1 -1 -1
Sample Output
24 21 0 2
Hint
For the first sample test case, the optimal strategy is to select k=2,3,5,4 in order.
For the second sample test case, the optimal strategy is to select k=2,5 in order.
题意:
当且仅当sk='('并且s(k+1)=')'时可以交换二者,并且得到两者价值乘积的价值,问最多可以得到多少价值。
思路:
我是用的dp做法
首先,这道题要求只能左括号和右括号换,这就决定了每个左括号能到达的最右位置,如果这个括号可以被移动到第j个位置,那么他后面的左括号一定都在比他右的位置,
也就是j+1位之后,设dp[i][j]是把第i个左括号移动到j右面(包括j)位中能得到的最大价值
转移方程为:
dp[i][j]=max(dp[i+1][j+1]+移动到这里能得到的值,dp[i][j+1])(可以直接开始,因为dp[i+1][j+1]一开始是0所以无影响)
而移动到这里所得到的值是(移动的这个左括号的值)*(他交换的右括号的值的和),用前缀和表示前多少个右括号的和,在输入的时候记录它前面有几个右括号,
他所在位减去他右边的左括号就是他右边的右括号的值数量。由此可知:右括号的值的和=前缀和(括号总数量-右边括号)-前缀和(左边的右括号)。
代码:
#include<bits/stdc++.h>
#define LL long long
#define lowbit(x) x&-x
using namespace std;
const LL MAXN = 100010;
const LL INF = 0x3f3f3f3f;
const double eps = 1e-9;
LL he[1005];
LL dp[1005][1005];
LL kuo[1005];
char chuan[1005];
LL val[1005];
LL wei[1005];
LL zhi[1005];
LL qi[1005];
int main()
{
LL a,k;
while(~scanf("%lld",&a))
{
while(a--)
{
scanf("%lld",&k);
scanf("%s",chuan);
LL ru=1;
kuo[0]=0;
LL ru1=1;
for(LL i=0; i<k; i++)
{
scanf("%lld",&val[i]);
if(chuan[i]==')')
{
kuo[ru]=kuo[ru-1]+val[i];
ru++;
}
else
{
wei[ru1]=i;
zhi[ru1]=val[i];
qi[ru1]=ru-1;
ru1++;
}
}
ru1--;
ru--;
memset(dp,0,sizeof(dp));
LL jie=0;
for(LL i=ru1; i>=1; i--)
{
LL zuo=ru1-i;
for(LL j=k-1-zuo; j>=wei[i]; j--)
{
LL hou=k-1-j-zuo;
LL de=kuo[ru-hou]-kuo[qi[i]];
if(j==k-1-zuo)dp[i][j]=zhi[i]*de+dp[i+1][j+1];
else dp[i][j]=max(zhi[i]*de+dp[i+1][j+1],dp[i][j+1]);
if(i==1)
{
if(dp[i][j]>jie)jie=dp[i][j];
}
}
for(LL j=wei[i]-1; j>=wei[i-1]; j--)
{
dp[i][j]=dp[i][j+1];
if(i==1)
{
if(dp[i][j]>jie)jie=dp[i][j];
}
}
}
printf("%lld\n",jie);
}
}
return 0;
}
/*
5
4
)))(
1 1 1 1
6
)())()
1 3 5 -1 3 2
6
)())()
1 3 5 -100 3 2
3
())
1 -1 -1
3
)))
-1 -1 -1
*/