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: #!/usr/bin/env python3
  import sys
  
  YES = "Yes"  # type: str
  NO = "No"  # type: str
  
  
  def iterate_tokens():
      for line in sys.stdin:
          for word in line.split():
              yield word
  
  
  def solve(N: int):
      tokens = iterate_tokens()
      left_all = 0
      right_all = 0
      for _ in range(N):
  
          # print("all", left_all, right_all)
  
          s = next(tokens)
          left = 0
          right = 0
          for ss in s:
              # print(ss)
              # print(left, right)
              if ss == ")":
                  if right > 0:
                      right -= 1
                  else:
                      left += 1
              else:
                  right += 1
  
          # print(left, right)
  
          new_left_all_right = max(0, left_all - right) + left
          new_right_all_right = max(0, right - left_all) + right_all
  
          # print(new_left_all_right, new_right_all_right)
  
          new_left_all_left = max(0, right_all - left) + right
          new_right_all_left = max(0, left - right_all) + left_all
  
          # print(new_left_all_left, new_right_all_left)
  
          if (
              new_left_all_right + new_right_all_right
              > new_left_all_left + new_right_all_left
          ):
              left_all = new_left_all_left
              right_all = new_right_all_left
          else:
              left_all = new_left_all_right
              right_all = new_right_all_right
      if left_all == 0 and right_all == 0:
          print(YES)
      else:
          print(NO)
      return
  
  
  # Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
  def main():
      tokens = iterate_tokens()
      N = int(next(tokens))  # type: int
      solve(N)
  
  
  if __name__ == "__main__":
      main()
  
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"}]