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:
def parse(s):
  out = []
  x = 0
  for c in s:    
    if c == "(":
      if x < 0:
        out.append(x)
        x = 1
      else:
        x += 1
    if c == ")":
      x -= 1
  if x != 0:
    out.append(x)
  return tuple(out)

def main():
  n = int(input())
  inputs = []
  for i in range(n):
    s = input()
    x = parse(s)
    inputs.append(x)
  inputs.sort(reverse=True)
  seq = ()
  for i in inputs:
    seq += i
  #print(seq)
  state = 0
  for v in seq:
    state += v
    if state < 0:
      print("No")
      return
  print("Yes")
main()

Submission status: Wrong Answer

Solution code:
def parse(s):
  out = []
  x = 0
  for c in s:
    if c == "(":
      if x >= 0:
        x += 1
      else:
        out.append(x)
        x = 1
    if c == ")":
      if x <= 0:
        x -= 1
      else:
        out.append(x)
        x = -1
  if x != 0:
    out.append(x)
  return tuple(out)

def main():
  n = int(input())
  inputs = []
  for i in range(n):
    s = input()
    x = parse(s)
    inputs.append(x)
  inputs.sort(reverse=True)
  seq = ()
  for i in inputs:
    seq += i
  #print(seq)
  state = 0
  for v in seq:
    state += v
    if state < 0:
      print("No")
      return
  print("Yes")
main()

Submission status: Wrong Answer

Solution code:
n=int(input())
s=''
for _ in range(n):
  s+=input()
m1,m2=0,0
for i in s:
  if(i=='('):
    m1+=1
  else:
    m2+=1
if(m1==m2):
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
# Author : code_marshal

def brac_check(s):
    m = '(' ; n = ')' ; check = 0
    for i in s:
        if i == m : check+=1
        else : check-=1
        if check < 0 : return True
    return check

incomplete=[]
for i in range(int(input())):
    s = input()
    if brac_check(s) : incomplete.append(s)

incomplete.sort()
s=''.join(incomplete)

if brac_check(s) : print("No")
else : print("Yes")

Submission status: Wrong Answer

Solution code:
N = int(input())
Up = []
Down = []
for _ in range(N):
    S = input()
    L = [0]
    for __ in range(len(S)):
        if S[__] == '(':
            L.append(L[-1] + 1)
        else:
            L.append(L[-1] - 1)
    las = L[-1]
    mi = min(L)
    if las > 0:
        Up.append((mi, las))
    else:
        Down.append((mi, las))

Up.sort(reverse=True)
Down.sort()

now = 0
for i, j in Up:
    if now + i < 0:
        print('No')
        exit()
    else:
        now += j
for i, j in Down:
    if now + i < 0:
        print('No')
        exit()
    else:
        now += j
if now == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [list(input()) for i in range(N)]
check1 = 0
check2 = 0

UPDOWN = [0]*N
LOWEST = [0]*N


for i in range(N):
    updown=0
    lowest=0
    
    for j in S[i]:
        if j == ")":
            updown -= 1
        else:
            updown += 1
        lowest = min(updown,lowest)
    
    UPDOWN[i]+=updown
    LOWEST[i]+=lowest
    
temp = sorted(list(zip(LOWEST,UPDOWN)), key=lambda x:(-x[0],x[1]))
temp2 = sorted(list(zip(UPDOWN,LOWEST)), key=lambda x:(x[0],x[1]))

position = 0
check = 0

for i in range(N):
    if temp[i][1]<0:
      break
    if position + temp[i][0]<0:
        check += 1
        break
    position += temp[i][1]

position2 = 0
check2 = 0
for i in range(N):
    if temp2[i][0]>0:
      break
    position2 -= temp2[i][0]
    if position2 + temp2[i][1]<0:
        check2 += 1
        break


