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:
n = int(input())
lr1, lr2 = [], []
flg = 0
for _ in range(n):
    s = input()
    m = len(s)

    l, r = 0, 0
    tmp = 0
    for i in range(m):
        if s[i] == ')':
            tmp += 1
        else:
            tmp -= 1
        l = max(l, tmp)
    tmp = 0
    for i in range(m):
        if s[m-1-i] == '(':
            tmp += 1
        else:
            tmp -= 1
        r = max(r, tmp)

    if l == r == 0:
        flg += 1
    elif r - l >= 0:
        lr1.append((l, r))
    else:
        lr2.append((l, r))

lr1.sort(key=lambda x: (-x[0], -x[1]))
lr2.sort(key=lambda x: (-x[1], -x[0]))
lr = lr1 + lr2[::-1]
#print(lr)

def judge():
    x = len(lr)
    check = True
    tmp = 0
    for i in range(x):
        l, r = lr[i]
        tmp -= l
        if tmp < 0:
            check = False
            break
        tmp += r
    if tmp > 0:
        check = False

    if check:
        print('Yes')
    else:
        print('No')


if flg == 0:
    if lr[0][0] != 0 or lr[-1][1] != 0:
        print('No')
    else:
        judge()

elif flg == 1:
    if lr[0][0] != 0 and lr[-1][1] != 0:
        print('No')
    else:
        judge()

else:
    judge()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N = ir()
    pos = []
    neg = []
    for i in range(N):
        tmp = 0
        b = 0
        for c in sr():
            if c == '(':
                tmp += 1
            else:
                tmp -= 1
                b = min(b, tmp)
        if tmp > 0:
            pos.append([tmp, b])
        else:
            neg.append([tmp, b])
    pos = sorted(pos, key=lambda x: (x[0], x[1]))[::-1]
    neg = sorted(neg, key=lambda x: (x[0], x[1]))[::-1]
    c = 0
    for p in pos:
        t, b = p
        if c+b < 0:
            print('No')
            return
        c += t
    for n in neg:
        t, b = n
        if c+b < 0:
            print('No')
            return
        c += t
    if c == 0:
        print('Yes')
    else:
        print('No')
resolve()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations, product
#import bisect# lower_bound etc
#import numpy as np
def run():
    N = int(input())
    dic = {'(':- 1, ')': 1}
    dic2 = {'(': 0, ')': 1}
    ret = 0
    left = 0
    right = 0
    mid = [0, 0]
    for _ in range(N):
        S = input()
        arr = []
        tmp = [0, 0]
        for s in S:
            ret += dic[s]
            arr.append(dic2[s])
            tmp[(dic2[s]+1) % 2] += 1
            try:
                if arr[-1] == 1 and arr[-2] == 0:
                    arr.pop()
                    arr.pop()
                    tmp[0] -= 1
                    tmp[1] -= 1

            except:
                continue
        #print('arr: {}'.format(arr))
        #print('tmp: {}'.format(tmp))

        if arr == []: continue
        if tmp[0] == 0:
            left += tmp[1]
        elif tmp[1] == 0:
            right += tmp[0]
        else:
            max_val = max(mid + tmp)
            if max_val == mid[1] or max_val == tmp[0]:
                if mid[1] >= tmp[0]:
                    v = mid[1] - tmp[0]
                    mid = [mid[0], tmp[1] + v]
                else:
                    v = tmp[0] - mid[1]
                    mid = [mid[0] + v, tmp[1]]
            else:
                if tmp[1] >= mid[0]:
                    v = tmp[1] - mid[0]
                    mid = [tmp[0], mid[1] + v]
                else:
                    v = mid[0] - tmp[1]
                    mid = [tmp[0] + v, mid[1]]



    if left - mid[0] == right - mid[1] and left - mid[0] >= 0:
        print('Yes')
    else:
        print('No')
if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10**7)
def run():
    N = int(input())
    dic = {'(':- 1, ')': 1}
    dic2 = {'(': 0, ')': 1}
    ret = 0
    left = 0
    right = 0
    mid = [0, 0]
    for _ in range(N):
        S = input()
        arr = []
        tmp = [0, 0]
        for s in S:
            ret += dic[s]
            arr.append(dic2[s])
            tmp[(dic2[s]+1) % 2] += 1
            try:
                if arr[-1] == 1 and arr[-2] == 0:
                    arr.pop()
                    arr.pop()
                    tmp[0] -= 1
                    tmp[1] -= 1

            except:
                continue
        #print('arr: {}'.format(arr))
        #print('tmp: {}'.format(tmp))

        if arr == []: continue
        if tmp[0] == 0:
            left += tmp[1]
        elif tmp[1] == 0:
            right += tmp[0]
        else:
            if mid[1] >= tmp[0]:
                v = mid[1] - tmp[0]
                mid = [mid[0], tmp[1] + v]
            else:
                v = tmp[0] - mid[1]
                mid = [mid[0] + v, tmp[1]]

    if left - mid[0] == right - mid[1] and left - mid[0] >= 0:
        print('Yes')
    else:
        print('No')
