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 <stdio.h>
#include <stdlib.h>
#include <string.h>

void neko(int* left, int* right, const char* data) {
	size_t s = strlen(data);
	size_t i;
	int count = 0;
	int foo = 0;
	for (i = 0; i < s; i++) {
		if (data[i] == '(') count++;
		else if (data[i] == ')') count--;
		if (count < foo) foo = count;
	}
	*left = -foo;

	foo = 0;
	count = 0;
	for (i = s; i > 0; i--) {
		if (data[i - 1] == '(') count++;
		else if (data[i - 1] == ')') count--;
		if (count > foo) foo = count;
	}
	*right = foo;
}

char S[1234567];

struct nya {
	int left, right;
};

int by_left(const void* x, const void* y) {
	struct nya a = *(const struct nya*)x, b = *(const struct nya*)y;
	return a.left < b.left ? -1 : a.left > b.left;
}

int by_left2(const void* x, const void* y) {
	return by_left(y, x);
}

int N;

int pc, mc;
struct nya p[1234567], m[1234567];

int main(void) {
	int i;
	int ayaneru = 0;
	if (scanf("%d", &N) != 1) return 1;
	for (i = 0; i < N; i++) {
		struct nya hoge;
		if (scanf("%1234566s", S) != 1) return 1;
		neko(&hoge.left, &hoge.right, S);
		if (hoge.left <= hoge.right) p[pc++] = hoge; else m[mc++] = hoge;
	}
	qsort(p, pc, sizeof(*p), by_left);
	qsort(m, mc, sizeof(*m), by_left2);
	/* ハードルが低いのから使う */
	for (i = 0; i < pc; i++) {
		if (p[i].left > ayaneru) {
			puts("No");
			return 0;
		}
		ayaneru -= p[i].left;
		ayaneru += p[i].right;
	}
	/* ハードルが高いのからやっつける */
	for (i = 0; i < mc; i++) {
		if (m[i].left > ayaneru) {
			puts("No");
			return 0;
		}
		ayaneru -= m[i].left;
		ayaneru += m[i].right;
	}
	puts(ayaneru == 0 ? "Yes" : "No");
	return 0;
}

/*

1 ())()()(()(()() 2

差に基づく貪欲はこれでダメ

3
(((
)))(((((((
)))))))

-------------

4
(
)))(((((((
)(((
)))))))

*/