Problem: A **bracket sequence** is a string that is one of the following:    1. An empty string;   2. The concatenation of `(`, A, and `)` in this order, for some bracket sequence A ;   3. The concatenation of A and B in this order, for some non-empty bracket sequences A and B /  Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Submission attempts (in order):
import sys
def input():
    return sys.stdin.readline().strip()

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

tmp = []
for i,s in enumerate(S):
    tmp.append([s.count("(")-s.count(")"),i])

tmp.sort(reverse=True)

a = 0
b = 0
s = ""
for _,i in tmp:
    s += S[i]
print(s)
for i in s:
    if i == "(":
        a += 1
    else:
        b += 1
    if b > a:
        print("No")
        exit()

if a-b == 0:
    print("Yes")
else:
    print("No")

import sys
from collections import deque
def input():
    return sys.stdin.readline().strip()

def check():
    s = input()
    m = 0
    res = deque([])
    for i in s:
        if i == "(":
            m += 1 
        else:
            m -= 1
            res.append(m)
    ##print(res)
    try:
        a = min(res)
    except:
        a = m
    return [a,m]

N = int(input())
S = [check() for _ in range(N)]
print(S)
S.sort(reverse=True)

a = 0
b = 0

for res,m in S:
    a += res
    b += m
    if a < 0:
        print("No")
        exit()

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

from collections import deque

N = int(input())
S = deque([input() for _ in range(N)])

tmp = []
for i,s in enumerate(S):
    tmp.append([s.count("(")-s.count(")"),i])

tmp.sort(reverse=True)

a = 0
b = 0
s = ""
for _,i in tmp:
    s += S[i]
print(s)
for i in range(len(s)):
    if s[i] == "(":
        a += 1
    else:
        b += 1
    if b > a:
        print("No")
        exit()

if a-b == 0:
    print("Yes")
else:
    print("No")
    

import sys
def input():
    return sys.stdin.readline().strip()

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

tmp = []
for i,s in enumerate(S):
    tmp.append([s.count("(")-s.count(")"),i])

tmp.sort(reverse=True)

a = 0
b = 0
s = ""
for _,i in tmp:
    s += S[i]
print(s)
for i in s:
    if i == "(":
        a += 1
    else:
        b += 1
    if b > a:
        print("No")
        exit()

if a-b == 0:
    print("Yes")
else:
    print("No")

import sys
from collections import deque
def input():
    return sys.stdin.readline().strip()

def check():
    s = input()
    m = 0
    res = 0
    for i in s:
        if i == "(":
            m += 1 
        else:
            m -= 1
            res = min(res,m)
    return [res,m]

N = int(input())
S = [check() for _ in range(N)]

S.sort(reverse=True)
print(S)

a = 0
b = 0

for res,m in S:
    a = b + res
    b += m
    if a < 0:
        print("No")
        exit()

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

import sys
from collections import deque
def input():
    return sys.stdin.readline().strip()

tozan = []
gezan = []

def check():
    s = input()
    m = 0
    res = 0
    for i in s:
        if i == "(":
            m += 1 
        else:
            m -= 1
            res = min(res,m)
    if m >= 0:
        tozan.append([res,m])
    else:
        gezan.append([res-m,res,m])

N = int(input())
for _ in range(N):
    check()

tozan.sort(reverse=True)
gezan.sort()

b = 0

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

for _,res,m in gezan:
    if b + res < 0:
        print("No")
        exit()
    b += m

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

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted