aboutsummaryrefslogtreecommitdiff
path: root/codechef/DenseBracketSequence/main.cpp
blob: e26df3c1a5b5b192cf0e4507619783659cfb71f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int cls = count(s.begin(), s.end(), ')');
    int opn = 0;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      cls -= (s[i] == ')');
      opn += (s[i] == '(');
      ans = max(ans, min(opn, cls));
    }
    cout << n - ans * 2 << endl;
  }
}