Input 3 4 ()() -1 -2 3 4 4 (()] -1 -2 3 4 4 [{]{ 1 2 3 4 Sample Output 7 1 0
Explanation
For first test case take last 2 elements: 3 + 4 = 7.
For second test case take the middle 2 elements: -2 + 3 = 1
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
using namespace std;
map<char,int> r;
ll x[100010];
ll sum[100010];
char y[100010];
int n;
int main(){
r[']']='[';
r['}']='{';
r['>']='<';
r[')']='(';
int t;
scanf("%d",&t);
while(t--){
// memset(sum,0,sizeof(sum)); 会超时
scanf("%d", &n);
getchar();
for(int i=1;i<=n;++i)
scanf("%c",&y[i]);
for(int i=1;i<=n;++i){
scanf("%lld",&x[i]);
sum[i]=0;
x[i]+=x[i-1];
}
stack<int> st;
ll s=0;
for(int i=1;i<=n;++i){
int u=0;
if(st.size()>0){
if(r[y[i]]==y[st.top()])
u=1;
}
if(u==1){
sum[i]=max(0ll,sum[st.top()-1]+x[i]-x[st.top()-1]);//((())())
s=max(s,sum[i]);
st.pop();
}
else
st.push(i);
}
printf("%lld\n", s);
}
return 0;
}