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: Accepted)

#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("input.txt");ofstream coutt("output.txt");


stack<char> getstk(const string &s){
    stack<char> stk;
    for(char c:s){
        
        if(c=='('){
            stk.push(c);
        }else{
            
            if(!stk.empty() && stk.top()=='('){
                stk.pop();
            }else{
                stk.push(c);
            }
        }
        
    }
    return stk;
}

string f(const string &s){
    stack<char> stk=getstk(s);
    string ss="";
    while(!stk.empty()){
        ss+=stk.top();
        stk.pop();
    }
    reverse(all(ss));
    return ss;
}

signed main(){
    FASTER;
    int n;cin>>n;
    vector<string> v(n),opn,cls,mid;
    
    for(string &s:v){
        cin>>s;
        s=f(s);
        if(s=="")continue;
        bool op=1,cl=1;
        for(char c:s)op&=(c=='(');
        for(char c:s)cl&=(c==')');
        if(op)opn.push_back(s);
        if(cl)cls.push_back(s);
        if(!op && !cl)mid.push_back(s);
    }
    
    sort(all(mid),[](const string &a,const string &b){
            int aka=0,bka=0;
            for(const char &c:a){
                if(c=='(')aka++;
                else aka--;
            }
            for(const char &c:b){
                if(c=='(')bka++;
                else bka--;
            }
            return aka>bka;
    });
    
    
    string s="";
    for(string a:opn)s+=a;
    for(string a:mid)s+=a;
    for(string a:cls)s+=a;
    
    if(getstk(s).empty())cout<<"Yes";
    else cout<<"No";
}