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)

#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define IINF 100000000
#define INF 300000000000000000
#define MOD 998244353
#define mod 998244353
#define INT_MAX_ 2147483647
#define REP(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
#define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define me memset
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp1(pair<ll,ll>a,pair<ll,ll> b){
        if(a.se!=b.se)
        return a.se<b.se;
        else
        return a.fi<b.fi;
}
//----------------------------------------------------------------------
ll cnt[1000010][2];
//----------------------------------------------------------------------
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------  
    //ll begin_time=clock();
    //-------------------------------
    ll n;cin>>n;
    V<string>s;
    for(ll i=0;i<n;i++){
        string t;cin>>t;
        if(t!="")s.pb(t);
    }
    n=s.size();
    ll min_ml=INF,min_mr=INF;
    int xl=0,xr=0,xxl=0,xxr=0;
    for(ll i=0;i<n;i++){
        ll l=0,r=0;
        for(ll j=0;j<s[i].length();j++){
            if(s[i][j]=='('){
                l++;
            }
            else{
                if(l)l--;
                else r++;
            }
        }
        cnt[i][0]=r;
        cnt[i][1]=l;
        if(l==0){
            xl++;
            xxl=i;
        }
        if(r==0){
            xr++;
            xxr=i;
        }
        if(r&&l){
            chmin(min_ml,l);
        }
        if(l&&r){
            chmin(min_mr,r);
        }
    }
    if(min_mr==INF)min_mr=0;
    if(min_ml==INF)min_ml=0;
    V<Pll>lr;
    ll tl=0,tr=0;
    for(ll i=0;i<n;i++){
        //cout<<" "<<cnt[i][1]<<" "<<cnt[i][0]<<endl;cnt[i][1]
        lr.pb(mp(cnt[i][0],cnt[i][1]));
        tl+=cnt[i][1];
        tr+=cnt[i][0];
    }
    sort(all(lr));
    //reverse(all(lr));

    bool suzuken;
    bool color[1000010];
    memset(color,0,sizeof(color));
    ll first_l=0,last_r=0;
    for(ll i=0;i<n;i++){
        if(lr[i].se==0){
            first_l+=lr[i].fi;
            color[i]=1;
        }
        else if(lr[i].fi>lr[i].se){
            first_l+=lr[i].fi-lr[i].se;
            color[i]=1;
        }
        if(lr[i].fi==0){
            last_r+=lr[i].se;
            color[i]=1;
        }
        else if(lr[i].se>lr[i].fi){
            last_r+=lr[i].se-lr[i].fi;
            color[i]=1;
        }
    }

    bool kkt=1;
    ll nl=0,nr=0;
    nl+=first_l;
    for(ll i=0;i<n;i++){
        if(color[i]==1 )continue;
        nr+=lr[i].fi;
        if(nr>nl){
            kkt=0;
            break;
        }
        nl+=lr[i].se;
    }
    //cout<<first_l<<" "<<min_mr<<endl;
    //cout<<last_r<<" "<<min_ml<<endl;
    if(first_l>=min_mr&&last_r>=min_ml){
        suzuken=1;
    }
    else suzuken=0;
    bool abc=0;
    if(suzuken==1&&kkt&&tr==tl){
        if(xl&&xr){ 
            if(xl==1&&xr==1){
                if(xxr!=xxl){
                    abc=1;
                    cout<<"Yes"<<endl;
                }
            }
            else{
                abc=1;
                cout<<"Yes"<<endl;
            }
        }
        
    }
    if(abc==0){
        if(n==1){
            if(xl&&xr){
                abc=1;
                cout<<"Yes"<<endl;
            }
        }
    }
    if(abc==0)cout<<"No"<<endl;
    //-------------------------------  
    //ll end_time=clock();cout<<"time="<<end_time-begin_time<<"ms"<<endl;
    //-------------------------------
    return 0;
}
//----------------------------------------------------------------------