Problem:


Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

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?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No



Sample input:
2
)
(()


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
# F - Bracket Sequencing

N = int(input())
L_sum = 0
L_min_sum = 0
R_sum = 0
R_min_sum = 0
for _ in range(N):
    S = list(str(input()))
    L_tmp = 0
    L_min_tmp = 0
    for s in S:
        if s == '(':
            L_tmp += 1
        else:
            L_tmp -= 1
            L_min_tmp = min(L_min_tmp, L_tmp)
    if L_tmp >= 0:
        L_sum += L_tmp
        L_min_sum += L_min_tmp
    R_tmp = 0
    R_min_tmp = 0
    for s in S[::-1]:
        if s == ')':
            R_tmp += 1
        else:
            R_tmp -= 1
            R_min_tmp = min(R_min_tmp, R_tmp)
    if R_tmp >= 0:
        R_sum += R_tmp
        R_min_sum += R_min_tmp

#print(L_sum, L_min_sum, R_sum, R_min_sum)
if L_sum == R_sum and L_sum+L_min_sum >= 0 and R_sum+R_min_sum >= 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
def main():
    import queue
    import sys
    input = lambda : sys.stdin.readline().rstrip()
    sys.setrecursionlimit(max(1000, 10**9))



    n = int(input())
    minsum = [None] * n # (min, sum)
    for i in range(n):
        tmp = list(map(int, " ".join(input()).replace("(", "1").replace(")", "-1").split()))
        c = 0
        m = 0
        for item in tmp:
            c += item
            m = min(c, m)
        minsum[i] = (m, sum(tmp))
    minsum.sort()

    if sum(item[1] for item in minsum)!=0:
        print("No")
    else:
        minsum1 = [item for item in minsum if item[1]>=0]
        minsum2 = [item for item in minsum if item[1]<0]
        c = 0
        for m,s in minsum1[::-1]:
            if c+m>=0:
                c += s
            else:
                print("No")
                return
        c = 0
        for m,s in minsum2[::-1]:
            if c+(-m)>=0:
                c+=(-s)
            else:
                print("No")
                return
        print("Yes")
main()

Submission status: Wrong Answer

Solution code:
def main():
    import queue
    import sys
    input = lambda : sys.stdin.readline().rstrip()
    sys.setrecursionlimit(max(1000, 10**9))



    n = int(input())
    minsum = [None] * n # (min, sum)
    for i in range(n):
        tmp = list(map(int, " ".join(input()).replace("(", "1").replace(")", "-1").split()))
        c = 0
        m = 0
        for item in tmp:
            c += item
            m = min(c, m)
        minsum[i] = (m, sum(tmp))
    minsum.sort()

    if sum(item[1] for item in minsum)!=0:
        print("No")
    else:
        minsum1 = [item for item in minsum if item[1]>0]
        minsum2 = [item for item in minsum if item[1]<=0]
        c = 0
        for m,s in minsum1[::-1]:
            if c+m>=0:
                c += s
            else:
                print("No")
                return
        for m,s in minsum2:
            if c+m>=0:
                c+=s
            else:
                print("No")
                return
        print("Yes")
main()

Submission status: Wrong Answer

Solution code:
def main():
    import queue
    import sys
    input = lambda : sys.stdin.readline().rstrip()
    sys.setrecursionlimit(max(1000, 10**9))


    n = int(input())
    minsum = [None] * n # (min, sum)
    for i in range(n):
        tmp = list(map(int, " ".join(input()).replace("(", "1").replace(")", "-1").split()))
        c = 0
        m = 0
        for item in tmp:
            c += item
            m = min(c, m)
        minsum[i] = (m, sum(tmp))
    minsum.sort()

    if sum(item[1] for item in minsum)!=0:
        print("No")
    else:
        minsum1 = [item for item in minsum if item[1]>=0]
        minsum2 = [item for item in minsum if item[1]<0]
        c = 0
        for m,s in minsum1[::-1]:
            if c+m>=0:
                c += s
            else:
                print("No")
                return
        for m,s in minsum2:
            if c+m>=0:
                c+=s
            else:
                print("No")
                return
        print("Yes")
main()

Submission status: Wrong Answer

Solution code:
import queue
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))



