#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,a[3000005],d;
ll dp,ans = 0;
unordered_map<ll,bool> vis;
unordered_map<ll,ll> m;
stack<ll> st;
char s[3000005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
srand(time(NULL));
m[0] = -1 * a[1];
for(int i=1;i<=n;i++) cin>>s[i];
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++){
d = rand() % 114514;
if(s[i] == '('){
st.push(d);
ans += d;
}
else if(st.size() != 0){
ans -= st.top();
st.pop();
}
else ans += d;
if(vis[ans] == 0 && ans != 0) m[ans] = -1e8;
dp = max(dp,m[ans] + a[i]);
m[ans] = max(m[ans],dp - a[i+1]);
vis[ans] = 1;
}
cout<<dp;
return 0;
}
全wa但本地测的都对了 然后还t了,求调,thx