if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y_min, y]

def isPossible(s):
    y = 0
    for i in range(len(s)):
        y += s[i][1]
        if y < 0 or (y + s[i][0]) < 0:
            return False
    return True

n = int(input())
s_l = []
s_r = []

h = 0
for i in range(n):
    arr = statusOf(input())
    if arr[1] > 0:
        s_l.append([arr[0], arr[1]])
    else:
        s_r.append([arr[0] - arr[1], -arr[1]])
    
    h += arr[1]

s_l.sort(key=lambda s_l: (s_l[0],-s_l[1]))
s_r.sort(key=lambda s_r: (s_r[0],-s_r[1]))

if isPossible(s_l) and isPossible(s_r) and h == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y_min, y]

def isPossible(s):
    y = 0
    for i in range(len(s)):
        y += s[i][1]
        if y < 0 or (y + s[i][0]) < 0:
            return False
    return True

n = int(input())
s_l = []
s_r = []

h = 0
for i in range(n):
    arr = statusOf(input())
    if arr[1] > 0:
        s_l.append([arr[0], arr[1]])
    else:
        s_r.append([arr[0] - arr[1], -arr[1]])
    
    h += arr[1]

s_l.sort(key=lambda s_l: (-s_l[0],s_l[1]))
s_r.sort(key=lambda s_r: (-s_r[0],s_r[1]))

if isPossible(s_l) and isPossible(s_r) and h == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
plus = []
minus = []
import numpy as np
def num(words,k):
    if words[k] =="(":
        return 1
    else:
        return -1
for i in range(n):
    a = input()
    mini = 0
    a_sum = 0
    for j in range(len(a)):
        a_sum += num(a,j)
        mini = min(mini,a_sum)
        if mini < 0:
            minus.append([mini,a_sum])
        else:
            plus.append([mini,a_sum])
minus.sort(key=lambda x:(x[0],x[1]),reverse = True)
plus.sort(key=lambda x:(x[0],x[1]),reverse = True)
s = plus + minus
sums=0
true = 1
for i in range(n):
    mini,fin = s[i]
    if sums < mini:
        true = 0
        break
    sums += fin
if true == 0 or sum != 0:
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y_min, y]

def isPossible(s):
    y = 0
    for i in range(len(s)):
        y += s[i][1]
        if y < 0 or (y + s[i][0]) < 0:
            return False
    return True

n = int(input())
s_l = []
s_r = []

h = 0
for i in range(n):
    arr = statusOf(input())
    if arr[1] > 0:
        s_l.append([arr[0], arr[1]])
    else:
        s_r.append([arr[0], -arr[1]])
    
    h += arr[1]

s_l.sort(key=lambda s_l: (s_l[0],-s_l[1]))
s_r.sort(key=lambda s_r: (s_r[0],-s_r[1]))

if isPossible(s_l) and isPossible(s_r) and h == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
plus = []
minus = []
import numpy as np
def num(words,k):
    if words[k] =="(":
        return 1
    else:
        return -1
for i in range(n):
    a = input()
    mini = 0
    a_sum = 0
    for j in range(len(a)):
        a_sum += num(a,j)
        mini = min(mini,a_sum)
        if mini < 0:
            minus.append([mini,a_sum])
        else:
            plus.append([mini,a_sum])
minus.sort(key=lambda x:(x[0],x[1]),reverse = True)
plus.sort(key=lambda x:(x[0],x[1]),reverse = True)
s = plus + minus
sums=0
true = 1
for i in range(n):
    mini,fin = s[i]
    if sums < mini:
        true = 0
        break
    sums += fin
if true == 1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y_min, y]

def isPossible(s):
    y = 0
    for i in range(len(s)):
        y += s[i][1]
        if y < 0 or (y + s[i][0]) < 0:
            return False
    return True

n = int(input())
s_l = []
s_r = []

h = 0
for i in range(n):
    arr = statusOf(input())
    if arr[1] > 0:
        s_l.append([arr[0], arr[1]])
    else:
        s_r.append([arr[0], -arr[1]])
    
    h += arr[1]

s_l.sort(key=lambda s_l: (-s_l[0],s_l[1]))
s_r.sort(key=lambda s_r: (-s_r[0],s_r[1]))

if isPossible(s_l) and isPossible(s_r) and h == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y_min, y]

def isPossible(s):
    y = 0
    for i in range(len(s)):
        y += s[i][1]
        if y < 0 or (y + s[i][0]) < 0:
            return False
    return True

n = int(input())
s_l = []
s_r = []

h = 0
for i in range(n):
    arr = statusOf(input())
    if arr[1] > 0:
        s_l.append([arr[0], arr[1]])
    else:
        s_r.append([arr[0], -arr[1]])
    
    h += arr[1]

s_l.sort(key=lambda s_l: (-s_l[0],s_l[1]))
s_r.sort(key=lambda s_l: (-s_l[0],s_l[1]))

if isPossible(s_l) and isPossible(s_r) and h == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y_min, y]

def isPossible(s):
    y = 0
    for i in range(len(s)):
        y += s[i][1]
        if y < 0 or (y + s[i][0]) < 0:
            return False
    return True

n = int(input())
s_l = []
s_r = []

h = 0
for i in range(n):
    arr = statusOf(input())
    if arr[1] > 0:
        s_l.append([arr[0], arr[1]])
    else:
        s_r.append([arr[0], -arr[1]])
    
    h += arr[1]

s_l.sort(reverse=True)
s_r.sort(reverse=True)

if isPossible(s_l) and isPossible(s_r) and h == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]
n = int(input())
plus, minus = [], []
for _ in range(n):
	s = input()
	mi, cur = 0, 0
	for i in range(len(s)):
		if s[i] == "(":
			cur += 1
		else:
			cur -= 1
		mi = min(mi, cur)
	if cur >= 0:
		plus.append((mi, cur))
	else:
		minus.append((mi, cur))

plus.sort(key=lambda x: -x[0])
minus.sort(key=lambda x: x[1] - x[0])
res = plus + minus

cur = 0
for m, t in res:
	if cur + m < 0:
		print("No")
		exit()
	cur += t

if cur != 0:
	print("No")
	exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y_min, y]

n = int(input())
s = []

for i in range(n):
    s.append(statusOf(input()))

s.sort(reverse=True)

chosen = [False]*n

y = 0
for i in range(n):
    flag = False
    for j in range(len(s)):
        if (y + s[j][0]) >= 0:
            flag = True
            y += s[j][1]
            s.pop(j)
            break
    if not flag:
        print('No')

if flag:
    if y == 0:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:

n = int(input())
s = [list(input()) for i in range(n)]

l = []
ans = 0
for i in s:
    now = 0
    c = 0
    for j in i:
        if j =="(":
            now += 1
        else:
            now -= 1
        c = min(c,now)
    if c == 0:
        ans += now
    else:
        l.append([c,now])

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

for i,j in l:
    if ans + i < 0:
        print("No")
        exit()
    elif ans + i + j < 0:
        print("No")
    else:
        ans += j

if ans == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y_min, y]

n = int(input())
s = []

for i in range(n):
    s.append(statusOf(input()))

s.sort(reverse=True)

chosen = [False]*n

y = 0
for i in range(n):
    flag = False
    for j in range(len(s)):
        if (y + s[j][0]) >= 0:
            flag = True
            y += s[j][1]
            s.pop(j)
            break
    if not flag:
        print('No')

if flag and y == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from itertools import accumulate,chain
n,*s=open(0).read().split()
u=[[min(accumulate(chain([0],t),lambda a,b: a+(1 if b=="(" else -1))),2*t.count("(")-len(t)] for t in s]
m=0
for c,d in chain([x for x in u if x[1]>=0],sorted([x for x in u if x[1]<0],key=lambda z:z[0]-z[1])):
    if m+c<0:print("No");break
    m+=d
else:print("No" if m else "Yes")

Submission status: Wrong Answer

Solution code:

n = int(input())
s = [list(input()) for i in range(n)]

l = []
ans = 0
for i in s:
    now = 0
    c = 0
    for j in i:
        if j =="(":
            now += 1
        else:
            now -= 1
        c = min(c,now)
    if c == 0:
        ans += now
    else:
        l.append([c,now])

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

for i,j in l:
    if ans + i < 0:
        print("No")
        exit()
    else:
        ans += j

if ans == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:

n = int(input())
s = [list(input()) for i in range(n)]

l = []

for i in s:
    now = 0
    c = 0
    for j in i:
        if j =="(":
            now += 1
        else:
            now -= 1
        c = min(c,now)
    l.append([c,now])

l.sort(reverse =True)
ans = 0
for i in range(n):
    a,b = l[i]
    if ans + a < 0:
        print("No")
        exit()
    else:
        ans += b
        
if ans == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

n = int(input())
increasing = []
decreasing = []
for _ in range(n):
    s = input().rstrip()
    minima = 0
    fin = 0
    for ch in s:
        if ch == ")":
            fin -= 1
            if fin < minima:
                minima = fin
        else:
            fin += 1
    if fin >= 0:
        increasing.append((minima, fin))
    else:
        decreasing.append((minima - fin, fin))

increasing.sort(reverse=True)
decreasing.sort()

current = 0
for minima, diff in increasing:
    if current + minima < 0:
        print("No")
        exit()
    current += diff
for minima, diff in decreasing:
    if current + minima < 0:
        print("No")
        exit()
    current += diff
if current != 0:
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

n = int(input())
increasing = []
decreasing = []
for _ in range(n):
    s = input().rstrip()
    minima = 0
    fin = 0
    for ch in s:
        if ch == ")":
            fin -= 1
            if fin < minima:
                minima = fin
        else:
            fin += 1
    if fin >= 0:
        increasing.append((minima, fin))
    else:
        decreasing.append((fin - minima, fin))

increasing.sort(reverse=True)
decreasing.sort()

current = 0
for minima, diff in increasing:
    if current + minima < 0:
        print("No")
        exit()
    current += diff
for minima, diff in decreasing:
    if current + minima < 0:
        print("No")
        exit()
    current += diff
if current != 0:
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
def statusOf(dyck):
    y = 0
    y_min = 0
    for i in range(len(dyck)):
        if dyck[i] == '(':
            y += 1
        else:
            y -= 1
        if y < y_min:
            y_min = y
    
    return [y, y_min]

n = int(input())
s = []

for i in range(n):
    s.append(statusOf(input()))

s.sort(reverse=True)

chosen = [False]*n

y = 0
for i in range(n):
    flag = False
    for j in range(len(s)):
        if (y + s[j][1]) >= 0:
            flag = True
            y += s[j][0]
            s.pop(j)
            break
    if not flag:
        print('No')

if flag and y == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
import numpy as np
def num(words,k):
    if words[k] =="(":
        return 1
    else:
        return -1
for i in range(n):
    a = input()
    mini = 0
    a_sum = 0
    for j in range(len(a)):
        a_sum += num(a,j)
        mini = min(mini,a_sum)
    s.append([mini,a_sum])
    s.sort(key=lambda x:(x[0],x[1]),reverse = True)
sums=0
true = 1
for i in range(n):
    mini,fin = s[i]
    if sums < mini:
        true = 0
        break
    sums += fin
if true == 1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:

n = int(input())
s = [list(input()) for i in range(n)]

l = []

for i in s:
    now = 0
    c = 0
    for j in i:
        if j =="(":
            now += 1
        else:
            now -= 1
        c = min(c,now)
    l.append([c,now])

l.sort(reverse =True)
ans = 0
for i,j in l:
    if ans + i < 0:
        print("No")
        exit()
    else:
        ans += j
if ans == 0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [[0,0]]
import numpy as np
def num(words,k):
    if words[k] =="(":
        return 1
    else:
        return -1
for i in range(n):
    a = input()
    mini = 0
    a_sum = 0
    for j in range(len(a)):
        a_sum += num(a,j)
        mini = min(mini,a_sum)
    s.append([mini,a_sum])
s.sort(key=lambda x:(x[0],x[1]),reverse = True)
sums=0
true = 1
for i in range(1,n):
    mini,fin = s[i]
    if sums < mini:
        true = 0
        break
    sums += fin