n = int(input())
minsum = [None] * n # (min, sum)
for i in range(n):
    tmp = list(map(int, " ".join(input()).replace("(", "1").replace(")", "-1").split()))
    c = 0
    m = 0
    for item in tmp:
        c += item
        m = min(c, m)
    minsum[i] = (m, sum(tmp))
minsum.sort()

if sum(item[1] for item in minsum)!=0:
    print("No")
else:
    minsum1 = queue.deque([item for item in minsum if item[1]>0])
    c = 0
    while minsum1:
        update = False
        for i in range(len(minsum1)):
            m, s = minsum1[-1]
            if c+m>=0:
                c += s
                update = True
                minsum1.pop()
            minsum1.rotate()
        if not update:
            break
    if minsum1:
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

Solution code:
# F - Bracket Sequencing

N = int(input())
L_sum = []
L_min = []
R_sum = []
R_min = []
for _ in range(N):
    S = list(str(input()))
    tmp = 0
    min_tmp = 0
    for s in S:
        if s == '(':
            tmp += 1
        else:
            tmp -= 1
            min_tmp = min(min_tmp, tmp)
    if tmp > 0:
        L_sum.append(tmp)
        L_min.append(min_tmp)
    else:
        R_sum.append(tmp * -1)
        R_min.append(min_tmp)

if sum(L_sum) == sum(R_sum) and sum(L_sum)+sum(L_min) >= 0 and sum(R_sum) + sum(R_min) >= 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import queue
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))



n = int(input())
minsum = [None] * n # (min, sum)
for i in range(n):
    tmp = list(map(int, " ".join(input()).replace("(", "1").replace(")", "-1").split()))
    c = 0
    m = 0
    for item in tmp:
        c += item
        m = min(c, m)
    minsum[i] = (m, sum(tmp))
minsum.sort()

if sum(item[1] for item in minsum)!=0:
    print("No")
else:
    minsum1 = queue.deque([item for item in minsum if item[1]>=0])
    c = 0
    while minsum1:
        update = False
        for i in range(len(minsum1)):
            m, s = minsum1[-1]
            if c+m>=0 and s>=0:
                c += s
                update = True
                minsum1.pop()
            minsum1.rotate()
        if not update:
            break
    if minsum1:
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import namedtuple
Line = namedtuple('Line', ['lowest', 'end'])


def main():
    N = int(input())
    up_lines = []
    down_lines = []
    for i in range(N):
        s = input()
        now = 0
        lowest = 0
        for c in s:
            if c == "(":
                now += 1
            else:
                now -= 1
                lowest = min(lowest, now)
        line = Line(lowest, now)
        if line.end > 0:
            up_lines.append(line)
        else:
            down_lines.append(line)
    up_lines.sort(key=lambda line: -line.end)
    up_lines.sort(key=lambda line: -line.lowest)
    down_lines.sort(key=lambda line: line.end)
    down_lines.sort(key=lambda line: line.lowest)
    now = 0
    lines = up_lines + down_lines
    for line in lines:
        if now + line.lowest < 0:
            print("No")
            return
        now += line.end
        if now < 0:
            print("No")
            break
    if now == 0:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import namedtuple
Line = namedtuple('Line', ['lowest', 'end'])


def main():
    N = int(input())
    up_lines = []
    down_lines = []
    for i in range(N):
        s = input()
        now = 0
        lowest = 0
        for c in s:
            if c == "(":
                now += 1
            else:
                now -= 1
                lowest = min(lowest, now)
        line = Line(lowest, now)
        if line.end > 0:
            up_lines.append(line)
        else:
            down_lines.append(line)
    up_lines.sort(key=lambda line: line.lowest)
    up_lines.sort(key=lambda line: line.end)
    up_lines.reverse()
    down_lines.sort(key=lambda line: line.end)
    down_lines.sort(key=lambda line: line.lowest)
    now = 0
    lines = up_lines + down_lines
    for line in lines:
        if now + line.lowest < 0:
            print("No")
            return
        now += line.end
        if now < 0:
            print("No")
            break
    if now == 0:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
