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>
#define x first
#define y second
#define int int long long // 2e9 & 9e18
using namespace std; const int inf = numeric_limits<int>::max();
struct __io{__io(){ios_base::Init i; ios_base::sync_with_stdio(0); cin.tie(0);}} __io; // fast I/O
#ifdef LOCAL // setting up print debugging (yes lol)
template<class K, class V>ostream& operator<<(ostream&s,const pair<K,V>&p){s<<'<'<<p.x<<", "<<p.y<<'>';return s;}
template<class T, class=typename T::value_type, class=typename enable_if<!is_same<T,string>::value>::type>
ostream& operator<<(ostream&s,const T&v){s<<'[';for(auto&x:v){s<<x<<", ";}if(!v.empty()){s<<"\b\b";}s<<']';return s;}
void __prnt(){cerr<<endl;} template<class T, class...Ts>void __prnt(T&&a,Ts&&...etc){cerr<<a<<' ';__prnt(etc...);}
#define print(...) __prnt(__VA_ARGS__)
#else
#define print(...)
#endif

typedef pair<int, int> pii;

bool solve() {
    int n;
    cin >> n;
    vector<pii> a(n);
    string s;
    int total = 0;
    for (int i = 0; i < n; ++i) {
        cin >> s;
        int low = 0, fin = 0;
        for (char c : s) {
            if (c == '(') {
                ++fin;
            } else {
                --fin;
                low = min(fin, low);
            }
        }
        a[i] = {low, fin};
        total += fin;
    }
    if (total != 0) {
        return false;
    }
    print(a);
    auto cmp1 = [](const pii &l, const pii &r) {return l.x > r.x;};
    auto cmp2 = [](const pii &l, const pii &r) {
        if ((l.y >= 0 && r.y >= 0) || (l.y < 0 && r.y < 0)) {
            return l.x < r.x;
        }
        if (l.y == r.y) return l.x < r.x;
        return l.y > r.y;
    };
    multiset<pii, decltype(cmp2)> possible(cmp2);
    multiset<pii, decltype(cmp1)> impossible(cmp1);

    for (auto &p : a) {
        if (p.x >= 0) {
            possible.insert(p);
        } else {
            impossible.insert(p);
        }
    }
    print("possible", possible);
    print("impossible", impossible);

    int so_far = 0;
    while (!possible.empty()) {
        auto p = *possible.begin();
        possible.erase(possible.begin());
        print("took", p);
        so_far += p.y;
        if (so_far < 0) return false;

        while (!impossible.empty() && impossible.begin()->x >= -so_far) {
            auto q = *impossible.begin();
            print("now available:", q);
            impossible.erase(impossible.begin());
            possible.insert(q);
        }

        print("possible", possible);
        print("impossible", impossible);
    }
    return impossible.empty();
}

signed main() {
    cout << (solve() ? "Yes" : "No") << endl;
}