if check == 0 and check2 == 0 and position == position2 :
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    on=0
    L=[]
    cn=0#後ろに置くやつ

    for i in range(N):
        s=input()
        close=0
        openn=0
        for ch in s:
            if ch==")":
                if openn==0:
                    close+=1
                else:
                    openn-=1
            else:
                openn+=1
        if close==0:
            on+=openn
        elif openn==0:
            cn+=close
        else:
            L.append((close,-1*openn))
    L.sort()
    flag=1
    
    #print(on)
    
    for co in L:
        cc=co[0]
        oo=co[1]
        #print(cc,oo)
        on-=cc
        if on<0:
            flag=0
            break
        on-=oo
        
    on-=cn   
    
    if on!=0:
        flag=0
        
    if flag==1:
        print("Yes")
    else:
        print("No")
        
        
    
      
        
    

main()

Submission status: Wrong Answer

Solution code:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    on=0
    L=[]

    for i in range(N):
        s=input()
        close=0
        openn=0
        for ch in s:
            if ch==")":
                if openn==0:
                    close+=1
                else:
                    openn-=1
            else:
                openn+=1
        if close==0:
            on+=openn
        else:
            L.append((close,-1*openn))
    L.sort()
    flag=1
    
    #print(on)
    
    for co in L:
        cc=co[0]
        oo=co[1]
        #print(cc,oo)
        on-=cc
        if on<0:
            flag=0
            break
        on-=oo
        
    #print(on)    
    
    if on!=0:
        flag=0
        
    if flag==1:
        print("Yes")
    else:
        print("No")
        
        
    
      
        
    

main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [list(input()) for i in range(N)]
check1 = 0
check2 = 0

UPDOWN = [0]*N
LOWEST = [0]*N


for i in range(N):
    updown=0
    lowest=0
    
    for j in S[i]:
        if j == ")":
            updown -= 1
        else:
            updown += 1
        lowest = min(updown,lowest)
    
    UPDOWN[i]+=updown
    LOWEST[i]+=lowest
    
temp = list(zip(LOWEST,UPDOWN))
temp2 = sorted(temp, key=lambda x:(-x[0], x[1]))

position = 0
check = 0

for i in range(N):
    if position + temp2[i][0]<0:
        check += 1
        break
    position += temp2[i][1]


if check == 0 and position == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [list(input()) for i in range(N)]
check1 = 0
check2 = 0

UPDOWN = [0]*N
LOWEST = [0]*N


for i in range(N):
    updown=0
    lowest=0
    
    for j in S[i]:
        if j == ")":
            updown -= 1
        else:
            updown += 1
        lowest = min(updown,lowest)
    
    UPDOWN[i]+=updown
    LOWEST[i]+=lowest
    
temp = list(zip(LOWEST,UPDOWN))
temp2 = sorted(temp, key=lambda x:(x[0], -x[1]))

position = 0
check = 0

for i in range(N):
    if position + temp2[i][0]<0:
        check += 1
        break
    position += temp2[i][1]


if check == 0 and position == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [list(input()) for i in range(N)]
check1 = 0
check2 = 0

UPDOWN = [0]*N
LOWEST = [0]*N


for i in range(N):
    updown=0
    lowest=0
    
    for j in S[i]:
        if j == ")":
            updown -= 1
        else:
            updown += 1
        lowest = min(updown,lowest)
    
    UPDOWN[i]+=updown
    LOWEST[i]+=lowest
    
temp = list(zip(UPDOWN,LOWEST))
temp2 = sorted(temp, key=lambda x:(-x[0], x[1]))

position = 0
check = 0

for i in range(N):
    if position + temp2[i][1]<0:
        check += 1
        break
    position += temp2[i][0]


if check == 0 and position == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    co=[[0,0]for _ in range(N)]
    for i in range(N):
        s=input()
        close=0
        openn=0
        for ch in s:
            if ch==")":
                if openn==0:
                    close+=1
                else:
                    openn-=1
            else:
                openn+=1
                
        co[i]=[close,-1*openn]
    co.sort()
    cn=0
    on=0
    flag=1
    for i in range(N):
        cn+=co[i][0]
        if cn>on:
            flag=0
            break
        on-=co[i][1]
    if on!=cn:
        flag=0
        
    if flag==1:
        print("Yes")
    else:
        print("No")
    

main()

Submission status: Wrong Answer

Solution code:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    co=[[0,0]for _ in range(N)]
    for i in range(N):
        s=input()
        close=0
        openn=0
        for ch in s:
            if ch==")":
                if openn==0:
                    close+=1
                else:
                    openn-=1
            else:
                openn+=1
                
        co[i]=[close,openn]
    co.sort()
    cn=0
    on=0
    flag=1
    for i in range(N):
        cn+=co[i][0]
        if cn>on:
            flag=0
            break
        on+=co[i][1]
    if on!=cn:
        flag=0
        
    if flag==1:
        print("Yes")
    else:
        print("No")
    

main()

Submission status: Wrong Answer

Solution code:
import itertools
def H(data):
    tmp = data.replace("()","")
    if tmp == data:
        return tmp
    H(tmp)
    
N = int(input())
var = []
for n in range(N):
    tmp = H(input())
    if tmp == None:
        pass
    else:
        var.append(tmp)
# print(var)
def T(data):
    tmp = data.replace("()","")
    if tmp == "":
        print("Yes")
        exit()
    elif tmp == data:
        pass
    else:
        T(tmp)
    return
for temp in itertools.permutations(var,len(var)):
    T("".join(temp))
print("No")

Submission status: Wrong Answer

Solution code:
N = int(input())
pl = []
mi = []

for i in range(N):
    _m = 0
    _t = 0
    s = input()
    for c in s:
        if c == ")":
            _t -= 1
            _m = min(_m, _t)
        else:
            _t += 1
    if _t > 0:
        pl.append([-_m, -_t]) # sortの調整のために負にしている
    else:
        mi.append([_m, - _t])
pl = sorted(pl)
mi = sorted(mi)

#import IPython;IPython.embed()


if len(pl) == 0:
    if mi[0][0] == 0:
        print("Yes")
    else:
        print("No")
    exit()
if len(mi) == 0:
    print("No")
    exit()

tot = 0
for m, nt in pl:
    if tot - m < 0: # これはひくではないのでは?
        print("No")
        exit()
    else:
        tot -= nt
for m, nt in mi:
    if tot + m <0:
        print("No")
        exit()
    else:
        tot -= nt
if tot != 0:
    print("No")
else:
    print("Yes")
        



# どういう時できるか
# で並べ方
# tot, minの情報が必要
# totが+のものはminとtotを気をつける必要がある
# minがsum以上であればどんどん足していけばいい.
# なのでminが大きい順にsortし,totの大きい順にsort
# その次はtotがminusの中ではminが小さい順に加える
# 合計が0になる時Yes,それ意外No.

Submission status: Wrong Answer

Solution code:
n = int(input())
li = []

for _ in range(n):
    s = input()
    count = 0
    while True:
        if count + 1 >= len(s):
            break
        if s[count] == '(' and s[count+1] == ')':
            s = s[:count] + s[count+2:]
            count = max(0, count-1)
        else:
            count += 1
    li.append(s)

li2 = []
for i in li:
    count = 0
    for j in range(len(i)):
        if i[j] == ')':
            count += 1
        else:
            break
    li2.append((count, len(i) - count))

li3 = []
li4 = []

for i in li2:
    if i[0] <= i[1]:
        li3.append(i)
    else:
        li4.append(i)

li3.sort()
li4.sort(key = lambda x: x[1], reverse = True)

now = [0,0]

for l,r in li3:
    if l == 0 and r == 0:
        pass
    elif l == 0:
        now[1] += r
    else:
        now[1] -= l
        if now[1] < 0:
            print('No')
            exit()
        now[1] += r

for l,r in li4:
    if l == 0 and r == 0:
        pass
    elif r == 0:
        now[1] -= l
        if now[1] < 0:
            print('No')
            exit()
    else:
        now[1] += r - l

if now[0] == 0 and now[1] == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n = int(input())
li = []

for _ in range(n):
    s = input()
    count = 0
    while True:
        if count + 1 >= len(s):
            break
        if s[count] == '(' and s[count+1] == ')':
            s = s[:count] + s[count+2:]
            count = max(0, count-1)
        else:
            count += 1
    li.append(s)

