BaoBao has just found a strange sequence {<, >, <, >, , <, >} of length in his pocket. As you can see, each element <, > 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 , swap the -th element and the -th element in the sequence, and increase 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 are multiple test cases. The first line of the input contains an integer , indicating the number of 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 , of which the meaning is described above.
The third line contains integers (). Their meanings are described above.
It's guaranteed that the sum of of all test cases will not exceed .
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 in order.
For the second sample test case, the optimal strategy is to select in order.
Author: WENG, Caizhi
Source: The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
题意:给你一个字符串,每个位置的字符对应一个值,当且仅当s[i]=='('&&s[i+1]==')'时可以交换两者,并且得到两者价值乘积的价值,问最多可以得到多少价值
解题思路:因为只能左括号和右括号换,所以每个左括号能到达的最右位置是固定的,如果这个括号可以被移动到第j个位置,那么他后面的左括号一定都在比他右的位置,也就是j+1位之后,设dp[i][j]是把第i个左括号移动到j右面(包括j)位中能得到的最大价值转移方程为:dp[i][j]=max(dp[i+1][j+1]+移动到这里能得到的值,dp[i][j+1]),而移动到这里所得到的值是(移动的这个左括号的值)*(他交换的右括号的值的和)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const LL INF = 0x3f3f3f3f3f3f3f3f;
char ch[1009];
int n, cnt[1009], p[1009];
LL sum[1009], a[1009], dp[2][1009];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
LL ans = 0, tot = 0;
scanf("%s", ch + 1);
cnt[n + 1] = dp[1][n + 1] = dp[0][n + 1] = sum[0] = 0;
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
dp[1][i] = dp[0][i] = 0;
}
for (int i = n; i >= 1; i--)
{
cnt[i] = cnt[i + 1] + (ch[i] == '(' ? 1 : 0);
if (ch[i] == ')')
{
sum[tot + 1] = sum[tot] + a[i];
tot++;
}
else p[i] = tot;
}
int now = 0;
for (int i = n; i >= 1; i--)
{
if (ch[i] == ')') continue;
for (int j = n - cnt[i] + 1; j >= i; j--)
dp[now ^ 1][j] = dp[now][j + 1] + 1LL * (sum[p[i]] - sum[n - j + 1 - cnt[i]]) * a[i];
for (int j = n - cnt[i]; j >= 1; j--)
dp[now ^ 1][j] = max(dp[now ^ 1][j], dp[now ^ 1][j + 1]);
now ^= 1;
ans = max(ans, dp[now][1]);
}
printf("%lld\n", ans);
}
return 0;
}