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.fi!=b.fi)
        return a.fi<b.fi;
        else
        return a.se>b.se;
}
//----------------------------------------------------------------------
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;
        s.pb(t);
    }
    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;

    }
    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),cmp1);
    //reverse(all(lr));
    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].fi==0){
            first_l+=lr[i].se;
            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;
        if(lr[i].fi>lr[i].se)continue;
        nr+=lr[i].fi;
        if(nr>nl){
            kkt=0;
            break;
        }
        nl+=lr[i].se;
    }

    if(kkt&&tr==tl){
        cout<<"Yes"<<endl;
    }
    else cout<<"No"<<endl;
    //-------------------------------  
    //ll end_time=clock();cout<<"time="<<end_time-begin_time<<"ms"<<endl;
    //-------------------------------
    return 0;
}
//----------------------------------------------------------------------