#include using namespace std; int main(){ int N; cin >> N; string S; cin >> S; vectorlscore(N + 1, -1e9); vectorrscore(N + 1, -1e9); lscore[0] = 0; rscore[N] = 0; { int score = 0; int sum = 0; bool exist = false; for(int i = 0; i < N; i++){ if(S[i] != '(' && sum <= 1){ lscore[i + 1] = score + exist; } if(S[i] == ')' || (S[i] == '?' && (exist || i == N - 1 || S[i + 1] == '('))){ if(sum == 0){ score += exist; exist = false; } else{ exist = true; sum--; } } else{ sum++; exist = true; } } } { int score = 0; int sum = 0; bool exist = false; for(int i = N - 1; i >= 0; i--){ if(S[i] != ')' && sum == 0){ rscore[i] = score + exist; } if(S[i] == '(' || (S[i] == '?' && (exist || i == 0 || S[i - 1] == ')'))){ if(sum == 0){ score += exist; exist = false; } else{ exist = true; sum--; } } else{ sum++; exist = true; } } } int ans = 0; for(int i = 0; i <= N; i++){ ans = max(ans, lscore[i] + rscore[i]); } cout << ans << endl; }