output_description: If a bracket sequence can be formed by concatenating all the N strings in some
  order, print `Yes`; otherwise, print `No`.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      S_1
      :
      S_N
attempt: from functools import reduce
  from itertools import permutations
  
  
  def f(s):
      while True:
          t = s.replace("()", "")
          if t == s:
              return s
          s = t
  
  
  N = int(input())
  S = [input() for _ in range(N)]
  
  l = 0
  r = 0
  for s in S:
      t = s.count("(")
      l += t
      r += len(s) - t
  if l != r:
      print("No")
      exit()
  
  S = [s for s in map(f, S) if s != ""]
  
  if len(S) == 0:
      print("Yes")
      exit()
  
  lS = [s for s in S if s[0] == "("]
  rS = [s for s in S if s[0] == ")"]
  
  ld = {}
  for s in lS:
      ld.setdefault(s, 0)
      ld[s] += 1
  
  rd = {}
  for s in rS:
      rd.setdefault(s, 0)
      rd[s] += 1
  
  for k1 in ld:
      for k2 in rd.keys():
          if f(k1 + k2) != "":
              continue
          t = max(ld[k1], rd[k2])
          ld[k1] -= t
          rd[k2] -= t
  
  if len(ld) == 0 and len(rd) == 0:
      print("Yes")
      exit()
  
  SS = []
  for k in ld:
      for i in range(ld[k]):
          SS.append(k)
  for k in rd:
      for i in range(rd[k]):
          SS.append(k)
  
  for ss in permutations(SS, len(SS)):
      t = reduce(lambda x, y: x + y, ss)
      if f(t) == "":
          print("Yes")
          exit()
  print("No")
  
problem_description: Statement
  
  A **bracket sequence** is a string that is one of the following:
  
    1. An empty string;
    2. The concatenation of `(`, A, and `)` in this order, for some bracket sequence A ;
    3. 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?
samples: [{"input": "2\n    )\n    (()", "output": "Yes\n    \n\nConcatenating `(()` and `)` in this order forms a bracket sequence.\n\n* * *"}, {"input": "2\n    )(\n    ()", "output": "No\n    \n\n* * *"}, {"input": "4\n    ((()))\n    ((((((\n    ))))))\n    ()()()", "output": "Yes\n    \n\n* * *"}, {"input": "3\n    (((\n    )\n    )", "output": "No"}]