#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } signed main(){ int n;cin>>n; string s;cin>>s; ll res=0; using P=pair; vector

v; ll p=0,ans=0,st=0; rep(i,n){ if(s[i]=='(') st++; else{ if(st==0) p++; else st--,ans+=n-i; } v.emplace_back(st,p); } sort(ALL(v),[](P a,P b){ ll afs=a.second+max(0ll,b.second-a.first); ll bfs=b.second+max(0ll,a.second-b.first); return afs