from cython import longlong

 

def main():

    n: longlong

    n=int(eval(input()))

    lp, lm = [], []

    for i in range(n):

        s=eval(input())

        

        x: longlong

        y: longlong

        x,y=0,0

        

        for j in s:

            if j=="(":x+=1

            else:x-=1

            y=min(y,x)

        if x>0: lp.append((y,x))

        else: lm.append((y-x,-x))

    lp.sort()

    s: longlong

    s=0

 

    i: longlong

    j: longlong

    for i,j in lp[::-1]:

        if s+i<0:

            print("No")

            exit()

        s+=j

    lm.sort()

 

    t: longlong

    t=0

 

    i: longlong

    j: longlong

    for i,j in lm[::-1]:

        if t+i<0:

            print("No")

            exit()

        t+=j

    if s==t: print("Yes")

    else: print("No")

 

if __name__ == '__main__':

    main()
import cython

 

def main():

    import sys

    readline = sys.stdin.readline

    

    N: cython.longlong

    N = int(readline())

    S = [readline().strip() for i in range(N)]

    

    D = []

    L, R = [], []

    for s in S:

        l: cython.longlong = 0

        r: cython.longlong = 0

        for c in s:

            if c == ')':

                if l > 0: l -= 1

                else: r += 1

            else: l += 1

        if l == 0 and r == 0:pass

        elif l == 0: R.append(r)

        elif r == 0: L.append(l)

        else: D.append((l, r))

    

    L: cython.longlong

    R: cython.longlong

    L = sum(L)

    R = sum(R)

    

    inc = []

    dec = []

    for l, r in D:

        if l > r:

            inc.append((l, r))

        else:

            dec.append((l, r))

    

    inc.sort(key=lambda x:x[1])

    dec.sort(key=lambda x:-x[1])

    

    D = inc + dec

    

    l: cython.longlong

    r: cython.longlong

    for _, (l,r) in enumerate(D):

        L -= r

        if L < 0:

            print('No')

            exit(0)

        L += l

    

    if L == R:

        print('Yes')

    else:

        print('No')

 

if __name__ == '__main__':

    main()
from cython import longlong


def main():
    n: longlong
    n = int(eval(input()))
    lp, lm = [], []
    for i in range(n):
        s = eval(input())
        x: longlong
        y: longlong
        x, y = 0, 0
        for j in s:
            if j == "(":
                x += 1
            else:
                x -= 1
            y = min(y, x)
        if x > 0:
            lp.append((y, x))
        else:
            lm.append((y - x, -x))
    lp.sort()
    s: longlong
    s = 0
    i: longlong
    j: longlong
    for i, j in lp[::-1]:
        if s + i < 0:
            print("No")
            exit()
        s += j
    lm.sort()
    t: longlong
    t = 0
    i: longlong
    j: longlong
    for i, j in lm[::-1]:
        if t + i < 0:
            print("No")
            exit()
        t += j
    if s == t:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

import cython


def main():
    import sys

    readline = sys.stdin.readline
    N: cython.longlong
    N = int(readline())
    S = [readline().strip() for i in range(N)]
    D = []
    L, R = [], []
    for s in S:
        l: cython.longlong = 0
        r: cython.longlong = 0
        for c in s:
            if c == ")":
                if l > 0:
                    l -= 1
                else:
                    r += 1
            else:
                l += 1
        if l == 0 and r == 0:
            pass
        elif l == 0:
            R.append(r)
        elif r == 0:
            L.append(l)
        else:
            D.append((l, r))
    L: cython.longlong
    R: cython.longlong
    L = sum(L)
    R = sum(R)
    inc = []
    dec = []
    for l, r in D:
        if l > r:
            inc.append((l, r))
        else:
            dec.append((l, r))
    inc.sort(key=lambda x: x[1])
    dec.sort(key=lambda x: -x[1])
    D = inc + dec
    l: cython.longlong
    r: cython.longlong
    for _, (l, r) in enumerate(D):
        L -= r
        if L < 0:
            print("No")
            exit(0)
        L += l
    if L == R:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

-from cython import longlong +import cython -    n: longlong -    n = int(eval(input())) -    lp, lm = [], [] -    for i in range(n): -        s = eval(input()) -        x: longlong -        y: longlong -        x, y = 0, 0 -        for j in s: -            if j == "(": -                x += 1 +    import sys + +    readline = sys.stdin.readline +    N: cython.longlong +    N = int(readline()) +    S = [readline().strip() for i in range(N)] +    D = [] +    L, R = [], [] +    for s in S: +        l: cython.longlong = 0 +        r: cython.longlong = 0 +        for c in s: +            if c == ")": +                if l > 0: +                    l -= 1 +                else: +                    r += 1 -                x -= 1 -            y = min(y, x) -        if x > 0: -            lp.append((y, x)) +                l += 1 +        if l == 0 and r == 0: +            pass +        elif l == 0: +            R.append(r) +        elif r == 0: +            L.append(l) -            lm.append((y - x, -x)) -    lp.sort() -    s: longlong -    s = 0 -    i: longlong -    j: longlong -    for i, j in lp[::-1]: -        if s + i < 0: +            D.append((l, r)) +    L: cython.longlong +    R: cython.longlong +    L = sum(L) +    R = sum(R) +    inc = [] +    dec = [] +    for l, r in D: +        if l > r: +            inc.append((l, r)) +        else: +            dec.append((l, r)) +    inc.sort(key=lambda x: x[1]) +    dec.sort(key=lambda x: -x[1]) +    D = inc + dec +    l: cython.longlong +    r: cython.longlong +    for _, (l, r) in enumerate(D): +        L -= r +        if L < 0: -            exit() -        s += j -    lm.sort() -    t: longlong -    t = 0 -    i: longlong -    j: longlong -    for i, j in lm[::-1]: -        if t + i < 0: -            print("No") -            exit() -        t += j -    if s == t: +            exit(0) +        L += l +    if L == R: