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: Runtime Error)

#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

template<typename T>
string to_string(const vector<T>&);

template<typename T>
string to_string(const vector<T>& vc, int w) {
	if(w + 1 == vc.size()) return to_string(vc[w]);
	return to_string(vc[w]) + "," + to_string(vc, w + 1);
}

template<typename T>
string to_string(const pair<T, T>& x) {
	return "(" + to_string(x.fi) + "," + to_string(x.se) + ")";
}

template<typename T>
string to_string(const vector<T>& vc) {
	return "{" + to_string(vc, 0) + "}";
}

void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

class DebugStream {}LOG;
template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; }
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29;

void no() {
	cout << "No\n"; exit(0);
}

ii gt(const string& str) {
	int opn = 0, cls = 0;
	for(auto el: str) {
		if(el == '(') opn++;
		else if(opn) opn--;
		else cls++;
	}
	return ii(cls, opn - cls);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	#endif

	int n;
	cin >> n;
	vector<vector<int>> vc;
	for(int i = 0; i < n; i++) {
		string inp;
		cin >> inp;
		ii x = gt(inp);
		while(vc.size() <= x.fi) vc.push_back(vector<int>());
		debug(x);
		vc[x.fi].push_back(x.se);
	}

	int d = 0, opt = -1;
	priority_queue<ii> pq;
	for(int i = 0; i < n; i++) {
		while(opt < d) {
			opt++;
			for(auto el: vc[opt]) pq.push(ii(el, opt));
		}
		while(pq.size() && pq.top().se > d) pq.pop();
		if(pq.empty()) no();
		d += pq.top().fi; pq.pop();
	}

	cout << (d? "No": "Yes") << '\n';
}