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 Haskell (GHC 8.8.3) (judge verdict: Time Limit Exceeded)

import qualified Data.ByteString.Char8 as BS
import qualified Data.List as L
import qualified Data.Set as S
import qualified Control.Monad as M
import Data.Maybe (fromJust)
import Data.Ord
import qualified Data.Vector.Unboxed as V

comb :: Int -> [a] -> [[a]]
comb 0 xs = [[]]
comb _ [] = []
comb n (x:xs) = [x:y | y <- comb (n-1) xs] ++ comb n xs


shori tsumi now [] = filter (0/=) [tsumi,now]
shori tsumi now (x:xs)
 |x<0&&now+x<0 = shori (now+x) 0 xs
 |x<0&&now+x>=0 = shori tsumi (now+x) xs
 |otherwise =shori tsumi (now+x) xs


msho [a] = [a]
msho [a,b]
 | a<0&&b<0 = [a+b]
 | otherwise = [a,b]

lae x y [] = (x,y)
lae x y ([a,b]:xs)
 |y<(-a) = lae (x-(y+a)) b xs
 |otherwise = lae x (y+a+b) xs

main = do
 n <- readLn :: IO Int
 str <-map (map (\x->if (BS.head x) == '(' then BS.length x else negate (BS.length x)) )<$>map BS.group <$>M.replicateM n BS.getLine
 let gatch = map msho$filter (not.null)$ map (shori 0 0 ) str


 let one = sum $map (\[x]->x)$ filter (\x->length x == 1) gatch
 let two = filter (\x->length x /= 1) gatch
 let allpat = filter ("Yes"==)$map (hyoka one)$map (lae 0 0)$ comb (length two) two
 putStrLn $ if length allpat /=0
  then "Yes"
  else "No"
--
--
hyoka one (resa,resb)
 |one /=0 && (resa==0||resb==0)="No"
 |one ==0 && (resa/=0||resb/=0)="No"
 |one == 0&&resa==0&&resb ==0 = "Yes"
 |one <0 && resa>0 = "No"
 |one <0 && resa==0&&resb+one==0="Yes"
 |one >0 && resb>0 = "No"
 |one >0 && resb==0&&resa - one == 0= "Yes"