li2 = []
for i in li:
    count = 0
    for j in range(len(i)):
        if i[j] == ')':
            count += 1
        else:
            break
    li2.append((count, len(i) - count))

li3 = []
li4 = []

for i in li2:
    if i[0] <= i[1]:
        li3.append(i)
    else:
        li4.append(i)

li3.sort()
li4.sort(key = lambda x: x[1], reverse = True)

now = [0,0]

for l,r in li3:
    if l == 0 and r == 0:
        pass
    elif l == 0:
        now[1] += r
    else:
        now[1] -= l
        if now[1] < 0:
            print('No')
            exit()
        now[1] += r

for l,r in li4:
    if l == 0 and r == 0:
        pass
    elif r == 0:
        now[1] -= l
    else:
        now[1] += r - l

if now[0] == 0 and now[1] == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())
pl = []
mi = []

for i in range(N):
    _m = 0
    _t = 0
    s = input()
    for c in s:
        if c == ")":
            _t -= 1
            _m = min(_m, _t)
        else:
            _t += 1
    if _t > 0:
        pl.append([-_m, -_t]) # sortの調整のために負にしている
    else:
        mi.append([_m, - _t])
pl = sorted(pl)
mi = sorted(mi)



if len(pl) == 0:
    if mi[0][0] == 0:
        print("Yes")
    else:
        print("No")
    exit()
if len(mi) == 0:
    print("No")
    exit()

tot = 0
for m, nt in pl:
    if tot + m < 0: # これはひくではないのでは?
        print("No")
        exit()
    else:
        tot -= nt
for m, nt in mi:
    if tot - m <0:
        print("No")
        exit()
    else:
        tot -= nt
if tot != 0:
    print("No")
else:
    print("Yes")
        



# どういう時できるか
# で並べ方
# tot, minの情報が必要
# totが+のものはminとtotを気をつける必要がある
# minがsum以上であればどんどん足していけばいい.
# なのでminが大きい順にsortし,totの大きい順にsort
# その次はtotがminusの中ではminが小さい順に加える
# 合計が0になる時Yes,それ意外No.

Submission status: Wrong Answer

Solution code:
n = int(input())
li = []

for _ in range(n):
    s = input()
    count = 0
    while True:
        if count + 1 >= len(s):
            break
        if s[count] == '(' and s[count+1] == ')':
            s = s[:count] + s[count+2:]
            count = max(0, count-1)
        else:
            count += 1
    li.append(s)

li2 = []
for i in li:
    count = 0
    for j in range(len(i)):
        if i[j] == ')':
            count += 1
        else:
            break
    li2.append((count, len(i) - count))

li3 = []
li4 = []

for i in li2:
    if i[0] <= i[1]:
        li3.append(i)
    else:
        li4.append(i)

li3.sort()
li4.sort(key = lambda x: x[1], reverse = True)

now = [0,0]
for l,r in li3:
    if l == 0 and r == 0:
        pass
    elif r == 0:
        if now[1] == 0:
            now[0] += l
        elif 0 < now[1] <= l:
            now[0] += l - now[1]
            now[1] = 0
        else:
            now[1] -= l
    elif l == 0:
        if now[0] == 0:
            now[1] += r
        elif 0 < now[0] <= r:
            now[1] += r - now[0]
            now[0] = 0
        else:
            now[0] -= r
    else:
        if now[0] == 0 and now[1] == 0:
            now[0] = l
            now[1] = r
        elif min(now[0], l) > min(now[1], r):
            if now[0] > l:
                now[0] -= l
            else:
                now[1] += l - now[0]
                now[0] = 0
        else:
            if now[1] > r:
                now[1] -= r
            else:
                now[0] += r - now[1]
                now[1] = 0

for l,r in li4:
    if l == 0 and r == 0:
        pass
    elif r == 0:
        if now[1] == 0:
            now[0] += l
        elif 0 < now[1] <= l:
            now[0] += l - now[1]
            now[1] = 0
        else:
            now[1] -= l
    elif l == 0:
        if now[0] == 0:
            now[1] += r
        elif 0 < now[0] <= r:
            now[1] += r - now[0]
            now[0] = 0
        else:
            now[0] -= r
    else:
        if now[0] == 0 and now[1] == 0:
            now[0] = l
            now[1] = r
        elif min(now[0], l) > min(now[1], r):
            if now[0] > l:
                now[0] -= l
            else:
                now[1] += l - now[0]
                now[0] = 0
        else:
            if now[1] > r:
                now[1] -= r
            else:
                now[0] += r - now[1]
                now[1] = 0

if now[0] == 0 and now[1] == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())
pl = []
mi = []

for i in range(N):
    _m = 0
    _t = 0
    s = input()
    for c in s:
        if c == ")":
            _t -= 1
            _m = min(_m, _t)
        else:
            _t += 1
    if _t > 0:
        pl.append([_m, -_t]) # sortの調整のために負にしている
        # totalは大きい順
        # mは小さい順
    else:
        mi.append([_m, - _t])
pl = sorted(pl)
mi = sorted(mi)

#import IPython;IPython.embed()


if len(pl) == 0:
    if mi[0][0] == 0:
        print("Yes")
    else:
        print("No")
    exit()
if len(mi) == 0:
    print("No")
    exit()

tot = 0
for m, nt in pl:
    if tot + m < 0:
        print("No")
        exit()
    else:
        tot -= nt
for m, nt in mi:
    if tot + m <0:
        print("No")
        exit()
    else:
        tot -= nt
if tot != 0:
    print("No")
else:
    print("Yes")
        



# どういう時できるか
# で並べ方
# tot, minの情報が必要
# totが+のものはminとtotを気をつける必要がある
# minがsum以上であればどんどん足していけばいい.
# なのでminが小さい順にsortし,totの大きい順にsort
# その次はtotがminusの中ではminが小さい順に加える
# 合計が0になる時Yes,それ意外No.

Submission status: Wrong Answer

Solution code:
N = int(input())
pl = []
mi = []

for i in range(N):
    _m = 0
    _t = 0
    s = input()
    for c in s:
        if c == ")":
            _t -= 1
            _m = min(_m, _t)
        else:
            _t += 1
    if _t > 0:
        pl.append([_m, -_t]) # sortの調整のために負にしている
        # totalは大きい順
        # mは小さい順
    else:
        mi.append([_m, - _t])
pl = sorted(pl)
mi = sorted(mi)

# import IPython;IPython.embed()


if len(pl) == 0:
    if mi[0][0] == 0:
        print("Yes")
    else:
        print("No")
    exit()
if len(mi) == 0:
    print("No")
    exit()

tot = 0
for m, nt in pl:
    if tot + m < 0:
        print("No")
        exit()
    else:
        tot += nt
for m, nt in mi:
    if tot + m <0:
        print("No")
        exit()
    else:
        tot -= nt
if tot != 0:
    print("No")
else:
    print("Yes")
        



# どういう時できるか
# で並べ方
# tot, minの情報が必要
# totが+のものはminとtotを気をつける必要がある
# minがsum以上であればどんどん足していけばいい.
# なのでminが小さい順にsortし,totの大きい順にsort
# その次はtotがminusの中ではminが小さい順に加える
# 合計が0になる時Yes,それ意外No.

Submission status: Wrong Answer

Solution code:
N = int(input())
pl = []
mi = []

for i in range(N):
    _m = 0
    _t = 0
    s = input()
    for c in s:
        if c == ")":
            _t -= 1
            _m = min(_m, _t)
        else:
            _t += 1
    if _t > 0:
        pl.append([- _m, _t]) # sortの調整のために負にしている
    else:
        mi.append([_m, - _t])
pl = sorted(pl)
mi = sorted(mi)



if len(pl) == 0:
    if mi[0][0] == 0:
        print("Yes")
    else:
        print("No")
    exit()
if len(mi) == 0:
    print("No")
    exit()

tot = 0
for m, nt in pl:
    if tot - m < 0:
        print("No")
        exit()
    else:
        tot += nt
for m, nt in mi:
    if tot + m <0:
        print("No")
        exit()
    else:
        tot -= nt
