Scanf.scanf "%d" (fun n ->
    let rec loop i score upper flatter downer =
        if i = n then (if score = 0 then Some (upper, flatter, downer) else None) else
            let s = Scanf.scanf " %s" (fun s -> s) in
            let sn = String.length s in
            let rec check i cur mi =
                if i = sn then cur, mi else
                    let cur = if s.[i] = '(' then cur + 1 else cur - 1 in
                    check (i + 1) cur (min mi cur)
            in
            let cur, mi = check 0 0 0 in
            let score = score + cur in
            if cur < 0 then loop (i + 1) score upper flatter ((mi, cur) :: downer) else
            if cur > 0 then loop (i + 1) score ((mi, cur) :: upper) flatter downer else
                            loop (i + 1) score upper ((mi, cur) :: flatter) downer
    in
    let sim u f d =
        let u = List.sort (fun a b -> compare b a) u in
        let d = List.sort (fun a b -> compare a b) d in
        let rec loop cur = function
            | [] -> cur
            | (mi, add) :: tl ->
                    if cur + mi < 0 then -1 else loop (cur + add) tl
        in
        let cur = loop 0 u in
        if cur < 0 then "No" else
            let cur = loop cur f in
            if cur < 0 then "No" else
            let cur = loop cur d in
            if cur = 0 then "Yes" else "No"
    in
    print_endline @@
        match loop 0 0 [] [] [] with
        | None -> "No"
        | Some (u, f, d) -> sim u f d
)