if true == 1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
def main():
    

    n = int(input())
    s = [list(input()) for i in range(n)]
    l = []
    r = []
    b = []

    for i in range(n):
        t = 0
        m = 0
        e = 0
        
        c = 0
        while c < len(s[i]) and s[i][c] == ")":
            c += 1
            t += 1
        d = len(s[i])-1
        while d >= c and s[i][d] == "(":
            d -= 1
            e += 1
        
        for j in range(c,d+1):
            if s[i][j] =="(":
                m += 1
            else:
                m -= 1
        
        if m >= 0:
            e += m
        else:
            t -= m
        if t== e ==0:
            continue
        elif t == 0:
            r.append(e)
        elif e == 0:
            l.append(t)
        else:
            b.append([t,e])
    
    if len(b) == 0:
        if sum(l) == sum(r):
            print("Yes")
        else:
            print("No")
        exit()

    if len(l) == 0 or len(r) == 0:
        print("No")
        exit()

    b.sort()
    now = sum(r)
    for i,j in b:
        if now < i:
            print("No")
            exit()
        else:
            now += j-i
    if now == sum(l):
        print("Yes")
    else:
        print("No")


if __name__ =="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    s = [list(input()) for i in range(n)]
    l = []
    r = []
    b = []

    for i in range(n):
        t = 0
        m = 0
        e = 0
        
        c = 0
        while c < len(s[i]) and s[i][c] == ")":
            c += 1
            t += 1
        d = len(s[i])-1
        while d >= c and s[i][d] == "(":
            d -= 1
            e += 1
        
        for j in range(c,d+1):
            if s[i][j] =="(":
                m += 1
            else:
                m -= 1
        
        if m >= 0:
            e += m
        else:
            t -= m
        if t== e ==0:
            continue
        elif t == 0:
            r.append(e)
        elif e == 0:
            l.append(t)
        else:
            b.append([t,e])

    if len(b) == 0:
        if sum(l) == sum(r):
            print("Yes")
        else:
            print("No")
        exit()

    if len(l) == 0 or len(r) == 0:
        print("No")
        exit()

    b.sort()
    now = sum(r)
    for i,j in b:
        if now < i:
            print("No")
            exit()
        else:
            now += j-i
    if now == sum(l):
        print("Yes")
    else:
        print("No")


if __name__ =="__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [list(input()) for i in range(n)]
l = []
r = []
b = []

for i in range(n):
    t = 0
    m = 0
    e = 0
    
    c = 0
    while c < len(s[i]) and s[i][c] == ")":
        c += 1
        t += 1
    d = len(s[i])-1
    while d >= c and s[i][d] == "(":
        d -= 1
        e += 1
    
    for j in range(c,d+1):
        if s[i][j] =="(":
            m += 1
        else:
            m -= 1
    
    if m >= 0:
        e += m
    else:
        t -= m
    if t== e ==0:
        continue
    elif t == 0:
        r.append(e)
    elif e == 0:
        l.append(t)
    else:
        b.append([t,e])

if len(b) == 0:
    if sum(l) == sum(r):
        print("Yes")
    else:
        print("No")
    exit()

if len(l) == 0 or len(r) == 0:
    print("No")
    exit()

b.sort()
now = sum(r)
for i,j in b:
    if now < i:
        print("No")
        exit()
    else:
        now += j-i
if now == sum(l):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    plus, minus = [], []
    readline = stdin.readline
    n = int(readline())
    for _ in range(n):
        s = readline().strip()
        lens = len(s)
        lcnt = s.count('(')
        if 2 * lcnt - lens > 0:
            plus.insert(-1, [s, lcnt])
        else:
            minus.insert(-1, [s, lens-lcnt])
    plus.sort(key = lambda x: x[1])
    minus.sort(key = lambda x: x[1], reverse = True)

    res = 0
    for v in ''.join(map(lambda x:x[0], plus + minus)):
        res = res + (1 if v == '(' else -1)
        if res < 0 : return print('No')
    if res != 0:
        return print('No')

    return print('Yes')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
L = []
R = []
for _ in range(n):
  s = input()
  btm = 0
  total = 0
  for c in s:
    if c == "(":
      total += 1
    else:
      total -= 1
    btm = min(btm, total)
  if total > 0:
    L.append((btm, total))
  else:
    R.append((btm, total))
    
L.sort(key=lambda x: (-x[0], -x[1]))
R.sort(key=lambda x: (-x[1], x[0]))
LR = L + R

crr_end = 0
for b, e in LR:
  if crr_end + b < 0:
    print("No")
    exit()
  crr_end += e

if crr_end == 0:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
n = int(input())
L = []
R = []
for _ in range(n):
  s = input()
  btm = 0
  total = 0
  for c in s:
    if c == "(":
      total += 1
    else:
      total -= 1
    btm = min(btm, total)
  if total > 0:
    L.append((btm, total))
  else:
    R.append((btm, total))
    
