#include #define INF 1000000007 #define LINF 1000000000000000007 using namespace std; typedef long long Int; typedef pair P; int n; P r[100000]; string s; int main(){ cin >> n; cin >> s; if(s[0] == ')') r[0] = make_pair(1, 0); else r[0] = make_pair(0,0); bool y = 0, x = 0; if(s[0] == ')') y = 1; else x = 1; for(int i = 1; i < n; i++){ r[i].first = r[i-1].first; if(s[i] == ')'){r[i].first++; y = 1;} else x = 1; r[i].second = i; } if(!x || !y){cout << 0 << endl; return 0;} sort(r, r+ n); Int cnt = 0, migi = 0, hidari = 0; for(int i = 1; i < n; i++){ int j = i, k = i; while(r[j-1].first == r[j].first){ j++; } i = j; while(j >= k){ if(j != 0) migi += r[j].first; hidari += r[j-1].second - r[j-1].first + 1; cnt += min(migi, hidari)*2; migi -= min(migi, hidari); hidari -= min(migi, hidari); j--; } } cout << cnt << endl; return 0; }