Sequence Swapping_DP


BaoBao has just found a strange sequence {<$s_1$, $v_1$>, <$s_2$, $v_2$>, $\dots$, <$s_n$, $v_n$>} of length $n$ in his pocket. As you can see, each element <$s_i$, $v_i$> in the sequence is an ordered pair, where the first element $s_i$ in the pair is the left parenthesis '(' or the right parenthesis ')', and the second element $v_i$ 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 $k$, swap the $k$-th element and the $(k+1)$-th element in the sequence, and increase his score by $(v_k \times v_{k+1})$, if and only if $1 \le k < n$, $s_k =$ '(' and $s_{k+1} =$ ')'.

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 $T$, indicating the number of test cases. For each test case:

The first line contains an integer $n$ ($1 \le n \le 10^3$), indicating the length of the sequence.

The second line contains a string $s$ ($|s| = n$) consisting of '(' and ')'. The $i$-th character in the string indicates $s_i$, of which the meaning is described above.

The third line contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^3 \le v_i \le 10^3$). Their meanings are described above.

It's guaranteed that the sum of $n$ of all test cases will not exceed $10^4$.

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 $k = 2, 3, 5, 4$ in order.

For the second sample test case, the optimal strategy is to select $k = 2, 5$ in order.

#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int N=1000+10;
const ll inf=0x3f3f3f3f3f3f3f3f;

ll dp[N][N],pre[N][N];

struct node{
    bool what;
    int va;
}nn[N];

char ch[N];

int main(){
    freopen("in.txt","r",stdin);
    int T=rd();
    while(T--){
        int n=rd();
        memset(dp,-inf,sizeof(dp));

        memset(pre,0,sizeof(pre));
        scanf("%s",ch);
        dp[0][0]=0;
        for(int i=1;i<=n;i++){
            nn[i].va=rd();
            nn[i].what=ch[i-1]=='('?0:1;
        }
        for(int i=1;i<=n;i++){
            if(nn[i].what){
                ll tp=-inf;
                for(int j=i-1;j>=0;j--){
                    tp=max(tp,dp[i-1][j]);
                    dp[i][j]=tp+pre[i-1][j]*nn[i].va;
                    pre[i][j]=pre[i-1][j];
                }
            }else{
                for(int j=1;j<=i;j++){
                    dp[i][j]=dp[i-1][j-1];
                    pre[i][j]=pre[i-1][j-1]+nn[i].va;
                }
            }
        }
        ll ans=0;
        for(int i=0;i<=n;i++){
            ans=max(ans,dp[n][i]);
        }
        cout<<ans<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值