c = []
for i in s:
    mi = 0
    tmp = 0
    for j in i:
        if j=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if tmp>0:
        a.append((mi, tmp))
    elif tmp==0:
        b.append((mi, tmp))
    else:
        c.append((mi, tmp))
a.sort(key=lambda x:-x[0])
c.sort(key=lambda x:(x[0], -x[1]))
tmp = 0
for i, j in a+b+c:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())

#INF = float('inf')

def check(A):
  th = 0 #高さの合計
  for i in range(len(A)):
    b = A[i][0]
    h = A[i][1]
    if th + b < 0:
      return False
    else:
      th += h
  return True
    

L = []
R = []
goukei = 0
for i in range(N):
  temp = str(input())
  b= 0 #最下点
  h = 0 #ゴールの位置
  for j in range(len(temp)):
    if temp[j] == "(":
      h += 1
    else:
      h -= 1
    b = min(b,h)
  if h > 0:
    L.append([b,h])
  else:
    R.append([b-h,-h])
  goukei += h

L.sort(reverse=True)
R.sort(reverse=True)
print(L,R)

if check(L) and check(R) and goukei == 0:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
from collections import namedtuple
Line = namedtuple('Line', ['lowest', 'end'])


def main():
    N = int(input())
    lines = [None]*N
    for i in range(N):
        s = input()
        now = 0
        lowest = 0
        for c in s:
            if c == "(":
                now += 1
            else:
                now -= 1
                lowest = min(lowest, now)
        lines[i] = Line(lowest, now)
    lines.sort(key=lambda line: line.lowest)
    lines.sort(key=lambda line: line.end)
    lines.reverse()
    now = 0
    for line in lines:
        if now + line.lowest < 0:
            print("No")
            return
        now += line.end
        if now < 0:
            print("No")
            break
    if now == 0:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
po,ne=[],[]
now=0
for i in range(n):
    p=0
    mp=0
    for j in input():
        p+=(1,-1)[j==")"]
        mp=min(p,mp)
    if mp:
        if p>=0:
            po.append((mp,p))
        else:ne.append((mp,p))
    else:now+=p
po.sort(key=lambda x: (-x[0],-x[1]))
ne.sort(key=lambda x: (-x[0],-x[1]))
for m,p in po+ne:
    if m+now<0:print("No");exit()
    else:now+=p
print("No") if now else print("Yes")

Submission status: Wrong Answer

Solution code:
n=int(input())
po,ne=[],[]
now=0
for i in range(n):
    p=0
    mp=0
    for j in input():
        p+=(1,-1)[j==")"]
        mp=min(p,mp)
    if mp:
        if p>=0:
            po.append((mp,p))
        else:ne.append((mp,p))
    else:now+=p
po.sort(key=lambda x: (-x[0],-x[1]))
ne.sort(key=lambda x: (-x[0],x[1]))
for m,p in po+ne:
    if m+now<0:print("No");exit()
    else:now+=p
print("No") if now else print("Yes")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
c = []
for i in s:
    mi = 0
    tmp = 0
    for j in i:
        if j=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if tmp>=0:
        a.append((mi, tmp))
    else:
        b.append((mi, tmp))
a.sort(key=lambda x:-x[0])
b.sort(key=lambda x:(x[0], -x[1]))
tmp = 0
for i, j in a+b:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[0]*n
info=[0]*n
for i in range(n):
    ss=list(input())
    now=0
    mi=0
    for sss in ss:
        if sss=="(":
            now+=1
        else:
            now-=1
            mi=min(mi,now)
    info[i]=(mi,now)
info.sort(key=lambda x:(x[1]>=0)*2+(x[0]>=0),reverse=True )

now=0
for inf in info:
    if now+inf[0]>=0:
        now+=inf[1]
    else:
        print("No")
        break
else:
    if now==0:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[0]*n
info=[0]*n
for i in range(n):
    ss=list(input())
    
    now=0
    mi=0
    for sss in ss:
        if sss=="(":
            now+=1
        else:
            now-=1
            mi=min(mi,now)
    info[i]=(mi,now)
