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 Ruby (2.7.1) (judge verdict: Wrong Answer)

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

n = inp[0]
t = na2(3,0)
n.times do
  s = gets.chomp
  now = na(s.size)
  pl = 0
  min = 0
  s.size.times do |i|
    if(s[i] == "(")
      pl += 1
    else
      pl -= 1
    end
    min = [min,pl].min
  end
  if(min >= 0)
    t[0]<<[pl,min]
  elsif(pl > 0)
    t[1]<<[pl,min]
  else
    t[2]<<[pl,min]
  end
end
now = 0
t[0].each do |a|
  now += a[0]
end

t[1].sort_by!{|u|u[1]}
t[1].each do |a|
  if(now < a[1]*-1)
    puts "No"
    exit
  end
  now += a[0]
end
t[2].sort_by!{|u|u[1]*-1}
t[2].each do |a|
  if(now < a[1]*-1)
    puts "No"
    exit
  end
  now += a[0]
end
puts now == 0? "Yes" : "No"