Problem:
Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14 100兆
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}

int main(){
  ll N;
  cin >> N;
  vector<string> S(N);
  rep(i, N){
    cin >> S.at(i);
  }
  vector<pair<ll, ll>> d(N);
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
  priority_queue<pair<ll, ll>> r;
  rep(i, N){
    ll c = 0;
    ll m = 0;
    rep(j, sz(S.at(i))){
      if(S.at(i).at(j) == '('){
        c++;
      }
      else{
        c--;
        chmin(m, c);
      }
    }
    d.at(i) = make_pair(m, c);
    if(c > 0){
      q.push(d.at(i));
    }
    else{
      r.push(make_pair(m-c, -c));
    }
  }
  ll now = 0;
  set<pair<ll, ll>> hold;
  while(!q.empty()){
    pair<ll, ll> p = q.top();
    q.pop();
    if(now + p.first < 0){
      cout << "No" << endl;
      return 0;
    }
    now += p.second;
  }
  while(!r.empty()){
    pair<ll, ll> p = r.top();
    r.pop();
    if(now + p.first-p.second < 0){
      cout << "No" << endl;
      return 0;
    }
    now -= p.second;
  }
  if(now != 0){
    cout << "No" << endl;
  }
  else{
    cout << "Yes" << endl;
  }
}