L.sort(key=lambda x: (-x[0], -x[1]))
R.sort(key=lambda x: (x[0], -x[1]))
LR = L + R

crr_end = 0
for b, e in LR:
  if crr_end + b < 0:
    print("No")
    exit()
  crr_end += e

if crr_end == 0:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
n = int(input())
L = []
R = []
for _ in range(n):
  s = input()
  btm = 0
  total = 0
  for c in s:
    if c == "(":
      total += 1
    else:
      total -= 1
    btm = min(btm, total)
  if total > 0:
    L.append((btm, total))
  else:
    R.append((btm, total))
    
L.sort(key=lambda x: -x[0])
R.sort(key=lambda x: (x[0], -x[1]))
LR = L + R

crr_end = 0
for b, e in LR:
  if crr_end + b < 0:
    print("No")
    exit()
  crr_end += e

if crr_end == 0:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
n = int(input())
L = []
R = []
for _ in range(n):
  s = input()
  btm = 0
  total = 0
  for c in s:
    if c == "(":
      total += 1
    else:
      total -= 1
    btm = min(btm, total)
  if total > 0:
    L.append((btm, total))
  else:
    R.append((btm, total))
    
L.sort(key=lambda x: -x[0])
R.sort(key=lambda x: x[0])
LR = L + R

crr_end = 0
for b, e in LR:
  if crr_end + b < 0:
    print("No")
    exit()
  crr_end += e

if crr_end == 0:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
cnt = 0
decrement = 0
profit = []
loss = []

for _ in range(n):
    s = input()
    min_p = 0
    end_p = 0
    point = 0
    for c in s:
        if c == '(':
            point += 1
        elif c == ')':
            point -= 1
            if point < min_p:
                min_p = point
    else:
        end_p = point
    if 0 <= min_p:
        cnt += end_p
    else:
        if min_p == end_p:
            decrement += end_p
        else:
            if 0 <= end_p:
                profit.append((min_p, -end_p))
            else:
                loss.append((min_p, -end_p))


profit.sort(reverse=True)
while profit:
    min_p, end_p = profit.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt -= end_p


loss.sort()
while loss:
    min_p, end_p = loss.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt -= end_p

print('Yes' if cnt+decrement == 0 else 'No')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
cnt = 0
decrement = 0
profit = []
loss = []

for _ in range(n):
    s = input()
    min_p = 0
    end_p = 0
    point = 0
    for c in s:
        if c == '(':
            point += 1
        elif c == ')':
            point -= 1
            if point < min_p:
                min_p = point
    else:
        end_p = point
    if 0 <= min_p:
        cnt += end_p
    else:
        if min_p == end_p:
            decrement += end_p
        else:
            if 0 <= end_p:
                profit.append((min_p, end_p))
            else:
                loss.append((min_p, end_p))


profit.sort(reverse=True)
while profit:
    min_p, end_p = profit.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt += end_p


loss.sort()
while loss:
    min_p, end_p = loss.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt += end_p

print('Yes' if cnt+decrement == 0 else 'No')

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    readline = stdin.readline
    n = int(readline())
    s = tuple(readline().strip() for _ in range(n))

    plus, minus = [], []
    for c in s:
        if 2*c.count('(') - len(c) > 0:
            plus.append(c)
        else:
            minus.append(c)

    plus.sort(key = lambda x: x.count(')'), reverse = True)
    minus.sort(key = lambda x: x.count('('))
    plus.extend(minus)
    sum = 0
    for v in plus:
        for vv in v:
            sum = sum + (1 if vv == '(' else -1)
            if sum < 0 : return print('No')
    if sum != 0:
        return print('No')

    return print('Yes')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    readline = stdin.readline
    n = int(readline())
    s = tuple(readline().strip() for _ in range(n))

    plus, minus = [], []
    for c in s:
        if 2*c.count('(') - len(c) > 0:
            plus.append(c)
        else:
            minus.append(c)

    plus.sort(key = lambda x: x.count(')'), reverse = True)
    minus.sort(key = lambda x: x.count(')'))
    plus.extend(minus)
    sum = 0
    for v in plus:
        for vv in v:
            sum = sum + (1 if vv == '(' else -1)
            if sum < 0 : return print('No')
    if sum != 0:
        return print('No')

    return print('Yes')

if __name__ == '__main__':
    main()

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 and stack[-1] == ')':
                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')