info.sort(key=lambda x:(x[1]>=0)*2+(x[0]>0),reverse=True )


now=0

for inf in info:
    if now+inf[0]>=0:
        now+=inf[1]
    else:
        print("No")
        break
else:
    if now==0:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
c = []
for i in s:
    mi = 0
    tmp = 0
    for j in i:
        if j=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if mi==0:
        a.append((0, tmp))
    elif tmp>=0:
        b.append((mi, tmp))
    else:
        c.append((mi, tmp))
b.sort(key=lambda x:-x[0])
c.sort(key=lambda x:(x[0], -x[1]))
tmp = 0
for i, j in a+b+c:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())

S = []
for i in range(N):
    T = input()
    tmp, m = 0, 0    
    for s in T:
        if s == '(':
            tmp += 1
        else:
            tmp -= 1
        m = min(m, tmp)
    S.append((tmp,m))

S.sort(reverse=True)

inter, end = 0, 0
for t in S:
    a, b = t
    inter = end + b
    end = end + a
    # print(inter, end)
    if inter < 0 or end < 0:
        print('No')
        break
else:
    if not end == 0:
        print('No')
    else:
        print('Yes')

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[0]*n
for i in range(n):
    ss=list(input())
    s[i]=[1 if sss=="(" else -1 for sss in ss]

import itertools
rui = [list(itertools.accumulate(ss)) for ss in s]

info = [(min(ss), ss[-1]) for ss in rui]
info.sort(key=lambda x:(x[1]>=0)*2+(x[0]>0),reverse=True )

now=0

for inf in info:
    if now+inf[0]>=0:
        now+=inf[1]
    else:
        print("No")
        break
else:
    if now==0:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
c = []
for i in s:
    mi = 0
    tmp = 0
    for j in i:
        if j=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if mi==0:
        a.append((0, tmp))
    elif tmp>0:
        b.append((mi, tmp))
    else:
        c.append((mi, tmp))
b.sort(key=lambda x:-x[0])
c.sort(key=lambda x:(x[0], -x[1]))
tmp = 0
for i, j in a+b+c:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for i in range(n):
    s=input()
    p=0
    mp=0

    for j in s:
        p+=(1,-1)[j==")"]
        mp=min(p,mp)
    l.append((mp,p))
l.sort(reverse=1)
now=0
for m,p in l:
    if m+now>=0:now+=p
    else:print("No");exit()
print("No" if now else "Yes")

Submission status: Wrong Answer

Solution code:
N = int(input())

S = []
for i in range(N):
    T = input()
    tmp, m = 0, 0    
    for s in T:
        if s == '(':
            tmp += 1
        else:
            tmp -= 1
        m = min(m, tmp)
    S.append((m,tmp))

S.sort(reverse=True)

inter, end = 0, 0
for t in S:
    a, b = t
    inter = end + a
    end = end + b
    # print(inter, end)
    if inter < 0 or end < 0:
        print('No')
        break
else:
    if not end == 0:
        print('No')
    else:
        print('Yes')

Submission status: Wrong Answer

Solution code:
def check(vec):
    if len(vec) == 0:
        return True
    h = 0
    for v in sorted(vec, reverse=True):
        h += v[0]
        if h < 0:
            return False
        h += v[1]
    return True


def parse(brackets):
    m = 0
    f = 0
    for c in brackets:
        if c == "(":
            f += 1
        else:
            f -= 1
        if m > f:
            m = f
    return m, f


def main():
    N = int(input())
    ls = []
    rs = []
    tot = 0
    for _ in range(N):
        m, f = parse(input().strip())
        tot += f
        if f >= 0:
            ls.append([m, f])
        else:
            rs.append([m-f, -f])
    return tot == 0 and check(ls) and check(rs)


if __name__ == "__main__":
    print("Yes" if main() else "No")

Submission status: Wrong Answer

Solution code:
def check(vec):
    h = 0
    for v in sorted(vec, reverse=True):
        h += v[0]
        if h < 0:
            return False
        h += v[1]
    return True


