This page is still under construction.

Parts of this page are still being built. What you see may change.

Concatenating Parenthesis Strings

Time limit2sMemory limit128 MB

Summary
Decide whether the given parenthesis strings can be ordered so their concatenation forms a correct bracket sequence.
Level

Medium6 of 10

Topics
Greedy, Sorting, Prefix sum
Solved
No attempts yet

Problem

You are given nn strings str1,str2,…,strnstr_1, str_2, \ldots, str_n, each consisting of ( and ) only. Decide whether the nn strings can be arranged in some order so that their concatenation is a valid parenthesis string. You may reorder the strings, but the contents of each string stay as they are.

A valid parenthesis string is defined as follows.

  • The empty string is valid.
  • If AA and BB are valid, then the concatenation of AA and BB is valid.
  • If AA is valid, then the string obtained by putting AA inside a pair of matching parentheses is valid.
  • No other string is valid.

For example, ()() and (()) are valid, while ()) and ((() are not.

Input

The first line contains the number of strings nn (1≤n≤1001 \le n \le 100). Each of the next nn lines contains one string stristr_i (1≤∣stri∣≤1001 \le |str_i| \le 100). Every character of stristr_i is ( or ).

Output

Print Yes on the first line if a valid parenthesis string can be built, and No otherwise. Do not print the quotes.

Examples5

  1. Example 1

    Input
    3
    ()(()((
    ))()()(()
    )())(())
    
    Expected output
    Yes
    
  2. Example 2

    Input
    2
    ))()((
    ))((())(
    
    Expected output
    No
    
  3. Example 3

    Input
    1
    ()
    
    Expected output
    Yes
    
  4. Example 4

    Input
    1
    )(
    
    Expected output
    No
    
  5. Example 5

    Input
    2
    ((
    ))
    
    Expected output
    Yes