#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); } // for(auto &p:v) cout<r.first-r.second;}); ll x=0; for(auto &p:v){ ll add=min(x,p.second); x-=add,ans+=add; x+=p.first; } cout<