#include #include #include using namespace std; typedef long long lint; typedef vectorvi; typedef pairpii; #define rep(i,n)for(int i=0;i<(int)(n);++i) struct cmp{ bool operator()(pii a,pii b)const{ int f=a.first-a.second; f=b.first+max(f,-b.second); int s=b.first-b.second; s=a.first+max(s,-a.second); return f==s?-a.second<-b.second:f>n>>s; int mi=0,cur=0; vector kenchon(n); rep(i,n){ cur+=s[i]=='('?1:-1; mi=min(mi,cur); kenchon[i]=pii(cur,mi); } sort(kenchon.begin(),kenchon.end(),cmp()); lint sum=(lint)n*(lint)(n+1)/2; lint ans=0; lint cursum=0; rep(i,n){ ans=kenchon[i].first+max(ans,(lint)-kenchon[i].second); cursum+=kenchon[i].first; } cout<