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++中容器的使用(二)

    第一章容器 第1条:慎重选择容器类型. 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器 ...

  2. oracle 序列改值

    1.oracle 序列改值 执行:Alter Sequence SQ_RM_FRAME Increment By 100; 执行:Select SQ_RM_FRAME.NextVal From Dua ...

  3. java菜鸟笔记(一)

  4. QT 实现拖放功能

    1. 文档拖放 获取文件名 mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #i ...

  5. hdoj1006--Tick and Tick

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  6. @RequestMapping

    可以设定访问的目录,与访问的方式 对象可以是类,也可以是方法 @RequestMapping(value = "/say",method = RequestMethod.GET) ...

  7. matplotlib中在for中画出多张图

    import matplotlib.pyplot as plt import numpy as np fig, axes = plt.subplots(2, 2) def showim(): for ...

  8. SQL Server 性能优化之RML Utilities:快速入门(Quick Start)(1)

      SQL Server 性能优化之RML Utilities:快速入门(Quick Start)(1) 安装Quick Start工具 RML(Replay Markup Language)是MS ...

  9. 为mac编写swift脚本

    代码示例: #!/usr/bin/env xcrun swift print("Hello World") 可以用Sublime Text编写,安装Swift包后有语法着色功能.然 ...

  10. ural 2017 Best of a bad lot

    2017. Best of a bad lot Time limit: 1.0 secondMemory limit: 64 MB A cruise liner hasn’t moved away f ...