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>
#define REP(i,n) for(int i=0,i##_len=int(n);i<i##_len;++i)
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define All(x) (x).begin(),(x).end()
#define rAll(x) (x).rbegin(),(x).rend()
using namespace std;
using ll = long long;

template<typename T> class SegmentTree{

  private:
    typedef std::function<T(T,T)> F;
    int n;
    T d0;
    std::vector<T> vertex;
    F f;
  public:
    SegmentTree(F f,T d):d0(d),f(f){}
    void init(int _n){
        n=1;
        while(n<_n) n*=2;
        vertex.resize(2*n-1,d0);
    }
    void build(const std::vector<T> &v){
        int n_=v.size();
        init(n_);
        for(int i=0;i<n_;i++) vertex[n+i-1]=v[i];
        for(int i=n-2;i>=0;i--)
        vertex[i]=f(vertex[2*i+1],vertex[2*i+2]);
    }
    void update(int i,T x,F g){
        int k=i+n-1;
        vertex[k]=g(vertex[k],x);
        while(k>0){
            k=(k-1)/2;
            vertex[k]=f(vertex[2*k+1],vertex[2*k+2]);
        }
        return;
    }
    T query(int l,int r){
        T vl=d0,vr=d0;
        l+=n-1;
        r+=n-1;
        for(;l<=r;l/=2,r=r/2-1){
            if(~l&1) vl=f(vl,vertex[l]);
            if(r&1) vr=f(vertex[r],vr);
        }
        return f(vl,vr);
    }
};

template<typename T>
struct compress{
    std::map<T,int> zip;
    vector<T> unzip;
    compress(vector<T> x){
        unzip=x;
        sort(All(x));
        x.erase(unique(All(x)),x.end());
        REP(i,x.size()){
            zip[x[i]]=i;
        }
    }
    size_t operator[](int k){return zip[unzip[k]];}
};


int main(){
    using P = pair<int,int> ;
    int N;cin>>N;
    vector<P> A(N,make_pair(0,0));
    int left=0,right=0;
    REP(i,N){
        string s;cin>>s;
        REP(j,s.size()){
            if(s[j]=='(') A[i].second++;
            else{
                if(A[i].second>0) A[i].second--;
                else A[i].first++;
            }
        }
        if(A[i].first==0) left+=A[i].second;
        if(A[i].second==0) right+=A[i].first;
    }
    vector<P> V;
    set<P> st;
    int cnt=0;
    REP(i,N) if(A[i].first&&A[i].second){
        V.push_back(P(A[i].first,A[i].second-A[i].first));
        cnt++;
    }
    compress<P> z(V);
    SegmentTree<P> seg([](P a,P b){if(a.second>b.second) return a; return b;},P(-1,-1e9));
    seg.init(1e6+9);
    REP(i,cnt){
        seg.update(z[i],P(z[i],z.unzip[i].second),[](P a,P b){return b;});
        st.insert(V[i]);
    }
    while(cnt--){
        auto r = st.lower_bound(P(left,1e9));
        r--;
        P now = seg.query(0,(*r).first);
        left += now.second;
        seg.update(now.first,P(-1,-1e9),[](P a,P b){return b;});
    }
    if(left == right) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}