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

def small(a,b) return (a<b)? a:b end

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

t[1].sort_by!{|u|u[1]*-1}
t[1].each do |a|
  if(now < a[1]*-1)
    puts "No"
    exit
  end
  now += a[0]
end
tt= copy(t[2].sort_by!{|u|u[0]*-1})
tt2 = copy(t[2].sort_by!{|u|u[1]})
h = {}
ind = 0

tt.each do |a|
  next if(h[a[2]])
  while tt2[ind][1] > now + a[1]
    if h[tt2[ind][2]]
      ind += 1
      next
    end
    if(now < tt2[ind][1] *-1)
      puts "No"
      exit
    end
    now += tt2[ind][0]
    h[tt2[ind][2]] = true
  end
  next if(h[a[2]])
  h[a[2]] = true
  if(now < a[1] *-1)
    puts "No"
    exit
  end
  now += tt2[ind][0]
end

puts now == 0? "Yes" : "No"