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)

    //For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;

    vector<pii> v,v2;

    bool comp(pii a,pii b)
    {
        if(a.s!=b.s)
            return a.s>b.s;
        return a.f<b.f;
    }

    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            int n,i,j;
            cin>>n;
            string ar[n+1];
            int ar2[n+1],ct,ct2,mini;
            for(i=1;i<=n;++i)
            {
                cin>>ar[i];
                ct=0,ct2=0,mini=0;
                for(j=0;j<ar[i].size();++j)
                {
                    if(ar[i][j]=='(')
                        ++ct;
                    else
                        --ct;
                    mini=min(mini,ct);
                }
                mini=mini*-1;
                // cout<<mini<<" "<<ct<<endl;
                if(ct>=0)
                {
                    v.pb({mini,ct});
                }
                else
                {
                    v2.pb({mini,ct});
                }
            }
            sort(v.begin(),v.end());
            ct=0;
            for(auto it:v)
            {
                if(ct<it.f)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
                ct=ct+it.s;
            }
            // cout<<ct<<endl;
            sort(v2.begin(),v2.end(),comp);
            for(auto it:v2)
            {
                if(ct<it.f)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
                ct=ct+it.s;
            }
            if(ct!=0)
            {
                cout<<"No"<<endl;
                    return 0;
            }
            cout<<"Yes"<<endl;
        }
        return 0;
    }