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 itertools

n = int(input())

s = []
for i in range(n):
    s.append(input())

comb_s = list(itertools.combinations(s, len(s)))

def checker(x):
    jud = 1
    ans = ''.join(x)

    if not s:
        return jud

    print(ans)
    r = x.count('(')
    l = x.count(')')
    if r != l:
        jud =0
        return jud

    if x[0] != '(' or x[-1] != ')':
        jud =0

    return jud

def main():
    for i in range(len(comb_s)):
        print(comb_s[i])
        juds = checker(comb_s[i])
        if juds == 1:
            print('Yes')
            return

    print('No')
    return
main()


from sys import stdin

def checker(x):
    l = x.count('(', 1, -1)
    r = len(x[1:-1]) - l
    if l != r: return 0

    sum = 0
    for c in x:
        print(c)
        if c == '(':
            sum = sum + 1
        else:
            sum = sum - 1
        if sum < 0:
            return 0
    if sum != 0 : return 0
    return 1

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

    sorted_s = sorted(s)
    jud = checker(''.join(sorted_s))

    if jud == 1:
        print('Yes')
    else:
        print('No')
    return

if __name__ == '__main__':
    main()


import itertools

n = int(input())

s = []
for i in range(n):
    s.append(input())

comb_s = list(itertools.combinations(s, len(s)))

def checker(x):
    jud = 1
    ans = ''.join(x)

    if not s:
        return jud

    print(ans)
    r = x.count('(')
    l = x.count(')')
    if r != l:
        jud =0
        return jud

    if x[0] != '(' or x[-1] != ')':
        jud =0

    return jud

def main():
    for i in range(len(comb_s)):
        print(comb_s[i])
        juds = checker(''.join(for j in comb_s[i]))
        if juds == 1:
            print('Yes')
            return

    print('No')
    return
main()


import itertools

n = int(input())

s = []
for i in range(n):
    s.append(input())

comb_s = list(itertools.combinations(s, len(s)))

def checker(x):
    jud = 1
    ans = ''.join(x)

    if not s:
        return jud

    print(ans)
    r = x.count('(')
    l = x.count(')')
    if r != l:
        jud =0
        return jud

    if x[0] != '(' or x[-1] != ')':
        jud =0

    return jud

def main():
    for i in range(len(comb_s)):
        print(comb_s[i])
        juds = checker(''.join( [j for j in comb_s[i]]))
        if juds == 1:
            print('Yes')
            return

    print('No')
    return
main()


import sys
from itertools import permutations
readline = sys.stdin.readline

n = int(readline())
s = [readline().strip() for i in range(n)]

comb_s = list(permutations(s, n))

def checker(x):
    if not x: return 0

    if x[0] != '(' or x[-1] != ')': return 0

    r = x.count('(')
    l = x.count(')')
    if r != l: return 0

    return 1

def main():
    for i in range(len(comb_s)):
        print(''.join( [j for j in comb_s[i]]))
        jud = checker(''.join( [j for j in comb_s[i]]))
        if jud == 1:
            print('Yes')
            return

    print('No')
    return

main()


import sys
from itertools import permutations
readline = sys.stdin.readline

n = int(readline())
s = [readline().strip() for i in range(n)]

comb_s = list(permutations(s, n))

def checker(x):
    print(x)
    if not x: return 0

    if x[0] != '(' or x[-1] != ')': return 0

    r = x.count('(')
    l = x.count(')')
    if r != l: return 0

    return 1

def main():
    for i in range(len(comb_s)):
        jud = checker(''.join(comb_s[i]))
        if jud == 1:
            print('Yes')
            return

    print('No')
    return

main()


from sys import stdin

def checker(x):
    l = x.count('(', 1, -1)
    r = len(x[1:-1]) - l
    if l != r: return 0

    sum = 0
    for c in x:
        print(c)
        if c == '(':
            sum = sum + 1
        else:
            sum = sum - 1
        if sum < 0:
            return 0
    return 1

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

    sorted_s = sorted(s)
    jud = checker(''.join(sorted_s))

    if jud == 1:
        print('Yes')
    else:
        print('No')
    return

if __name__ == '__main__':
    main()


from sys import stdin

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

    sum = 0
    for v in ''.join(plus + minus):
        sum = sum + (1 if v == '(' else -1)
        if sum < 0 : return print('No')
    if sum != 0:
        return print('No')

    return print('Yes')

if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted