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.

就是一个dp,dp[i][j]表示第i个左括号扩到第j个时候的最大值。

然后更新即可。

但是他没到的不能和他已经到的同等对待。

玛德智障,

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e3+;
char s[N];
int n,p[N],nxt[N];
long long sum[N],v[N];
long long mx[][N];
int main(){
int T;
while(scanf("%d",&T)!=EOF){
while(T--){
scanf("%d",&n);
long long ans=;
int tot=,f=;
scanf("%s",s+);
for(int i=;i<=n;++i) scanf("%lld",v+i);
for(int i=n;i>=;--i) if(s[i]=='(') p[++tot]=i;
for(int i=;i<=n;++i) sum[i]=sum[i-]+(s[i]==')'?v[i]:);
memset(nxt,,sizeof(nxt));
for(int i=;i<=n;++i) if(s[i]==')') {
if(f) nxt[f]=i;
f=i;
}
for(int i=;i<=n;++i) mx[][i]=mx[][i]=;
mx[][]=mx[][]=-(1LL<<);
int cur=;
for(int i=;i<=tot;++i) {
for(int j=p[i]+;j<=n;++j) if(s[j]==')') mx[cur][j]=v[p[i]]*(sum[j]-sum[p[i]])+mx[cur^][j];
for(int j=n;j>=p[i];--j) if(s[j]==')') mx[cur][j]=max(mx[cur][nxt[j]],mx[cur][j]);
for(int j=p[i]-;j>=;--j) if(s[j]==')') mx[cur][j]=max(mx[cur^][j],mx[cur][nxt[j]]);
for(int j=;j<=n;++j) if(s[j]==')') ans=max(ans,mx[cur][j]);
cur^=;
}
printf("%lld\n",ans);
}
}
}

D:Sequence Swapping的更多相关文章

  1. 第15届浙江省赛 D Sequence Swapping(dp)

    Sequence Swapping Time Limit: 1 Second      Memory Limit: 65536 KB BaoBao has just found a strange s ...

  2. ZOJ 4027 Sequence Swapping(DP)题解

    题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...

  3. zoj4027 Sequence Swapping

    首先容易想到二维方程dp(i,j),表示第i个左括号去匹配到第j个右括号时产生的最大值,但如果如此表示的话,首先需要枚举(i,j)以及一个k即dp(i-1,k). 考虑变化dp(i,j)的表示方法,可 ...

  4. ZOJ4027 Sequence Swapping DP

    link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4027 题意: 有一个括号序列,每个括号对应一个值,现在可以使得相 ...

  5. 2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

    我是铁牌选手 这次比赛非常得爆炸,可以说体验极差,是这辈子自己最脑残的事情之一. 天时,地利,人和一样没有,而且自己早早地就想好了甩锅的套路. 按理说不开K就不会这么惨了啊,而且自己也是毒,不知道段错 ...

  6. [POJ 1674] Sorting by Swapping

    Sorting by Swapping Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9514   Accepted: 50 ...

  7. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  8. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  9. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

随机推荐

  1. web前端项目中遇到的一些问题总结(08.23更新)

    个人网站 https://iiter.cn 程序员导航站 开业啦,欢迎各位观众姥爷赏脸参观,如有意见或建议希望能够不吝赐教! 写一些最近工作中Vue项目中遇到的问题. 巴啦啦小魔仙,污卡拉,全身变,小 ...

  2. JavaScript面向对象那些东西-继承

    继承 父类里有些属性方法 子类想把父类中的这些属性方法 继承过来给子类自己的实例也用用 ( ps: →_→ 能不能专业点 没文化真可怕 ) 一.原型链继承 // 原型链继承:把子类的原型作为父类的实例 ...

  3. IT成长中的龟兔赛跑

    IT成长中的龟兔赛跑 相信"龟兔赛跑"的故事大家都听过吧,那就让咱给各位看官讲讲我看到的一些事情吧.      最近看到很多文章总是叹息网管如何,起得比鸡早,睡得比狗晚,吃得比猪差 ...

  4. 为给定字符串生成MD5指纹

    import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache ...

  5. Binary Index Tree

    0 引言 Leetcode307 这道题给一个可变数组,求从\(i\)到\(j\)的元素之和. 一个naive的做法是,每次查询都从\(i\)累加到\(j\): class NumArray { pu ...

  6. csp-j2019游记

    我一pj蒟蒻这点水平还来写游记? 算了,毕竟是第一次,记录一下吧 noip->csp 话说我跟竞赛是不是天生八字不合啊...... 小学的时候学小奥,等我开始报名比赛,当时似乎所有竞赛都被叫停了 ...

  7. pycharm 新建文件后选错文件格式怎么改

    经常在新建文件的时候,忘记填写文件后缀,导致文件无默认格式,而且同名字的文件怎么改都改不成想要的格式,所以随手记录一下怎么修正: 原因:肯定是pycharm已经默认指定了一个格式,所以再重复新建同样名 ...

  8. Arduino编程基础1.1

    Arduino编程语言是建立在C/C++语言基础上的,即以C/C++语言为基础,把AVR单片机(微控制器)相关的一些寄存器参数设置等进行函数化,以利于开发者更加快速地使用.其主要使用的函数包括数字I/ ...

  9. django+nginx+uwsgi的生产环境部署(Ubuntu16.04)

    一,准备工作: 代码一定要能本地跑起来! 各种基础包的安装略默认已经安装python3,nginx,uwsgi等基础依赖,注意版本问题. 本地setting.py文件修改如下(改为生产模式,把debu ...

  10. SpringBoot:整合Druid、MyBatis

    目录 简介 JDBC 导入依赖 连接数据库 CRUD操作 自定义数据源 DruidDataSource Druid 简介 配置数据源 配置 Druid 数据源监控 配置 Druid web 监控 fi ...