def parse(brackets):
    m = 0
    f = 0
    for c in brackets:
        if c == "(":
            f += 1
        else:
            f -= 1
        if m > f:
            m = f
    return m, f


def main():
    N = int(input())
    ls = []
    rs = []
    tot = 0
    for _ in range(N):
        m, f = parse(input().strip())
        tot += f
        if f >= 0:
            ls.append([m, f])
        else:
            rs.append([m-f, -f])
    return tot == 0 and check(ls) and check(rs)


if __name__ == "__main__":
    print("Yes" if main() else "No")

Submission status: Wrong Answer

Solution code:
n = int(input())
s=['' for _ in range(n)]
for i in range(n):
    s[i]=input()
x=[[0,0] for _ in range(n)]
for i in range(n):
    t=s[i]
    lv=0
    mn=0
    for u in t:
        if u == '(':
            lv+=1
        elif u == ')':
            lv-=1
        mn=min(mn,lv)
    x[i][0],x[i][1]=lv,mn
x.sort(key = lambda x:x[0],reverse=1)
ind=0
for lv,mn in x:
    if lv<0:
        break
    ind+=1
p=x[:ind]
m=x[ind:]
p.sort(key = lambda x:x[1],reverse=1)
m.sort(key = lambda x:x[1],reverse=1)
ind = 0
for lv,mn in p:
    if mn<0:
        break
    ind+=1
pp=p[:ind]
pm=p[ind:]
ind = 0
for lv,mn in m:
    if mn<0:
        break
    ind+=1
mp=m[:ind]
mm=m[ind:]
#print(x)
#print(pp,pm,mp,mm)
mn=0
lv=0
list=[pp,pm,mm,mp]
for i in range(4):
    x=list[i]
    for a,b in x:
        lv+=a
        mn+=b
        if mn<0:
            print('No')
            exit()
        mn=lv
if lv==0:
    print('Yes')
else:
    print('No')
#print(lv,mn)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
for i in s:
    mi = 0
    tmp = 0
    for c in i:
        if c=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if mi==0:
        a.append((0, tmp))
    else:
        b.append((mi, tmp))
b.sort(key=lambda x:(-x[1],-x[0]))
tmp = 0
for i, j in a+b:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
N=int(input())

L=[]
for i in range(N):
    s=input()
    Min=0
    cnt=0
    for j in range(len(s)):
        if s[j]=="(":
            cnt+=1
        else:
            cnt-=1
            if Min>cnt:
                Min=cnt
    L.append([-Min,-cnt])
#print(L)

Sum=0
for i,j in L:
    Sum+=j
if Sum!=0:
    print("No")
else:
    #print("Yes?")
    cnt=0
    for i,j in L:
        if cnt+(-i)<0:
            print("No")
            exit()
        cnt+=(-j)
    print("Yes")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
for i in s:
    mi = 0
    tmp = 0
    for c in i:
        if c=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if mi==0:
        a.append((0, tmp))
    else:
        b.append((mi, tmp))
b.sort(key=lambda x:(-x[1],x[0]))
tmp = 0
for i, j in a+b:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
for i in s:
    mi = 0
    tmp = 0
    for c in i:
        if c=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if mi==0:
        a.append((0, tmp))
    else:
        b.append((mi, tmp))
a.sort(key=lambda x:(-x[1],x[0]))
tmp = 0
for i, j in a+b:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
import numpy  as np


def parse(brackets):
    m = 0
    f = 0
    for c in brackets:
        if c == "(":
            f += 1
        else:
            f -= 1
        if m > f:
            m = f
    return m, f


def climbable(g, reverse=False):
    if len(g) == 0:
        return True
    if reverse:
        g = g * np.array([1,-1])
        g[:,0] = np.sum(g)
    top = np.sum(g[g[:,0]>=0][:,1])
    v = g[g[:,0]<0]
    while len(v) > 0:
        flag = v[:,0]+top>=0
        if not np.any(flag):
            return False
        if np.all(flag):
            return True
        top += np.sum(v[flag][:,1])
        v = v[np.logical_not(flag)]
    return True


