Sequence Swapping


Time Limit: 1 Second      Memory Limit: 65536 KB

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.

题解:
一对括号交换相当于左括号向右移一位,右括号想左移一位,
所有交换中所有左括号的相对位置不变,右括号同理。
可以从右边开始枚举每个左括号移动的位置,左括号移动
到一个点的加分等于此括号到这个点的加分加上之前的括号
所能到达的点的最大得分。
代码: #include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=;
char t[maxn];
ll a[maxn],dp[maxn][maxn];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
int n;scanf("%d",&n);
scanf("%s",t+);
for(int i=;i<=n;i++)scanf("%lld",&a[i]);
int now=;
ll ma=;
for(int i=n;i>=;i--)
{
if(t[i]=='(')
{
now++;
for(int j=i+;j<=n;j++)
{
if(t[j]=='(')dp[now][j]=dp[now][j-];
else dp[now][j]=a[i]*a[j]+dp[now][j-];
ma=max(dp[now][j]+dp[now-][j],ma);
}
for(int j=i;j<=n;j++)dp[now][j]+=dp[now-][j];
for(int j=n;j>;j--)dp[now][j-]=max(dp[now][j-],dp[now][j]);
}
}
printf("%lld\n",ma);
}
return ;
}

第15届浙江省赛 D Sequence Swapping(dp)的更多相关文章

  1. 第15届浙江省赛 E LIS

    LIS Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge DreamGrid is learning the LI ...

  2. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

  3. CSUST 第15届 校赛总结

    一直想记录一下自己的比赛,却感觉空间说说有点不适,思考了一番还是打算放到自己的博客园 这次比赛总体来说还是不错,签到还是稳的一批,基本前四小时都在rk1 开局切了几道签到题,然后开了一道思维gcd,正 ...

  4. ZOJ 3872 Beauty of Array DP 15年浙江省赛D题

    也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...

  5. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  6. ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

  7. ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds      Me ...

  8. 第十五届浙江省赛 F Now Loading!!!

    Now Loading!!! Time Limit: 1 Second      Memory Limit: 131072 KB DreamGrid has  integers . DreamGrid ...

  9. 2016第十三届浙江省赛 D - The Lucky Week

    D - The Lucky Week Edward, the headmaster of the Marjar University, is very busy every day and alway ...

随机推荐

  1. Go 文件操作

    一.读取文件 普通版 ioutil版 bufio版 二.文件写入 普通版 ioutil版 bufio版 三.文件复制 ioCopy 1.普通版读取文件 package main import ( &q ...

  2. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  3. freemarker 异常处理

    SSH2处理方案: freemarker文件如果出错,网站的前台页面会报出很明显的错误-焦黄的背景,血红的文字,很不利于用户体验的.如何修改这个问题呢?首先需要在struts.xml配置文件里添加下面 ...

  4. 多校hdu-5775 Bubble sort(线段树)

    题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距: 分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用 ...

  5. 最长k可重区间集

      P3358 最长k可重区间集问题 P3357 最长k可重线段集问题 P3356 火星探险问题 P4012 深海机器人问题 P3355 骑士共存问题 P2754 [CTSC1999]家园 题目描述 ...

  6. java 项目中每个jar包的作用总结

    别人的总结 1.Struts2 1)commons-fileupload :2)common-io:文件上传 3)commons-lang:它扩展了标准 java.langAPI ArrayUtils ...

  7. SpringMVC中响应json数据(异步传送)

    1.首先导入3个jar包: jackson-annotations-2.1.5.jar jackson-core-2.1.5.jar jackson-databind-2.1.5.jar JSON所需 ...

  8. JBOSS invoker GETSHELL(PHP版)

    <?php $target = @$argv[1]; $procotol = @$argv[2]; if ($argc < 2) { print "[-]:php Jboss.p ...

  9. 0.00-050613_zc

    1. ROM bios --> 启动盘第一个扇区(此处内容为boot) 加载到 内存位置0x7C00(31KB) --> 执行权转移(也就相当于跳转) boot程序主要功能:把 软盘/映像 ...

  10. js轮训

    Later.js,一个独立的JavaScript类库,提供了循环事件触发的高级应用,可以为项目替换以上两种方法. 通常,我们要完成复杂的时间预定,需要大量的if...else语句.Later.js提供 ...