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: Runtime Error)

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
int pp[1000010] = {};
int min_val[1000010] = {};
int mm[1000010] = {};
string S[1000010];
bool use[1000010] = {};
typedef struct {
    ll a;
    ll b;
    ll c;
} P;


void solve(){
    ll N;
    cin >> N;
    rep(i,0,N) cin >> S[i];
    // if(N >= 5000) exit(1);
    ll c0 = 0, c1 = 0;
    rep(i,0,N) min_val[i] = 1e9;
    rep(i,0,N){
        int r = 0;
        for(auto &e: S[i]){
            if(e == '('){
                c0++;
                pp[i]++;
                r++;
            }else{
                c1++;
                r--;
                pp[i]--;
            }
            min_val[i] = min(min_val[i], r);
            mm[i] = max(mm[i], r);
        }
    }
    if(c0 != c1){
        print("No");
        return;
    }
    ll cur = 0;
    bool ok = true;
    ll cnt = 0;
    // rep(i,0,N){
    //     print(pp[i], min_val[i]);
    // }
    vector<P> lp;
    rep(i,0,N) lp.push_back({pp[i], min_val[i], mm[i]});
    sort(lp.begin(), lp.end(), [](P l1, P l2){return l1.a + l2.a > abs(l1.b + l2.b);});
    rep(i,0,N){
        // print(lp[i].b);
        if(cur + lp[i].b < 0){
            ok = false;
            break;
        }
        cur += lp[i].a;
    }

    // while(cnt < N){
    //     ll max_idx = -1, max_val = -INF;

    //     rep(i,0,N){
    //         if(use[i]) continue;
    //         if(cur + min_val[i] >= 0){
    //             if(max_val < pp[i]){
    //                 max_val = pp[i];
    //                 max_idx = i;
    //             }
    //         }
    //     }
    //     if(max_idx == -1){
    //         ok = false;
    //         break;
    //     }
    //     print(max_idx);
    //     use[max_idx] = true;
    //     cur += max_val;
    //     // print(cur);
    //     cnt++;
    // }


    if(ok){
        print("Yes");
    }else{
        print("No");
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}