if tot != 0:
    print("No")
else:
    print("Yes")
        



# どういう時できるか
# で並べ方
# tot, minの情報が必要
# totが+のものはminとtotを気をつける必要がある
# minがsum以上であればどんどん足していけばいい.
# なのでminが小さい順にsortし,totの大きい順にsort
# その次はtotがminusの中ではminが小さい順に加える
# 合計が0になる時Yes,それ意外No.

Submission status: Wrong Answer

Solution code:
N = int(input())
pl = []
mi = []

for i in range(N):
    _m = 0
    _t = 0
    s = input()
    for c in s:
        if c == ")":
            _t -= 1
            _m = min(_m, _t)
        else:
            _t += 1
    if _t > 0:
        pl.append([- _m, _t]) # sortの調整のために負にしている
    else:
        mi.append([_m, - _t])
pl = sorted(pl)
mi = sorted(mi)



if len(pl) == 0:
    if mi[0][0] == 0:
        print("Yes")
    else:
        print("No")
    exit()
if len(mi) == 0:
    print("No")
    exit()

tot = 0
for m, nt in pl:
    if tot + m < 0:
        print("No")
        exit()
    else:
        tot += nt
for m, nt in mi:
    if tot - m <0:
        print("No")
        exit()
    else:
        tot -= nt
if tot != 0:
    print("No")
else:
    print("Yes")
        



# どういう時できるか
# で並べ方
# tot, minの情報が必要
# totが+のものはminとtotを気をつける必要がある
# minがsum以上であればどんどん足していけばいい.
# なのでminが小さい順にsortし,totの大きい順にsort
# その次はtotがminusの中ではminが小さい順に加える
# 合計が0になる時Yes,それ意外No.

Submission status: Wrong Answer

Solution code:
N = int(input())
pl = []
mi = []

for i in range(N):
    _m = 0
    _t = 0
    s = input()
    for c in s:
        if c == ")":
            _t -= 1
            _m = min(_m, _t)
        else:
            _t += 1
    if _t > 0:
        pl.append([_m, -_t]) # sortの調整のために負にしている
    else:
        mi.append([_m, - _t])
pl = sorted(pl)
mi = sorted(mi)


if len(pl) == 0:
    if mi[0][0] == 0:
        print("Yes")
    else:
        print("No")
    exit()
if len(mi) == 0:
    print("No")
    exit()

tot = 0
for m, nt in pl:
    if tot - m < 0:
        print("No")
        exit()
    else:
        tot -= nt
for m, nt in mi:
    if tot - m <0:
        print("No")
        exit()
    else:
        tot -= nt
if tot != 0:
    print("No")
else:
    print("Yes")
        



# どういう時できるか
# で並べ方
# tot, minの情報が必要
# totが+のものはminとtotを気をつける必要がある
# minがsum以上であればどんどん足していけばいい.
# なのでminが小さい順にsortし,totの大きい順にsort
# その次はtotがminusの中ではminが小さい順に加える
# 合計が0になる時Yes,それ意外No.

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
a = []; b = []; c = []
level = 0
for i in range(n):
    z = input()
    mm = 0; M = 0
    for x in z:
        if x == '(':
            M += 1
        elif x == ')':
            M -= 1
        mm = min(mm,M)
    if M > 0:
        a.append((mm,M))
    elif M == 0:
        b.append((mm,M))
    else:
        c.append((mm,M))

a.sort(reverse = True); b.sort(); c.sort()
d = a + b + c

for z in d:
    if level + z[0] >= 0:
        level += z[1]
    else:
        level = -1;break
if level == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
a = []; b = []; c = []
level = 0
for i in range(n):
    z = input()
    mm = 0; M = 0
    for x in z:
        if x == '(':
            M += 1
        elif x == ')':
            M -= 1
        mm = min(mm,M)
    if M > 0:
        a.append((mm,M))
    elif M == 0:
        b.append((mm,M))
    else:
        c.append((mm,M))

a.sort(reverse = True); b.sort(); c.sort(reverse = True)
d = a + b + c

