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):
# 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 numpy as np

def run():
    N = int(input())
    current = 0
    ways = []
    dic = {'(': 1, ')': -1}
    for _ in range(N):
        S = input()
        go = 0
        max_depth = 0
        for s in S:
            go += dic[s]
            max_depth = min(max_depth, go)
        if current + max_depth < 0:
            ways.append((go, max_depth))
        else:
            current += go

    ways = sorted(ways, key=lambda x:x[0], reverse=True)
    while ways:
        for idx, (go, max_depth) in enumerate(ways):
            if current + max_depth >= 0:
                current += go
                del ways[idx]
                continue
        print('No')
        return None
    if current == 0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    run()

# 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 numpy as np
def run():
    N = int(input())
    ret = 0
    for _ in range(N):
        S = sysread()
        for s in S:
            ret += dic[s]
    if ret == 0:
        print('Yes')
    else:
        print('No')
if __name__ == "__main__":
    run()

# 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 numpy as np

def run():
    N = int(input())
    current = 0
    ways = []
    dic = {'(': 1, ')': -1}
    for _ in range(N):
        S = input()
        go = 0
        max_depth = 0
        for s in S:
            go += dic[s]
            max_depth = min(max_depth, go)
        if current + max_depth < 0:
            ways.append((go, max_depth))
        else:
            current += go

    ways = sorted(ways, key=lambda x:x[0], reverse=True)

    while ways:
        done = False
        for idx, (go, max_depth) in enumerate(ways):
            if current + max_depth >= 0:
                done = True
                if go < 0:
                    break
                current += go
                del ways[idx]
                break
        if not done:
            break

    ways = sorted(ways, key = lambda x:x[1])

    while ways and current >= 0:
        done = False
        for idx, (go, max_depth) in enumerate(ways):
            if current + max_depth >= 0:
                current += go
                del ways[idx]
                done = True
                break
        if not done:
            print('No')
            return None


    if current == 0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    run()

# 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 numpy as np

def run():
    N = int(input())
    current = 0
    ways = []
    dic = {'(': 1, ')': -1}
    for _ in range(N):
        S = input()
        go = 0
        max_depth = 0
        for s in S:
            go += dic[s]
            max_depth = min(max_depth, go)
        if current + max_depth < 0:
            ways.append((go, max_depth))
        else:
            current += go


    ways = sorted(ways, key=lambda x:x[0], reverse=True)

    while ways:
        done = False
        for idx, (go, max_depth) in enumerate(ways):
            if current + max_depth >= 0:
                if go < 0:
                    break
                current += go
                del ways[idx]
                done = True
        if not done:
            print('No')
            return None

    ways = [(-a, b) for a, b in ways]
    ways = sorted(ways, key = itemgetter(1,0))

    while ways and current >= 0:
        done = False
        for idx, (go, max_depth) in enumerate(ways):
            if current + max_depth >= 0:
                current -= go
                del ways[idx]
                done = True
                break
        if not done:
            print('No')
            return None


    if current == 0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    run()

# 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 numpy as np

def run():
    N = int(input())
    current = 0
    ways = []
    dic = {'(': 1, ')': -1}
    for _ in range(N):
        S = input()
        go = 0
        max_depth = 0
        for s in S:
            go += dic[s]
            max_depth = min(max_depth, go)
        if current + max_depth < 0:
            ways.append((go, max_depth))
        else:
            current += go

    ways = sorted(ways, key=lambda x:x[0], reverse=True)

    cont = True
    while cont and ways:
        done = False
        for idx, (go, max_depth) in enumerate(ways):
            if current + max_depth >= 0:
                if go < 0:
                    cont = False
                    break
                current += go
                del ways[idx]
                done = True
                break
        if not done:
            print('No')
            return None

    ways = sorted(ways, key = lambda x:x[1])
    while ways and current >= 0:
        done = False
        for idx, (go, max_depth) in enumerate(ways):
            if current + max_depth >= 0:
                current += go
                del ways[idx]
                done = True
                break
        if not done:
            print('No')
            return None


    if current == 0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    run()

# 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 numpy as np

def run():
    N = int(input())
    current = 0
    ways = []
    dic = {'(': 1, ')': -1}
    SS = read().split()

    for S in SS:
        path = [0]
        for s in S:
            path.append(path[-1]+ dic[s])
        ways.append((path[-1], min(path)))

    ways_pos = sorted([(a,b) for a,b in ways if a >= 0], key = lambda x:(x[1], x[0]), reverse=True)
    ways_neg = sorted([(a,b) for a,b in ways if a < 0], key = lambda x:(x[1] - x[0], -x[0]))

    for i in range(len(ways_pos)):
        go, max_depth = ways_pos[i]
        if current + max_depth >= 0:
            current += go
        else:
            print("No")
            return None

    for i in range(len(ways_neg)):
        go, max_depth = ways_neg[i]
        if current + max_depth >= 0:
            current += go
        else:
            print("No")
            return None

    if current == 0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    run()

Verdicts: Wrong Answer

Runtime Error

Time Limit Exceeded

Wrong Answer

Wrong Answer

Accepted