def main():
    n = int(input())
    g = np.stack([np.array(parse(input().strip()), dtype=int) for _ in range(n)])
    if np.sum(g[:, 1]) != 0:
        return False
    f = g[:, 1]
    return climbable(g[f>=0]) and climbable(g[f<0], reverse=True)


if __name__ == "__main__":
    print("Yes" if main() else "No")

Submission status: Wrong Answer

Solution code:
N = int(input())

mem = [0] * N
for i in range(N):
    s = input()
    x = 0
    y = 0
    for j in range(len(s)):
        if s[j] == '(':
            x += 1
        else:
            x -= 1
            y = min(x, y)
    mem[i] = (x, y)

mem_a = sorted([ele for ele in mem if ele[0] > 0], key=lambda x: -x[1])
mem_b = sorted([ele for ele in mem if ele[0] <= 0], key=lambda x: -x[1])

ans = True
val = 0
for ele in mem_a:
    if val + ele[1] < 0:
        ans = False
    val += ele[0]
for ele in mem_b:
    if val + ele[1] < 0:
        ans = False
    val += ele[0]


YesNo = lambda tf: print('Yes') if tf else print('No')
YesNo(ans and val == 0)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for _ in range(n)]

c=[]

for i in range(n):
    si=s[i]
    l=len(si)
    cnt=0
    mn=0
    for j in range(len(si)):
        if si[j]=="(":
            cnt+=1
        else:
            cnt-=1
        if j:
            mn=min(cnt,mn)
        else:
            mn=cnt
    c.append((mn,-cnt))
c.sort(reverse=True)
tmp=0
for ci,di in c:
    if tmp+ci<0:
        print("No")
        exit()
    tmp-=di
if tmp!=0:
    print("No")
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
# Hello from https://codeforces.com/gym/101341/problem/A

class BracketString:
    def __init__(self, s, i):
        self.i = i
 
        balance = 0
        minimum = 0
        for c in s:
            if c == "(":
                balance += 1
            else:
                balance -= 1
                minimum = min(minimum, balance)
 
        revBalance = 0
        revMinimum = 0
        for c in reversed(s):
            if c == ")":
                revBalance += 1
            else:
                revBalance -= 1
                revMinimum = min(revMinimum, revBalance)
 
        assert balance == -revBalance
 
        self.balance = balance
        self.minimum = minimum
        self.revMinimum = revMinimum
 
    @staticmethod
    def cmpForPositive(a, b):
        return b.minimum - a.minimum
 
    @staticmethod
    def cmpForNegative(a, b):
        return a.revMinimum - b.revMinimum
 
 
n = int(raw_input())
positive = []
negative = []
neutral = []
sumBalance = 0
for i in xrange(n):
    s = raw_input()
    bs = BracketString(s, i)
    if bs.balance > 0:
        positive.append(bs)
    elif bs.balance < 0:
        negative.append(bs)
    else:
        neutral.append(bs)
    sumBalance += bs.balance
 
if sumBalance != 0:
    print "NO"
    exit()
 
positive.sort(cmp=BracketString.cmpForPositive)
negative.sort(cmp=BracketString.cmpForNegative)
 
ans = []
curBalance = 0
 
for bs in positive + neutral + negative:
    if curBalance + bs.minimum < 0:
        print "No"
        exit()
    curBalance += bs.balance
    ans.append(bs.i + 1)
 
print "Yes"

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for _ in range(n)]

c=[]

for i in range(n):
    si=s[i]
    l=len(si)
    cnt=0
    mn=0
    for j in range(len(si)):
        if si[j]=="(":
            cnt+=1
        else:
            cnt-=1
        if j:
            mn=min(cnt,mn)
        else:
            mn=cnt
    c.append((mn,cnt))
c.sort(reverse=True)
tmp=0
for ci,di in c:
    if tmp+ci<0:
        print("No")
        exit()
    tmp+=di
if tmp!=0:
    print("No")
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
N = int(input())
S_list = [input() for _ in range(N)]