for z in d:
    if level + z[0] >= 0:
        level += z[1]
    else:
        level = -1;break
if level == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
l = [[0,0] for _ in range(n)]
for i in range(n):
    z = input()
    mm = 0
    for x in z:
        if x == '(':
            l[i][1] += 1
        elif x == ')':
            l[i][1] -= 1
        mm = min(mm,l[i][1]) 
    l[i][0] = mm
level = 0
a = []; b = []; c = []
for z in l:
    if z[1] > 0:
        a.append(z)
    elif z[1] == 0:
        b.append(z)
    else:
        c.append(z)
a.sort(reverse = True)
b.sort()
c.sort(reverse = True)
d = a + b + c

for z in d:
    if level + z[0] >= 0:
        level += z[1]
    else:
        level = -1;break
if level == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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

T = [0]*N
M = [0]*N
for i in range(N):
    res = 0
    idx = 0
    s = S[i]
    l = len(s)
    m = 0
    while idx<l:
        if s[idx]==')':
            res -= 1
            m = min(res,m)
        else:
            res += 1
        idx += 1
    
    M[i] = -m
    T[i] = res-m

MT = []
for i in range(N):
    MT.append([M[i], -(T[i]-M[i])])

MT.sort()

if MT[0][0] != 0:
    print('No')
    exit()

res = 0
for m,t in MT:
    if res<m:
        print('No')
        exit()
    res -= t
if res!=0:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6) #再帰関数の上限
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque 
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする


def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)

def main():
    mod = 10**9+7
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    #A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    l=[]
    l2=[]
    for i in range(N):
        S=input()
        lmin=0
        tot=0
        for s in S:
            if s=='(':
                tot+=1
            else:
                tot-=1
                lmin=min(lmin,tot)
        if tot>=0:
            heappush(l,(-lmin,tot))
        else:
            heappush(l2,(lmin,tot))
    tot=0
    cmin=0

    while len(l):
        lmin,ltot=heappop(l)
        lmin=-lmin
        cmin=tot+lmin
        tot+=ltot
        if cmin<0:
            print('No')
            exit()
    while len(l2):
        lmin,ltot=heappop(l2)
        cmin=tot+lmin
        tot+=ltot
        if cmin<0:
            print('No')
            exit()
    if tot==0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6) #再帰関数の上限
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque 
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする


def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)

def main():
    mod = 10**9+7
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    #A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    l=[]
    mt=0
    for i in range(N):
        S=input()
        lmin=0
        tot=0
        for s in S:
            if s=='(':
                tot+=1
            else:
                tot-=1
                lmin=min(lmin,tot)
        if tot>=0:
            heappush(l,(-lmin,tot))
        else:
            mt+=tot
    tot=0
    cmin=0

    while len(l):
        lmin,ltot=heappop(l)
        lmin=-lmin
        cmin=tot+lmin
        tot+=ltot
        if cmin<0:
            print('No')
            exit()
    tot+=mt
    if tot==0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

LRM = []
for s in S:
    left, right, min_bracket = 0, 0, 0
    tmp = 0
    for ss in s:
        if ss == "(":
            left += 1
            tmp += 1
        else:
            right += 1
            tmp -= 1
        min_bracket = min(tmp, min_bracket)
    LRM.append([left, right, min_bracket])
#print(LRM)

l_sum, r_sum = 0, 0
for l, r, m in LRM:
    l_sum += l
    r_sum += r
if l_sum != r_sum:
    print("No")
    exit()
    
plus, plus_m, minus = [], [], []
for i, (l, r, m) in enumerate(LRM):
    if m >= 0:
        plus.append([l, r, m, i])
    else:
        if l >= r:
            plus_m.append([l, r, m, i])
        else:
            minus.append([l, r, m, i])
plus_m.sort(key = lambda x: x[2])
minus.sort(key = lambda x: x[2])

cnt = 0
for l, r, m, i in plus + plus_m + minus:
    #print(S[i])
    for s in S[i]:
        if s == "(":
            cnt += 1
        else:
            cnt -= 1
        if cnt < 0:
            print("No")
            exit()
print("Yes")