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: Accepted)

// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

const int MAX_N = TEN(6);
int n;
string s[MAX_N];

int main() {
    fastio();

    cin >> n;
    forn(i, n) cin >> s[i];

    vec<pii> pos, neg;
    forn(i, n) {
        int cur = 0;
        int mn = 0;
        for (char c : s[i]) {
            if (c == '(') {
                cur++;
            } else {
                cur--;
            }
            chmin(mn, cur);
        }
        if (cur >= 0) {
            pos.emplace_back(mn, cur);
        } else {
            neg.emplace_back(mn - cur, -cur);
        }
    }

    sort(all(pos), greater<pii>());
    sort(all(neg), greater<pii>());

    int cur1 = 0;
    for (auto [mn, end] : pos) {
        if (cur1 + mn < 0) {
            cout << "No\n";
            return 0;
        }

        cur1 += end;
    }

    int cur2 = 0;
    for (auto [mn, end] : neg) {
        if (cur2 + mn < 0) {
            cout << "No\n";
            return 0;
        }

        cur2 += end;
    }

    cout << (cur1 == cur2 ? "Yes\n" : "No\n");

    return 0;
}