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)

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pll pair<ll,ll>
#define pb push_back
#define mp(x, y) make_pair((x), (y))
#define F first
#define S second
#define I insert
#define vll vector<ll>
#define vbl vector<bool>
#define vvll vector<vll>
#define vld vector<ld>
#define vb vector<bool>
#define vc vector<char>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()

template<class T> ostream& operator<<(ostream &os, vector<T> V){
	os << "[ ";
	for(auto v  : V) os << v << " ";
	return os << " ]";
}
template<class T> ostream& operator<<(ostream &os, set<T> V){
	os << "[ ";
	for(auto v  : V) os << v << " ";
	return os << " ]";
}
template<class T , class R > ostream& operator<<(ostream &os, map<T , R> V){
	os << "[ ";
	for(auto v  : V) os << "(" << v.first << ":" << v.second << ")";
	return os << " ]";
}
template<class T , class R > ostream& operator<<(ostream &os, pair<T , R> V){
	return os << "(" << V.first << "," << V.second << ")";
}

#define cerr cout
#define TRACE
#ifdef TRACE
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');
        cerr.write(names, comma - names) << " : " << arg1<<" | ";
        __f(comma+1, args...);
    }
    clock_t clk=clock();
    #define checktime() cout<<"\nTime Elapsed : "<<float(clock()-clk)/CLOCKS_PER_SEC<<endl
#else
    #define trace(...);
    #define checktime();
#endif

// const ll Mod = 1e9 + 7;
const ll Mod = 998244353;
const ll MAXN = 1e6;

void solve(){
    ll n;cin>>n;
    vll a(n,0),b(n,0),m(n,1e9);
    vpll c(n),x,y,z;
    ll to=0,tc=0;
    for(ll i=0;i<n;i++){
        string s;cin>>s;
        for(char cc:s){
            if(cc==')') b[i]++;
            else a[i]++;
            m[i] = min(m[i], a[i]-b[i]);
        }
        to+=a[i];tc+=b[i];
        // c[i]={m[i],a[i]-b[i]};
        if(a[i]-b[i]>=0 && m[i]>=0) x.pb({m[i],a[i]-b[i]});
        else if(a[i]-b[i]>=0) y.pb({m[i],a[i]-b[i]});
        else z.pb({m[i],a[i]-b[i]});
    }
    if(to!=tc){
        cout<<"No\n";
        return;
    }
    sort(all(x));reverse(all(x));
    sort(all(y));
    sort(all(z));reverse(all(z));
    ll val=0;
    for(auto pp:x){
        if(val+pp.F<0){
            cout<<"No\n";
            return;
        }
        val+=pp.S;
    }
    for(auto pp:y){
        if(val+pp.F<0){
            cout<<"No\n";
            return;
        }
        val+=pp.S;
    }
    for(auto pp:z){
        if(val+pp.F<0){
            cout<<"No\n";
            return;
        }
        val+=pp.S;
    }
    cout<<"Yes\n";
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout<<fixed<<setprecision(25);

    ll t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}