description: A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example:
  
    * bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); 
    * bracket sequences ")(", "(" and ")" are not. 
  
  
  
  Let's denote the concatenation of two strings x and y as x+y. For example, "()()" + ")(" = "()())(".
  
  You are given n bracket sequences s_1, s_2, ..., s_n. You can rearrange them in any order (you can rearrange only the strings themselves, but not the characters in them).
  
  Your task is to rearrange the strings in such a way that the string s_1 + s_2 + ... + s_n has as many non-empty prefixes that are RBS as possible.
  
  Input
  
  The first line contains a single integer n (1 ≤ n ≤ 20).
  
  Then n lines follow, the i-th of them contains s_i — a bracket sequence (a string consisting of characters "(" and/or ")". All sequences s_i are non-empty, their total length does not exceed 4 ⋅ 10^5.
  
  Output
  
  Print one integer — the maximum number of non-empty prefixes that are RBS for the string s_1 + s_2 + ... + s_n, if the strings s_1, s_2, ..., s_n can be rearranged arbitrarily.
  
  Examples
  
  Input
  
  
  2
  (
  )
  
  
  Output
  
  
  1
  
  
  Input
  
  
  4
  ()()())
  (
  (
  )
  
  
  Output
  
  
  4
  
  
  Input
  
  
  1
  (())
  
  
  Output
  
  
  1
  
  
  Input
  
  
  1
  )(()
  
  
  Output
  
  
  0
  
  Note
  
  In the first example, you can concatenate the strings as follows: "(" + ")" = "()", the resulting string will have one prefix, that is an RBS: "()".
  
  In the second example, you can concatenate the strings as follows: "(" + ")" + "()()())" + "(" = "()()()())(", the resulting string will have four prefixes that are RBS: "()", "()()", "()()()", "()()()()".
  
  The third and the fourth examples contain only one string each, so the order is fixed.
solution: #include <bits/stdc++.h>
  using namespace std;
  long long bs(vector<long long>& v, long long x) {
    long long l = 0;
    long long r = v.size();
    long long ans = r;
    while (l < r) {
      long long mid = (l + r) >> 1;
      if (x + v[mid] < 0) {
        ans = mid;
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    return ans;
  }
  signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long n;
    cin >> n;
    vector<string> s(n);
    vector<map<long long, vector<long long>>> mp(n);
    vector<vector<long long>> v(n);
    vector<long long> sm(n);
    for (long long i = 0; i < n; i++) cin >> s[i];
    for (long long i = 0; i < n; i++) {
      long long smm = 0;
      long long mi = 0;
      for (long long j = 0; j < s[i].length(); j++) {
        char c = s[i][j];
        if (c == '(')
          smm++;
        else
          smm--;
        mp[i][smm].push_back(j);
        mi = min(mi, smm);
        v[i].push_back(mi);
      }
      sm[i] = smm;
    }
    vector<long long> SM(1 << n);
    for (long long i = 0; i < 1 << n; i++) {
      for (long long j = 0; j < n; j++) {
        if ((i >> j) & 1) {
          SM[i] += sm[j];
        }
      }
    }
    vector<vector<long long>> dp(1 << n, vector<long long>(2, INT_MIN));
    dp[0][0] = 0;
    dp[0][1] = 0;
    for (long long i = 1; i < 1 << n; i++) {
      for (long long j = 0; j < n; j++) {
        if ((i >> j) & 1) {
          long long mask = i ^ (1 << j);
          if (v[j].back() + SM[mask] >= 0) {
            auto it = mp[j].find(-SM[mask]);
            long long tot = 0;
            if (it != mp[j].end()) tot = it->second.size();
            dp[i][0] = max(dp[i][0], dp[mask][0] + tot);
          } else {
            long long idx = bs(v[j], SM[mask]);
            long long tot = 0;
            auto it = mp[j].find(-SM[mask]);
            if (it != mp[j].end()) {
              tot = lower_bound(it->second.begin(), it->second.end(), idx) -
                    it->second.begin();
            }
            dp[i][1] = max(dp[i][1], dp[mask][0] + tot);
          }
          dp[i][1] = max(dp[i][1], dp[mask][1]);
        }
      }
    }
    cout << max(dp[(1 << n) - 1][0], dp[(1 << n) - 1][1]);
    return 0;
  }
  