Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

E - Parentheses

Problem Statement

You are given $n$ strings $\mathit{str}_1, \mathit{str}_2, \ldots, \mathit{str}_n$, each consisting of ( and ). The objective is to determine whether it is possible to permute the $n$ strings so that the concatenation of the strings represents a valid string.

Validity of strings are defined as follows:

The empty string is valid.

If $A$ and $B$ are valid, then the concatenation of $A$ and $B$ is valid.

If $A$ is valid, then the string obtained by putting $A$ in a pair of matching parentheses is valid.

Any other string is not valid.

For example, "()()" and "(())" are valid, while "())" and "((()" are not valid.

Input

The first line of the input contains an integer $n$ ($1 \leq n \leq 100$), representing the number of strings. Then $n$ lines follow, each of which contains $\mathit{str}_i$ ($1 \leq \lvert \mathit{str}_i \rvert \leq 100$). All characters in $\mathit{str}_i$ are ( or ).

Output

Output a line with "Yes" (without quotes) if you can make a valid string, or "No" otherwise.

Sample Input 1

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

Output for the Sample Input 1

Yes

Sample Input 2

2
))()((
))((())(

Output for the Sample Input 2

No

Solution in JavaScript (judge verdict: Wrong Answer)

var n;
var s = [];
var p = [];

function simplify(s){
	var p = [0, 0];
	rep(s.length, function(i){
		if(s.charAt(i) === '('){
			++p[1];
		}
		else{
			if(p[1] > 0){
				--p[1];
			}
			else{
				++p[0];
			}
		}
	});
	return p;
}

function main(){
	n = scan();
	rep(n, function(i){
		s[i] = scan_string();
	});
	
	rep(n, function(i){
		p[i] = simplify(s[i]);
	});

	var q = 0;
	var ans = true;
	rep(n, function(){
		var update = false;
		rep(n, function(i){
			if(p[i] !== null && q >= p[i][0]){
				q += (p[i][1] - p[i][0]);
				p[i] = null;
				update = true;
				return false;
			}
		});
		if(!update){
			ans = false;
			return false;
		}
	});
	print(ans && q === 0 ? 'Yes' : 'No');
}

function rep(a, b, c){
	if(c === undefined){
		c = b;
		b = a;
		a = 0;
	}
	for(var i = a; i < b; ++i){
		if(c(i) === false){
			break;
		}
	}
}
 
process.stdin.resume();
process.stdin.setEncoding('utf8');
 
var input = '';
var input_index = 0;
 
function scan(){
	return +input[input_index++];
}

function scan_string(){
	return input[input_index++];
}
 
function print(val){
	console.log(val);
}
 
process.stdin.on('data', function(chunk){
	input += chunk;
});
process.stdin.on('end', function(){
	input = input.trim().split(/\s+/);
	main();
});