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 Ruby (judge verdict: Runtime Error)

n = gets.to_i
a = (1..n).map { gets.chomp }

diff = []
min = [0] * n
diff_rev = []
min_rev = [0] * n
a.each_with_index do |s, i|
    x = 0
    s.each_char do |c|
        x += (c == ?( ? 1 : -1)
        min[i] = x if x < min[i]
    end
    diff[i] = x
    x = 0
    s.reverse.each_char do |c|
        x += (c == ?) ? 1 : -1)
        min_rev[i] = x if x < min_rev[i]
    end
    diff_rev[i] = x
end

if min.max < 0 || min_rev.max < 0 || diff.inject(:+) != 0
    puts 'No'
    exit
end

rest = (0..n-1).to_a

h = 0
while rest.map {|i| diff[i]}.max >= 0
    i = rest.select {|i| h + min[i] >= 0}.max_by {|i| diff[i]}
    if !i
        puts 'No'
        exit
    else
        h += diff[i]
        rest.delete(i)
    end
end

h = 0
until rest.empty?
    i = rest.select {|i| h + min_rev[i] >= 0}.max_by {|i| diff_rev[i]}
    if !i
        puts 'No'
        exit
    end
    h += diff_rev[i]
    rest.delete(i)
end

puts 'Yes'