min_revr, min_revl = 10**20, 10**20
left, rev_l, rev_r, right = 0, 0, 0, 0
for S in S_list:
    stack = []
    l, r = 0, 0
    for s in S:
        if s == '(':
            if stack:
                if stack[-1] == ')':
                    stack.append('(')
                    l = 1
                else:
                    stack.append('(')
                    l += 1
            else:
                stack.append('(')
                l = 1
        else:   # s==')'
            if stack:
                if stack[-1] == '(':
                    stack.pop()
                    l -= 1
                else:
                    stack.append(')')
                    r += 1
            else:
                stack.append(')')
                r = 1
    if l == 0:
        right += r
    elif r == 0:
        left += l
    else:
        rev_r += r
        rev_l += l
        min_revr = min(min_revr, rev_r)
        min_revl = min(min_revl, rev_l)

if min_revr == 10**20:
    min_revr = 0
if min_revl == 10**20:
    min_revl = 0

if left+rev_l == rev_r+right and left >= min_revr and right >= min_revl:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())
S_list = [input() for _ in range(N)]

min_revr, min_revl = 10**20, 10**20
left, rev_l, rev_r, right = 0, 0, 0, 0
for S in S_list:
    stack = []
    l, r = 0, 0
    for s in S:
        if s == '(':
            if stack:
                if stack[-1] == ')':
                    stack.append('(')
                    l = 1
                else:
                    stack.append('(')
                    l += 1
            else:
                stack.append('(')
                l = 1
        else:   # s==')'
            if stack:
                if stack[-1] == '(':
                    stack.pop()
                    l -= 1
                else:
                    stack.append(')')
                    r += 1
            else:
                stack.append(')')
                r = 1
    if l == 0:
        right += r
    elif r == 0:
        left += l
    else:
        rev_r += r
        rev_l += l
        min_revr = min(min_revr, rev_r)
        min_revl = min(min_revl, rev_l)

if min_revr == 10**20:
    min_revr = 0
if min_revl == 10**20:
    min_revl = 0

if left+rev_l == rev_r+right and left >= 1 and right >= 1:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())
S_list = [input() for _ in range(N)]

min_revr, min_revl = 10**20, 10**20
left, rev_l, rev_r, right = 0, 0, 0, 0
for S in S_list:
    stack = []
    l, r = 0, 0
    for s in S:
        if s == '(':
            if stack:
                if stack[-1] == ')':
                    stack.append('(')
                    l = 1
                else:
                    stack.append('(')
                    l += 1
            else:
                stack.append('(')
                l = 1
        else:   # s==')'
            if stack:
                if stack[-1] == '(':
                    stack.pop()
                    l -= 1
                else:
                    stack.append(')')
                    r += 1
            else:
                stack.append(')')
                r = 1
    if l == 0:
        right += r
    elif r == 0:
        left += l
    else:
        rev_r += r
        rev_l += l
        min_revr = min(min_revr, rev_r)
        min_revl = min(min_revl, rev_l)

if min_revr == 10**20:
    min_revr = 0
if min_revl == 10**20:
    min_revl = 0

if left+rev_l == rev_r+right:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())
S_list = [input() for _ in range(N)]

min_revr, min_revl = 10**20, 10**20
left, rev_l, rev_r, right = 0, 0, 0, 0
for S in S_list:
    stack = []
    l, r = 0, 0
    for s in S:
        if s == '(':
            if stack:
                if stack[-1] == ')':
                    stack.append('(')
                    l = 1
                else:
                    stack.append('(')
                    l += 1
            else:
                stack.append('(')
                l = 1
        else:   # s==')'
            if stack and stack[-1] == '(':
                stack.pop()
                l -= 1
            else:
                stack.append(')')
                r += 1
    if l == 0:
        right += r
    elif r == 0:
        left += l
    else:
        rev_r += r
        rev_l += l
        min_revr = min(min_revr, rev_r)
        min_revl = min(min_revl, rev_l)

if min_revr == 10**20:
    min_revr = 0
if min_revl == 10**20:
    min_revl = 0

if left+rev_l == rev_r+right:
    print('Yes')
else:
    print('No')