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;
    }
}


猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80150286
今日推荐