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: Time Limit Exceeded)

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define sz(a) (int) (a).size()
#define all(a) (a).begin(), (a).end()
#define x first
#define y second

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 2e5 + 10;
const int p = 998244353;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
    return a.y - a.x > b.y - b.x;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    multiset<pair<int, int>> c;
    int aa = 0, bb = 0;
    int sum = 0;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        int b = 0;
        int mn = 0;
        for (auto &el: s) {
            if (el == '(') b++;
            else b--;
            mn = min(mn, b);
        }
        a[i].x = abs(mn);
        a[i].y = abs(b - mn);
        sum += (a[i].y - a[i].x);
        if (a[i].x == 0) {
            bb += a[i].y;
        }
        if (a[i].y == 0) {
            aa += a[i].x;
        }
        if (a[i].x && a[i].y) {
            c.insert(a[i]);
        }
    }
    if (sum == 0) {
        int b = bb;
        while (!c.empty()) {
            multiset<pair<int, int>, decltype(cmp)*> add(cmp);
            while (!c.empty()) {
                auto it = *c.begin();
                if (it.x <= b) {
                    c.erase(c.begin());
                    add.insert({it.x, it.y});
                } else {
                    break;
                }
            }
            if (add.empty()) {
                cout << "No\n";
                return 0;
            }
            while (!add.empty()) {
                auto it = *(add.begin());
                if (it.y - it.x < 0) break;
                b += it.y - it.x;
                add.erase(add.begin());
            }
            for (auto &el : add) {
                c.insert(el);
            }
        }
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }

    return 0;
}