求助:WA on #10
查看原帖
求助:WA on #10
982540
123456a01楼主2024/1/29 13:54

思路如这篇博客

#include<bits/stdc++.h>
using namespace std;
const int N=3e6+5;
int n,top;
long long a[N],f[N],ans;
char s[N];
priority_queue<long long> q[N];
int sta[N];
inline int rd(){
	int x=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		x=(x<<3)+(x<<1)+(c^48);
		c=getchar();
	}
	return x;
}
inline void clear(int m){
	while(q[m].size()) q[m].pop();
}
int main(){
	n=rd();
	scanf("%s",s+1);
	for(int i=1;i<=n;i++) a[i]=rd();
	for(int i=1;i<=n;i++){
		f[i]=max(f[i],f[i-1]);
		if(s[i]=='('){
			top++;
			sta[top]=i;
		}
		else{
			clear(top+1);
			if(top){
				q[top].push(f[sta[top]-1]-a[sta[top]]);
				f[i]=max(f[i],q[top].top()+a[i]);
				ans=max(ans,f[i]);
				top--;
			}
		}
	}
	printf("%lld",ans);
	return 0;
}
2024/1/29 13:54
加载中...