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;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL); 

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define ld long double


#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template <typename T,typename U>pair<T,U> operator+(const pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second};}
typedef long long int ll;
#define int ll
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ll mod =1e18;
const ll MAX_NODES =200001;
const ll MAX_LOG =50;
const ll maxn = 400000+1;
int bal(string s){
  int bal = 0;
  for(auto i:s)
      if(i =='(')
        bal++;
      else
      {
        bal--;
      }
    return bal;
      
}
int minim(string s){
  int bal = 0,minim = 1e9;
  for(auto i:s){
      if(i =='(')
        bal++;
      else
      {
        bal--;
      }
      minim = min(minim,bal);
      }
    return minim;
  
}
int32_t main()
{ 

  IOS 
  int n;
  cin>>n;
  string s[n];
  int tot = 0;
  vector<pair<int,int>> v,v1;
  for(int i = 0;i<n;i++){
    cin>>s[i];
    tot+=bal(s[i]);
    if(bal(s[i])>=0)
    v.pb({minim(s[i]),bal(s[i])});
    else
    {
      v1.push_back({minim(s[i]),bal(s[i])});
    } 
  }
  if(tot!=0){
    cout<<"No"<<endl;
    return 0;
  }
  sort(v.begin(),v.end(),greater<pair<int,int>>());
  sort(v1.begin(),v1.end(),greater<pair<int,int>>());

  //cout<<v<<endl;
  int bal = 0;
  for(int i = 0,j = 0;i<v.size();i = j){
    int tempbal = 0;
    while(j<v.size() and v[j].first>=(-bal))
      {
        tempbal+=v[j].second;
        j++;
      }
    //trace(j);
    bal+=tempbal;
    if(i == j){
      cout<<"No"<<endl;
      return 0;
    }
  }
  for(int i = 0,j = 0;i<v1.size();i = j){
    vector<pair<int,int>> temp;
    while(j<v1.size() and v1[j].first>=(-bal))
      {
        temp.push_back({v1[j].second,v1[j].first});
        j++;
      }
      if(i == j){
        cout<<"No"<<endl;
        return 0;
      }
    sort(temp.begin(),temp.end(),greater<pair<int,int>>());
    for(int  j = 0;j<temp.size();j++){
      if(temp[j].second+bal<0){
        cout<<"No"<<endl;
        return 0;
      }
      bal+=temp[j].first;
    }
  }
  cout<<"Yes"<<endl;
  

  




}