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. c# 图片 与 BASE64 字符串 互相转换。

    using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...

  2. Android中设置自己软件的铃声+震动

    有时候一些通讯软件需要这些个功能,比如说收到短信,通知等,要求手机发出铃声,或震动,或发光以提示用户知晓. 往往手机都是有默认设置的,比如说用户开启了铃声+震动:只铃声不震动:完全静音等等... 这个 ...

  3. CentOS 7 安装 docker-compose

    compose是用来在docker中定义和运行复杂应用的小工具,比如在一个文件中定义多个容器,只用一行命令就可以让一切就绪并运行. 安装pip: 这里显示,找不到相应的包?? 说没有python-pi ...

  4. JAVA 写中文字符串到指定文件 中文乱码 问题解决

    之前试过下面代码里面的注释掉的 方法,都不行,后来想到了不如指定编码格式试试,果真可以了. String as= “中文字符”; //byte[] b = as.getBytes(); try{ Fi ...

  5. 关于谷歌浏览器(chrome)的一些好用的插件推荐

    很多在测试时候都可以使用 第一部分: A:Adblock Plus for Google Chrome™https://chrome.google.com/webstore/detail/cfhdoj ...

  6. java从小白到架构师大牛必看书籍

    一.基础类 1.<ThinkingJava>,入门第一位是建立正确的概念. 2.<Core Java>,我没系统读过,这本书更贴近实践,更多API的介绍,同样,更新也更频繁. ...

  7. Get UTI (uniform type identifier) and ContentType

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  8. Asp.net 使用 Jsonp

    简介 由于JavaScript的安全机制,ajax不支持跨域调用.所以出现了jsonp. 实现 服务器 public string Jsonp(string name) { string result ...

  9. GEF入门实例_总结_06_为编辑器添加内容

    一.前言 本文承接上一节:GEF入门实例_总结_05_显示一个空白编辑器 在上一节我们为我们的插件添加了一个空白的编辑器,这一节我们将为此编辑器添加内容. 二.GEF的MVC模式 在此只简单总结一下, ...

  10. python-Django初体验

    1.搭建Django开发环境 2.创建工程与应用 CentOS6.5环境下 Python 2.6 ipython 1.2.1 Django 1.6.5 pip install -y django == ...