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 pp push_back
#define pf push_front
#define mp make_pair
#define fs first
#define sc second
#define sf scanf

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

const int N = (int) 1e6;
const int INF = (int) 1e9 + 7;

bool comp(ii a, ii b) {
	int x = max(a.fs, a.sc) - a.sc;
	int y = max(b.fs, b.sc) - b.sc;
	return x > y;
}

int main(int argc, char *argv[])
{
	// ios_base::sync_with_stdio(false);
	// cin.tie(NULL);

	int n;
	sf("%d", &n);
	char s[N + 7];
	int l = 0, r = 0;
	vector<ii> a;
	vector<int> b;
	for (int j = 0; j < n; ++j) {
		sf("%s", s);
		int x = 0;
		int y = 0;
		for (int i = 0; i < strlen(s); ++i) {
			if (s[i] == '(') {
				++x;
			}
			else {
				if (x < 1) {
					++y;
				}
				else {
					--x;
				}
			}
		}

		if (x > 0 && y > 0) {
			if (x - y >= 0) {
				a.pp(mp(y, x));
			}
			else {
				b.pp(x - y);
			}
		}
		else {
			l += x;
			r += y;
		}
	}

	sort(a.begin(), a.end());

	for (int i = 0; i < a.size(); ++i) {
		if (l < a[i].fs) {
			puts("No");
			return 0;
		}

		l += a[i].sc - a[i].fs;
	}

	sort(b.begin(), b.end());

	for (int i = 0; i < b.size(); ++i) {
		if (l + b[i] < 0) {
			puts("No");
			return 0;
		}

		l += b[i];
	}

	puts(l == r ? "Yes" : "No");

	return 0;
}