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>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,n) for(ll i=0;i<(n);++i)
#define REPR(i,n) for(ll i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template<typename T>
void print(const vector<T> &x) {
    int n = x.size();
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x) {
    int n = x.size();
    rep(i,n) {
        rep(j,x[i].size()) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

template<typename T>
void print(const vector<T> &x, int n) {
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x, int n, int m) {
    rep(i,n) {
        rep(j,m) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void input_init() {
    cin.tie(0); ios::sync_with_stdio(false);
}

void brute_force(vector<string> &s, int n) {
    sort(s.begin(), s.end());
    do {
        string t;
        rep(i,n) t+=s[i];
        stack<char> st;
        rep(i,t.length()) {
            if (st.empty()) {
                st.push(t[i]);
            }
            else if (st.top() == '(') {
                if (t[i] == ')') {
                    st.pop();
                }
                else {
                    st.push(t[i]);
                }
            }
            else {
                st.push(t[i]);
            }
        }

        if (st.empty()) {
            cout << "Yes" << '\n';
            cout << t << '\n';
            return;
        }
    } while (next_permutation(s.begin(), s.end()));

    cout << "No" << '\n';
}

int main(int argc, char const *argv[]) {
    int n; cin>>n;
    vector<string> s(n);
    vector<ll> a(n);
    map<char, int> mp;

    rep(i,n) {
        cin>>s[i];
        stack<char> st;
        for (auto &&e: s[i]) {
            if (st.empty()) {
                st.push(e);
            }
            else if (st.top() == '(') {
                if (e == ')') st.pop();
                else st.push(e);
            }
            else {
                st.push(e);
            }
        }

        while (!st.empty()) {
            mp[st.top()]++;
            st.pop();
        }
    }
    // brute_force(s,n);

    if (mp['('] == mp[')']) {
        cout << "Yes" << '\n';
    }
    else {
        cout << "No" << '\n';
    }

    return 0;
}