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>
using namespace std;
#define fast       ios::sync_with_stdio(0); cin.tie(0);cout.tie(0)
#define fs          first
#define sc          second
#define pb         push_back
#define nl          '\n'
#define all(a) a.begin(),a.end()
#define REP(i,start,end) for (int i = start; i <=end; i++)
#define RREP(i,end,start) for (int i=end; i>=start ;i--)
typedef long long LL;
class seq{
    public:
    int low, high, bal, id;
    seq(int a,int b, int c,int d)
    {
        low=a;
        high=b;
        bal=c;
        id=d;
    }
};
bool custom(seq a , seq b)
{
    if(a.low==b.low)
        return a.bal>b.bal;
    return a.low<b.low;
}
bool custom2(seq a , seq b)
{
    if(a.high==b.high)
        return a.bal<b.bal;
    return a.high<b.high;
}
void solve()
{
    int n;
    cin>>n;
    vector<seq> vect1,vect2;
    REP(i,1,n)
    {
        string s;
        cin>>s;
        int low=0;
        int high=0;
        int bal=0;
        REP(j,0,s.size()-1)
        {
            bal+=(s[j]=='(');
            bal-=(s[j]==')');
            low=min(low,bal);
        }
        bal=0;
        RREP(j,s.size()-1,0)
        {
            bal-=(s[j]=='(');
            bal+=(s[j]==')');
            high=min(high,bal);
        }
        seq sq(-low,-high,-bal,i);
        if(bal<0)
            vect1.pb(sq);
        else
            vect2.pb(sq);
    }
    sort(all(vect1), custom);
    sort(all(vect2), custom2);
    int bal=0;
    if(vect1.size()>=1)
    {
        REP(i,0,vect1.size()-1)
        {
            if(vect1[i].low>bal)
            {
                cout<<"No"<<nl;
                return;
            }
            bal+=vect1[i].bal;
            
        }
    }
    int bal2=0;
    if(vect2.size()>=1)
    {
        REP(i,0,vect2.size()-1)
        {
            if(vect2[i].high>bal2)
            {
               
                cout<<"No"<<nl;
                return;
            }
            bal2-=vect2[i].bal;
        }
    }
    if(bal==bal2)
    {
        cout<<"Yes"<<nl;
    } 
    else
        cout<<"No"<<nl;
}
int main()
{
    fast;
        solve();
}