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: import bisect
  import sys
  from collections import defaultdict
  
  input = sys.stdin.readline
  
  
  class Node:
      def __init__(self, value, index):
          self.value = value
          self.index = index
  
      def __str__(self):
          return "val : " + str(self.value) + " index : " + str(self.index)
  
  
  # 0-indexed SegmentTree
  
  
  class SegmentTree:
      def __init__(self, N, default_list, default_value=-(10**10)):
          self.N0 = pow(2, (N - 1).bit_length())
          self.nodes = [
              Node(default_value, i + 1 - self.N0) for i in range(2 * self.N0 - 1)
          ]
          for i in reversed(range(2 * self.N0 - 1)):
              ind = i + 1 - self.N0
              if ind >= N:
                  self.nodes[i].value = default_value
              elif N > ind >= 0:
                  self.nodes[i].value = default_list[ind]
              else:
                  self.nodes[i] = self.process(
                      self.nodes[i * 2 + 1], self.nodes[i * 2 + 2]
                  )
  
      def __str__(self):
          return (
              "["
              + ", ".join(
                  map(str, [self.nodes[i + self.N0 - 1].value for i in range(self.N0)])
              )
              + "]"
          )
  
      def query(self, L, R):  # [L,R)の値
          retnode = Node(-(10**10), -(10**10))
          L += self.N0
          R += self.N0
          while L < R:
              if R & 1:
                  R -= 1
                  retnode = self.process(retnode, self.nodes[R - 1])
              if L & 1:
                  retnode = self.process(retnode, self.nodes[L - 1])
                  L += 1
              L >>= 1
              R >>= 1
          return retnode
  
      def process(self, node_x, node_y):  # x,yが子の時，親に返る値
          if node_x.value > node_y.value:
              return node_x
          else:
              return node_y
  
      def update(self, i, x):
          i += self.N0 - 1
          self.nodes[i] = Node(x, i + 1 - self.N0)
          while i:
              i = (i - 1) // 2
              self.nodes[i] = self.process(self.nodes[i * 2 + 1], self.nodes[i * 2 + 2])
  
  
  N = int(input())
  ss = [input().replace("\n", "") for _ in range(N)]
  
  
  def calc(S):
      tmp = 0
      MIN = 0
      for s in S:
          if s == "(":
              tmp += 1
          else:
              tmp -= 1
          MIN = min(tmp, MIN)
  
      return -MIN, tmp
  
  
  cnts = []
  for s in ss:
      down, up = calc(s)
      cnts.append((down, up, s))
  
  cnts.sort()
  # print(cnts)
  
  ups = []
  downs = []
  rights = defaultdict(int)
  for i, (d, u, s) in enumerate(cnts):
      downs.append(d)
      ups.append(u)
  
  ST = SegmentTree(N, ups)
  
  tmp = 0
  for _ in range(N):
      R = bisect.bisect_right(downs, tmp)
      # print(tmp, R)
      node = ST.query(0, R)
      ST.update(node.index, -(10**10))
  
      tmp += node.value
  
      if tmp < 0:
          print("No")
          sys.exit()
  
  
  if tmp == 0:
      print("Yes")
  else:
      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"}]