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)

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

class FBracketSequencing {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        vector<string> S(N);
        REP(i, N) cin >> S[i];

        int lo = 0;
        int ro = 0;
        vector<pair<int, int>> v;
        for (auto& s : S) {
            int cl = 0;
            int cr = 0;
            for (auto c : s) {
                if (c == '(') {
                    cl++;
                } else {
                    if (cl > 0) {
                        cl--;
                    } else {
                        cr++;
                    }
                }
            }
            if (cr == 0) {
                lo += cl;
            } else if (cl == 0) {
                ro += cr;
            } else {
                v.emplace_back(cr, cl);
            }
        }

        int cl = lo;
        sort(v.begin(), v.end());
        for (auto kv : v) {
            int r, l;
            tie(r, l) = kv;
            if (cl < r) {
                cout << "No" << endl;
                return;
            }
            cl -= r;
            cl += l;
        }

        if (cl == ro) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
};


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    FBracketSequencing solver;
    solver.solve(in, out);
    return 0;
}