AtCoder Beginner Contest 167 - Bracket Sequencing

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-----
2
)
(()

-----Sample Output-----
Yes

Concatenating (() and ) in this order forms a bracket sequence.

Difficulty: MEDIUM

Starter code:

Solutions:
n = int(input())
s = []
for i in range(n):
	s.append(input())
ls1 = []
ls2 = []
for item in s:
	stk1 = []
	stk2 = []
	for ch in item:
		if ch == '(':
			stk2.append(ch)
		elif len(stk2) > 0:
			stk2.pop()
		else:
			stk1.append(ch)
	l1 = len(stk1)
	l2 = len(stk2)
	if l2 >= l1:
		ls1.append((l1, l2))
	else:
		ls2.append((l1, l2))
ls1.sort()
cnt = 0
for item in ls1:
	cnt -= item[0]
	if cnt < 0:
		print('No')
		return
	else:
		cnt += item[1]
ls2.sort(key=lambda x: x[1])
ls2.reverse()
for item in ls2:
	cnt -= item[0]
	if cnt < 0:
		print('No')
		return
	else:
		cnt += item[1]
if cnt != 0:
	print('No')
else:
	print('Yes')


n = int(input())
s = []
for i in range(n):
	a = input()
	b = []
	for j in range(len(a)):
		if a[j] == '(':
			b.append(1)
		else:
			b.append(-1)
	s.append(b)
l = []
r = []
for x in s:
	b = 0
	c = 0
	for y in x:
		c += y
		b = min(b, c)
	z = len(x)
	d = 0
	e = 0
	for j in range(z):
		e += x[z - j - 1]
		d = max(d, e)
	if c > 0:
		l.append([b, c])
	else:
		r.append([d, c])
l.sort(reverse=True)
r.sort()
flag = 1
x = 0
for (b, c) in l:
	if x + b < 0:
		flag = 0
	x += c
y = 0
for (d, c) in r:
	if y + d > 0:
		flag = 0
	y += c
if y + x != 0:
	flag = 0
print('Yes' if flag else 'No')


n = int(input())
pdat = []
mdat = []
for i in range(n):
	s = input()
	mini = 0
	now = 0
	for q in range(len(s)):
		if s[q] == '(':
			now += 1
		else:
			now -= 1
			mini = min(mini, now)
	if now >= 0:
		pdat.append((now, mini, mini - now))
	else:
		mdat.append((now, mini, mini - now))
pdat.sort(key=lambda x: -x[1])
mdat.sort(key=lambda x: x[2])
now = 0
for d in pdat + mdat:
	if now + d[1] < 0:
		print('No')
		break
	now += d[0]
else:
	if now == 0:
		print('Yes')
	else:
		print('No')


N = int(input())
s = []
for i in range(N):
	s.append(input())
inc = []
dec = []
total = 0
for i in s:
	cn = 0
	mn = 0
	for j in i:
		if j == '(':
			cn += 1
		else:
			cn -= 1
		mn = min(cn, mn)
	total += cn
	if cn >= 0:
		inc.append([mn, cn])
	else:
		dec.append([mn - cn, -cn])
inc.sort(reverse=True, key=lambda x: x[0])
dec.sort(reverse=True, key=lambda x: x[0])

def check(lis):
	hi = 0
	for i in lis:
		if hi + i[0] < 0:
			return False
		else:
			hi += i[1]
	return True
if check(inc) and check(dec) and (total == 0):
	print('Yes')
else:
	print('No')


import sys
input = sys.stdin.readline
n = int(input())
plus = []
minus = []
for _ in range(n):
	min_s = 0
	ts = 0
	s = input().strip()
	for c in s:
		ts += 1 if c == '(' else -1
		min_s = min(ts, min_s)
	if ts >= 0:
		plus.append((-min_s, ts))
	else:
		minus.append((-(min_s - ts), -ts))
plus.sort()
minus.sort()
s1 = 0
for (m, t) in plus:
	if s1 - m < 0:
		print('No')
		exit()
	s1 += t
s2 = 0
for (m, t) in minus:
	if s2 - m < 0:
		print('No')
		exit()
	s2 += t
if s1 == s2:
	print('Yes')
else:
	print('No')


from operator import itemgetter
from itertools import chain
N = int(input())
inc = []
dec = []
for i in range(N):
	S = input()
	low = 0
	pos = 0
	for s in S:
		if s == '(':
			pos += 1
		else:
			pos -= 1
		low = min(low, pos)
	if pos >= 0:
		inc.append((low, pos))
	else:
		dec.append((low, pos))
inc.sort(key=itemgetter(0), reverse=True)
dec.sort(key=lambda x: x[0] - x[1])
height = 0
for (i, (low, pos)) in enumerate(chain(inc, dec)):
	if height + low < 0:
		ans = 'No'
		break
	height += pos
else:
	ans = 'Yes' if height == 0 else 'No'
print(ans)


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 + diff < 0:
		print('No')
		exit()
	current += diff
if current != 0:
	print('No')
else:
	print('Yes')


def main():
	n = int(input())
	up = []
	down = []
	for _ in range(n):
		height = 0
		m = 0
		for si in input():
			if si == '(':
				height += 1
			else:
				height -= 1
				m = min(m, height)
		if height >= 0:
			up.append([m, height])
		else:
			down.append([m - height, -height])
	up.sort(reverse=True)
	down.sort(reverse=True)
	if sum([i for (_, i) in up]) - sum([i for (_, i) in down]) != 0:
		print('No')
		return
	h = 0
	for (mi, hi) in up:
		if h + mi < 0:
			print('No')
			return
		h += hi
	h = 0
	for (mi, hi) in down:
		if h + mi < 0:
			print('No')
			return
		h += hi
	print('Yes')
	return
main()


def check():
	N = int(input())
	(plus, minus) = ([], [])
	for i in range(N):
		S = input()
		now = 0
		mini = 0
		for s in S:
			if s == '(':
				now += 1
			else:
				now -= 1
				mini = min(mini, now)
		if now >= 0:
			plus.append([mini, now])
		else:
			minus.append([now - mini, now])
	plus.sort(reverse=True)
	minus.sort(reverse=True)
	now = 0
	for (a, b) in plus:
		if now + a < 0:
			return 'No'
		now += b
	for (a, b) in minus:
		if now + b - a < 0:
			return 'No'
		now += b
	if now > 0:
		return 'No'
	return 'Yes'
print(check())


N = int(input())
S = [input() for i in range(N)]
xs1total = 0
xs2 = []
xs3 = []
xs4total = 0
for s in S:
	n = 0
	m = 0
	for c in s:
		if c == '(':
			n += 1
		elif c == ')':
			n -= 1
			m = min(m, n)
	k = n - m
	if m == 0 and k == 0:
		continue
	elif m == 0:
		xs1total += k
	elif k == 0:
		xs4total += -m
	elif -m < k:
		xs2.append((-m, k))
	else:
		xs3.append((-m, k))
xs2.sort()
xs3.sort(key=lambda x: -x[1])

def check(xs1total, xs2, xs3, xs4total):
	t = xs1total
	for x in xs2:
		if t < x[0]:
			return False
		t = t - x[0] + x[1]
	for x in xs3:
		if t < x[0]:
			return False
		t = t - x[0] + x[1]
	return t == xs4total
if check(xs1total, xs2, xs3, xs4total):
	print('Yes')
else:
	print('No')


N = int(input())

def check(A):
	th = 0
	for i in range(len(A)):
		b = A[i][0]
		h = A[i][1]
		if th + b < 0:
			return False
		else:
			th += h
	return True
L = []
R = []
goukei = 0
for i in range(N):
	temp = str(input())
	b = 0
	h = 0
	for j in range(len(temp)):
		if temp[j] == '(':
			h += 1
		else:
			h -= 1
		b = min(b, h)
	if h > 0:
		L.append([b, h])
	else:
		R.append([b - h, -h])
	goukei += h
L.sort(reverse=True)
R.sort(reverse=True)
if check(L) and check(R) and (goukei == 0):
	print('Yes')
else:
	print('No')


import sys
input = sys.stdin.readline
n = int(input())
br = [input().rstrip() for i in range(n)]
ls = []
numsum = 0
charge = 0
for i in range(n):
	s = br[i]
	need = 0
	num = 0
	for t in s:
		if t == '(':
			num += 1
		else:
			num -= 1
		need = max(need, -num)
	numsum += num
	if need == 0:
		charge += num
	else:
		ls.append([need, num])
if numsum != 0:
	print('No')
	exit()
ls.sort()
l = len(ls)
vis = [0] * l
for (i, x) in enumerate(ls):
	(need, num) = x
	if need <= charge and num >= 0:
		charge += num
		ls[i][0] = -1
ls.sort(key=lambda x: x[0] + x[1], reverse=True)
for i in range(l):
	(need, num) = ls[i]
	if need == -1:
		continue
	if need > charge:
		print('No')
		exit()
	charge += num
print('Yes')


def check(DR):
	D = 0
	for dr in DR[::-1]:
		(dmin, d) = dr
		if D + dmin < 0:
			return False
		D += d
	return True

def main():
	N = int(input())
	S = []
	for i in range(N):
		S.append(input())
	DR1 = []
	DR2 = []
	total = 0
	for s in S:
		d = 0
		d_min = 0
		for lr in s:
			if lr == '(':
				d += 1
			else:
				d -= 1
				d_min = min(d, d_min)
		total += d
		if d > 0:
			DR1.append((d_min, d))
		else:
			DR2.append((d_min - d, -d))
	DR1.sort()
	DR2.sort()
	if check(DR1) and check(DR2) and (sum([d[1] for d in DR1]) == sum([d[1] for d in DR2])):
		print('Yes')
	else:
		print('No')
main()


import sys
n = int(input())
si = []
for _ in range(n):
	s = input()
	l = 0
	r = 0
	count = 0
	for i in range(len(s)):
		if s[i] == '(':
			count += 1
		else:
			count = max(0, count - 1)
	l = count
	count = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == ')':
			count += 1
		else:
			count = max(0, count - 1)
	r = count
	si.append((l, r))
minus = []
plus = []
while si:
	(a, b) = si.pop()
	if a <= b:
		plus.append((a, b))
	else:
		minus.append((b, a))
plus.sort()
minus.sort()
countl = 0
for (a, b) in plus:
	if countl >= a:
		countl += b - a
	else:
		print('No')
		sys.exit()
countr = 0
for (a, b) in minus:
	if countr >= a:
		countr += b - a
	else:
		print('No')
		sys.exit()
if countr == countl:
	print('Yes')
else:
	print('No')


(N,) = map(int, input().split())
import sys
input = sys.stdin.readline
X = []
Y = []
for _ in range(N):
	s = input().strip()
	(a, b) = (0, 0)
	for c in s:
		if c == '(':
			a += 1
		elif a > 0:
			a -= 1
		else:
			b += 1
	if a >= b:
		X.append((a, b))
	else:
		Y.append((b, a))
X.sort(key=lambda x: x[1])
Y.sort(key=lambda x: x[1])
s = 0
for i in range(len(X)):
	(a, b) = X[i]
	if s >= b:
		s += a - b
	else:
		print('No')
		sys.exit()
s2 = 0
for i in range(len(Y)):
	(a, b) = Y[i]
	if s2 >= b:
		s2 += a - b
	else:
		print('No')
		sys.exit()
if s == s2:
	print('Yes')
else:
	print('No')


from functools import reduce
(n, *s) = open(0).read().split()
u = []
for t in s:
	close_cnt = 0
	tmp = 0
	for c in t:
		tmp += 1 if c == '(' else -1
		close_cnt = min(close_cnt, tmp)
	u.append((close_cnt, tmp - close_cnt))
M = 10 ** 6 + 1
acc = 0
for (a, b) in sorted(u, key=lambda z: -M - z[0] if sum(z) >= 0 else M - z[1]):
	if acc + a < 0:
		print('No')
		return
	else:
		acc += a + b
print('No' if acc else 'Yes')


n = int(input())

def _kakko(s):
	(l, lr) = (0, 0)
	for c in s:
		if c == ')':
			if lr == 0:
				l = l + 1
			else:
				lr = lr - 1
		else:
			lr = lr + 1
	return (l, lr)
s = [_kakko(input()) for _ in range(n)]
sl = [ss for ss in s if ss[1] >= ss[0]]
sr = [ss for ss in s if ss[1] < ss[0]]
sl.sort(key=lambda x: x[0])
sr.sort(key=lambda x: x[1], reverse=True)
r = 0
for ss in sl:
	if ss[0] > r:
		print('No')
		return
	r = r + ss[1] - ss[0]
for ss in sr:
	if ss[0] > r:
		print('No')
		return
	r = r + ss[1] - ss[0]
if r != 0:
	print('No')
	return
print('Yes')


n = int(input())
S = [input() for _ in range(n)]
U = []
L = []
for s in S:
	(l, r) = (0, 0)
	for t in s:
		if t == '(':
			r += 1
		elif r > 0:
			r -= 1
		else:
			l += 1
	if l <= r:
		U.append((l, r - l))
	else:
		L.append((r, l - r))
U.sort()
L.sort()
(s, t) = (0, 0)
for (a, c) in U:
	if s < a:
		print('No')
		exit()
	s += c
for (a, c) in L:
	if t < a:
		print('No')
		exit()
	t += c
if s != t:
	print('No')
	exit()
print('Yes')


def main():
	import sys

	def input():
		return sys.stdin.readline().rstrip()
	n = int(input())
	lx = []
	rx = []
	for i in range(n):
		a = 0
		cnt = 0
		for j in input():
			if j == '(':
				cnt += 1
			else:
				cnt -= 1
			a = min(a, cnt)
		if cnt >= 0:
			lx.append((a, cnt))
		else:
			rx.append((a, cnt))
	lx.sort(key=lambda x: -x[0])
	rx.sort(key=lambda x: x[0] - x[1])
	total = 0
	check = True
	for (x, cnt) in lx + rx:
		if total + x < 0:
			check = False
			break
		total += cnt
	if total == 0 and check:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


import sys
readline = sys.stdin.readline

def solve():
	N = int(input())
	brackets_generator = (readline().strip() for _ in range(N))
	grads_positive = list()
	grads_negative = list()
	total = 0
	for brackets in brackets_generator:
		(elevation, bottom) = (0, 0)
		for bk in brackets:
			elevation += 1 if bk == '(' else -1
			bottom = min(bottom, elevation)
		if elevation >= 0:
			grads_positive.append((bottom, elevation))
		elif elevation < 0:
			grads_negative.append((bottom - elevation, -elevation))
		total += elevation
	if total != 0:
		return False
	grads_positive.sort(reverse=True)
	grads_negative.sort(reverse=True)

	def is_good(grads):
		(elevation, bottom) = (0, 0)
		for grad in grads:
			bottom = elevation + grad[0]
			if bottom < 0:
				return False
			elevation += grad[1]
		return True
	return is_good(grads_positive) and is_good(grads_negative)

def main():
	ok = solve()
	if ok:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


n = int(input())
s = [input() for i in range(n)]
(l, r) = ([], [])
for i in s:
	(a, b) = (0, 0)
	for j in range(len(i)):
		if i[j] == '(':
			b += 1
		if i[j] == ')':
			if b <= 0:
				a += 1
			else:
				b -= 1
	if b - a > 0:
		l.append([a, b])
	else:
		r.append([a, b])
l.sort(key=lambda x: x[0])
r.sort(key=lambda x: x[1], reverse=True)
li = l + r
ans = 0
for i in li:
	ans -= i[0]
	if ans < 0:
		exit(print('No'))
	ans += i[1]
print('Yes' if ans == 0 else 'No')


import sys

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

def solve():
	N = int(input())
	inc_chunks = []
	dec_chunks = []
	for _ in range(N):
		depth = 0
		level = 0
		for c in input():
			if c == '(':
				level += 1
			else:
				level -= 1
				depth = min(depth, level)
		if level > 0:
			inc_chunks.append((depth, level))
		else:
			dec_chunks.append((depth - level, -level))
	inc_chunks.sort(reverse=True)
	dec_chunks.sort(reverse=True)
	inc_level = 0
	for (depth, level) in inc_chunks:
		if inc_level + depth < 0:
			return 'No'
		inc_level += level
	dec_level = 0
	for (depth, level) in dec_chunks:
		if dec_level + depth < 0:
			return 'No'
		dec_level += level
	if inc_level != dec_level:
		return 'No'
	return 'Yes'
print(solve())


N = int(input())
from collections import deque
L = deque([])
R = deque([])
for i in range(N):
	S = input()
	l = [0] * len(S)
	if S[0] == '(':
		l[0] = 1
	else:
		l[0] = -1
	for j in range(1, len(S)):
		if S[j] == '(':
			l[j] = l[j - 1] + 1
		else:
			l[j] = l[j - 1] - 1
	if l[-1] < 0:
		R.append([min(l) - l[-1], -l[-1], min(l), l[-1]])
	else:
		L.append([min(0, min(l)), l[-1]])
R = sorted(R, key=lambda x: x[0])
L = sorted(L, key=lambda x: x[0], reverse=True)
now = 0
judge = True
for l in L:
	if now + l[0] < 0:
		judge = False
		break
	else:
		now += l[1]
for r in R:
	if now + r[2] < 0:
		judge = False
		break
	else:
		now += r[3]
if now != 0:
	judge = False
if judge:
	print('Yes')
else:
	print('No')


def main():
	n = int(input())
	Ss = [input() for _ in range(n)]
	U = [[0, 0] for _ in range(n)]
	for i in range(n):
		for c in Ss[i]:
			if c == '(':
				U[i][1] += 1
			elif U[i][1] == 0:
				U[i][0] += 1
			else:
				U[i][1] -= 1
	(L, R) = (0, 0)
	P = []
	for i in range(n):
		if U[i][0] == 0 and U[i][1] > 0:
			L += U[i][1]
		elif U[i][0] > 0 and U[i][1] == 0:
			R += U[i][0]
		elif U[i][0] > 0 and U[i][1] > 0:
			P.append([U[i][0], U[i][1]])
	P.sort(key=lambda x: (x[0] - x[1], x[0], -x[1]))
	if L == 0 and R == 0 and (len(P) == 0):
		print('Yes')
	elif (L == 0 or R == 0) and len(P) > 0:
		print('No')
	else:
		f = True
		for i in range(len(P)):
			L -= P[i][0]
			if L < 0:
				f = False
				break
			L += P[i][1]
		if L == R and f:
			print('Yes')
		else:
			print('No')

def __starting_point():
	main()
__starting_point()


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(')'))
	minus.sort(key=lambda x: x.count('('), reverse=True)
	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')
main()


from itertools import *
(n, *s) = open(0).read().split()
u = [[min(accumulate(t, lambda a, b: a + (1 if b == '(' else -1), initial=0)), 2 * t.count('(') - len(t)] for t in s]
m = 0
for (c, d) in chain(sorted([x for x in u if x[1] >= 0])[::-1], 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')


def scan(s):
	m = 0
	a = 0
	for c in s:
		if c == '(':
			a += 1
		elif c == ')':
			a -= 1
		m = min(m, a)
	return (m, a)

def key(v):
	(m, a) = v
	if a >= 0:
		return (1, m, a)
	else:
		return (-1, a - m, a)
N = int(input())
S = [input() for _ in range(N)]
c = 0
for (m, a) in sorted([scan(s) for s in S], reverse=True, key=key):
	if c + m < 0:
		c += m
		break
	c += a
if c == 0:
	print('Yes')
else:
	print('No')


import sys
input = sys.stdin.readline
N = int(input())
S = [input().rstrip() for _ in range(N)]
A = []
B = []
for s in S:
	a = 0
	k = 0
	for i in range(len(s)):
		if s[i] == ')':
			k += 1
		else:
			k -= 1
		a = max(a, k)
	b = 0
	k = 0
	for i in range(len(s))[::-1]:
		if s[i] == '(':
			k += 1
		else:
			k -= 1
		b = max(b, k)
	A.append(a)
	B.append(b)
L = []
R = []
for i in range(N):
	if B[i] - A[i] >= 0:
		L.append((A[i], i))
	else:
		R.append((B[i], i))
L.sort()
R.sort(reverse=True)
T = []
for (a, i) in L:
	T.append(S[i])
for (b, i) in R:
	T.append(S[i])
T = ''.join(T)
k = 0
for t in T:
	if t == '(':
		k += 1
	else:
		k -= 1
	if k < 0:
		print('No')
		break
else:
	if k == 0:
		print('Yes')
	else:
		print('No')


N = int(input())
S = [input() for i in range(N)]
pos = []
neg = []
for s in S:
	sm = 0
	mn = 0
	for c in s:
		if c == '(':
			sm += 1
		else:
			sm -= 1
			mn = min(mn, sm)
	if sm >= 0:
		pos.append((mn, sm))
	else:
		neg.append((mn - sm, -sm))
pos.sort(key=lambda x: -x[0])
neg.sort(key=lambda x: -x[0])
hp = 0
for (m, s) in pos:
	if hp + m < 0:
		print('No')
		return
	hp += s
hn = 0
for (m, s) in neg:
	if hn + m < 0:
		print('No')
		return
	hn += s
print('Yes' if hp == hn else 'No')


def to_lines(s):
	h = 0
	b = 0
	for c in s:
		if c == '(':
			h += 1
		else:
			h -= 1
		b = min([b, h])
	return (b, h)

def check(lines):
	h = 0
	for line in lines:
		if h + line[0] < 0:
			return False
		h += line[1]
	return True

def main():
	N = int(input())
	up_lines = list()
	down_lines = list()
	total = 0
	for _ in range(N):
		line = to_lines(input())
		total += line[1]
		if line[1] >= 0:
			up_lines.append(line)
		else:
			inv_line = (line[0] - line[1], -line[1])
			down_lines.append(inv_line)
	up_lines.sort(key=lambda x: x[0], reverse=True)
	down_lines.sort(key=lambda x: x[0], reverse=True)
	if total == 0 and check(up_lines) and check(down_lines):
		print('Yes')
	else:
		print('No')
main()


N = int(input())
if N == 0:
	print('Yes')
	exit()
P = []
M = []
for i in range(N):
	s = input()
	f = 0
	m = 0
	cnt = 0
	for j in range(len(s)):
		if s[j] == '(':
			cnt += 1
		else:
			cnt -= 1
			if cnt < m:
				m = cnt
	if cnt >= 0:
		P.append([m, cnt])
	else:
		M.append([m - cnt, -cnt])
P.sort(reverse=True)
M.sort(reverse=True)
SUM = 0
for (i, j) in P:
	SUM += j
for (i, j) in M:
	SUM -= j
if SUM != 0:
	print('No')
	exit()
SUMP = 0
for (i, j) in P:
	if SUMP >= -i:
		SUMP += j
	else:
		print('No')
		exit()
SUMM = 0
for (i, j) in M:
	if SUMM >= -i:
		SUMM += j
	else:
		print('No')
		exit()
print('Yes')


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, -m])
N = int(input())
for _ in range(N):
	check()
tozan.sort(reverse=True)
gezan.sort(reverse=True)
t = 0
g = 0
for (res, m) in tozan:
	if t + res < 0:
		print('No')
		exit()
	t += m
for (res, m) in gezan:
	if g + res < 0:
		print('No')
		exit()
	g += m
if t == g:
	print('Yes')
else:
	print('No')


import sys
input = sys.stdin.readline
N = int(input())
(D, E) = ([], [])
(t, l) = (0, 0)
res = 0
for _ in range(N):
	S = input().rstrip()
	(x, y) = (0, 0)
	for s in S:
		if s == '(':
			x += 1
		else:
			x = max(x - 1, 0)
	for s in reversed(S):
		if s == ')':
			y += 1
		else:
			y = max(y - 1, 0)
	D.append((x, y))
D.sort(key=lambda x: x[1])
t = 0
for (x, y) in D:
	if x - y >= 0:
		if t >= y:
			t += x - y
		else:
			print('No')
			exit()
D.sort(key=lambda x: x[0])
s = 0
for (x, y) in D:
	if y - x >= 0:
		if s >= x:
			s += y - x
		else:
			print('No')
			exit()
if t != s:
	print('No')
else:
	print('Yes')


from sys import exit
from itertools import accumulate, chain
(n, *s) = open(0).read().split()
t = list(map(lambda x: 2 * x.count('(') - len(x), s))
if sum(t) != 0:
	print('No')
	exit()
st = [[t_, min(accumulate(s_, lambda a, b: a + (1 if b == '(' else -1), initial=0))] for (s_, t_) in zip(s, t)]
now = sum(map(lambda x: x[0], filter(lambda x: x[1] >= 0, st)))
v = list(filter(lambda x: x[1] < 0 and x[0] >= 0, st))
w = list(filter(lambda x: x[1] < 0 and x[0] < 0, st))
v.sort(reverse=True, key=lambda z: z[1])
w.sort(key=lambda z: z[0] - z[1], reverse=True)
for sub in chain(v, w):
	if now + sub[1] < 0:
		print('No')
		exit()
	now += sub[0]
print('Yes')


import sys
readline = sys.stdin.buffer.readline
N = int(readline())
S = [readline().rstrip().decode() for _ in range(N)]

def count_cb_ob(s):
	st = []
	for (i, si) in enumerate(s):
		if si == '(' or len(st) == 0 or st[-1] != '(':
			st.append(si)
		else:
			st.pop()
	return (st.count(')'), st.count('('))
cb_obs = list(map(count_cb_ob, S))
f = list(filter(lambda cb_ob: cb_ob[0] < cb_ob[1], cb_obs))
b = list(filter(lambda cb_ob: cb_ob[0] > cb_ob[1], cb_obs))
s = list(filter(lambda cb_ob: cb_ob[0] == cb_ob[1], cb_obs))
f = sorted(f)
b = sorted(b, key=lambda x: x[1], reverse=True)
count = 0
ans = 'Yes'
for (down, up) in (*f, *s, *b):
	count -= down
	if count < 0:
		ans = 'No'
	count += up
if count != 0:
	print('No')
else:
	print(ans)


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[0] - x[1])
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')


def CNT(A):
	(tmp, Min) = (0, 0)
	for a in A:
		if a == '(':
			tmp += 1
		else:
			tmp -= 1
		Min = min(Min, tmp)
	return (-Min, tmp - Min)
N = int(input())
S = [input() for _ in range(N)]
T = [CNT(s) for s in S]
pls = []
mis = []
for (l, r) in T:
	if l <= r:
		pls.append((l, r))
	else:
		mis.append((l, r))
pls.sort(key=lambda a: a[0])
mis.sort(key=lambda a: a[1], reverse=True)
totl = pls + mis
levl = 0
for (l, r) in totl:
	levl -= l
	if levl < 0:
		print('No')
		exit()
	levl += r
print('Yes' if levl == 0 else 'No')


from operator import itemgetter
N = int(input())

def checkNonnegative(brankets):
	curup = 0
	for (minup, up) in sorted(brankets, key=itemgetter(0), reverse=True):
		if curup + minup < 0:
			return False
		curup += up
	return True

def canArrangeBranket(brankets):
	totup = 0
	left_brankets = []
	right_brankets = []
	for branket in brankets:
		up = 0
		minup = 0
		for c in list(branket):
			if c == '(':
				up += 1
			else:
				up -= 1
				minup = min(minup, up)
		totup += up
		if up >= 0:
			left_brankets.append((minup, up))
		else:
			right_brankets.append((minup - up, -up))
	if totup != 0:
		return False
	return checkNonnegative(left_brankets) and checkNonnegative(right_brankets)
branketList = []
for i in range(N):
	l = input()
	branketList.append(l)
if canArrangeBranket(branketList):
	print('Yes')
else:
	print('No')


n = int(input())
scop = []
scom = []
ssum = 0
for i in range(n):
	s = input()
	sa = 0
	ms = 0
	for i in s:
		if i == '(':
			sa += 1
		else:
			sa -= 1
		if ms > sa:
			ms = sa
	if sa > 0:
		scop.append([ms, sa])
	elif not sa == ms:
		scom.append([ms, sa])
	ssum += sa
if not ssum == 0:
	print('No')
	return
scop.sort(reverse=True)
scom.sort()
now = 0
for i in scop:
	if now + i[0] < 0:
		print('No')
		return
	now += i[1]
for i in scom:
	if now + i[0] < 0:
		print('No')
		return
	now += i[1]
print('Yes')


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, -total))
L.sort(key=lambda x: (-x[0], -x[1]))
R.sort(key=lambda x: (-x[0], -x[1]))
Lcrr_end = 0
for (b, e) in L:
	if Lcrr_end + b < 0:
		print('No')
		exit()
	Lcrr_end += e
Rcrr_end = 0
for (b, e) in R:
	if Rcrr_end + b < 0:
		print('No')
		exit()
	Rcrr_end += e
if Lcrr_end - Rcrr_end == 0:
	print('Yes')
else:
	print('No')


from itertools import permutations
n = int(input())
s = [input() for i in range(n)]
lsa = list()
lsd = list()
for i in range(n):
	now = 0
	mn = 0
	for c in s[i]:
		if c == '(':
			now += 1
		else:
			now -= 1
			mn = min(mn, now)
	if now >= 0:
		lsa.append([mn, now])
	else:
		lsd.append([mn - now, -now])
lsa.sort(reverse=True)
tmn = 0
tend = 0
flag = True
for (mn, end) in lsa:
	tmn = mn + tend
	tend += end
	if tmn < 0 or tend < 0:
		flag = False
		break
tenda = tend
lsd.sort(reverse=True)
tmn = 0
tend = 0
for (mn, end) in lsd:
	tmn = mn + tend
	tend += end
	if tmn < 0 or tend < 0:
		flag = False
		break
if flag and tend == tenda:
	print('Yes')
else:
	print('No')


N = int(input())
Up = []
Down = []
for _ in range(N):
	S = input()
	L = [0]
	mi = 0
	now = 0
	for __ in range(len(S)):
		if S[__] == '(':
			now += 1
		else:
			now -= 1
			mi = min(mi, now)
	if now > 0:
		Up.append((mi, now))
	else:
		Down.append((mi - now, mi, now))
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')


n = int(input())
flag = 0
ML = []
MM = []
MR = []
L = 0
R = 0
for _ in range(n):
	s = input()
	saisho = 0
	cur = 0
	for w in s:
		if w == '(':
			cur += 1
		else:
			cur -= 1
			saisho = min(saisho, cur)
	if cur > 0:
		ML.append((saisho, cur))
	elif cur < 0:
		MR.append((-cur + saisho, -cur))
	else:
		MM.append((saisho, cur))
ML.sort(reverse=True)
MR.sort(reverse=True)
leftnow = 0
for (saisho, delta) in ML:
	if leftnow + saisho < 0:
		flag = 1
	leftnow += delta
for (saisho, delta) in MM:
	if leftnow + saisho < 0:
		flag = 1
	leftnow += delta
rightnow = 0
for (saisho, delta) in MR:
	if rightnow + saisho < 0:
		flag = 1
	rightnow += delta
if rightnow != leftnow:
	flag = 1
if flag:
	print('No')
else:
	print('Yes')


n = int(input())
L = 0
R = 0
p1 = []
p2 = []
p3 = []
for _ in range(n):
	s = input()
	flag = 0
	ll = 0
	rr = 0
	temp = 0
	for ss in s:
		if ss == ')':
			if rr > 0:
				rr -= 1
			else:
				ll += 1
		elif ss == '(':
			rr += 1
	if rr > 0 and ll > 0:
		p1.append((ll - rr, ll, rr))
	elif rr > 0:
		p3.append(rr)
	else:
		p2.append(ll)
cnt = 0
for item in p3:
	cnt += item
p1.sort()
for item in p1:
	lll = item[1]
	rrr = item[2]
	cnt -= lll
	if cnt < 0:
		print('No')
		exit()
	cnt += rrr
	if cnt < 0:
		print('No')
		exit()
for item in p2:
	cnt -= item
	if cnt < 0:
		print('No')
		exit()
if cnt == 0:
	print('Yes')
else:
	print('No')


import sys
input = lambda : sys.stdin.readline().strip()
n = int(input())
A = []
PM = [[0, 0] for i in range(n)]
for i in range(n):
	now = 0
	mini = 0
	for j in input():
		if j == '(':
			now += 1
		else:
			now -= 1
			mini = min(mini, now)
	PM[i] = [mini, now]
if sum([PM[i][1] for i in range(n)]) != 0:
	print('No')
	return
MINI = 0
NOW = 0
PMf = [PM[i] for i in range(n) if PM[i][1] >= 0]
PMf.sort()
for i in range(len(PMf)):
	MINI = min(MINI, NOW + PMf[-i - 1][0])
	NOW += PMf[-i - 1][1]
	if MINI < 0:
		print('No')
		return
PMs = [PM[i] for i in range(n) if PM[i][1] < 0]
PMs = sorted(PMs, key=lambda x: x[1] - x[0])
for i in range(len(PMs)):
	MINI = min(MINI, NOW + PMs[-i - 1][0])
	NOW += PMs[-i - 1][1]
	if MINI < 0:
		print('No')
		return
print('Yes')


import sys
from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
from heapq import heappop, heappush
sys.setrecursionlimit(10 ** 7)

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]), reverse=True)[::-1]
	for (go, max_depth) in ways_pos:
		if current + max_depth >= 0:
			current += go
		else:
			print('No')
			return None
	for (go, max_depth) in ways_neg:
		if current + max_depth >= 0:
			current += go
		else:
			print('No')
			return None
	if current == 0:
		print('Yes')
	else:
		print('No')

def __starting_point():
	run()
__starting_point()


n = int(input())
s1 = 0
s2 = 0
s3 = list()
s4 = list()
s5 = list()
for i in range(n):
	a = str(input())
	b = str()
	while a != b:
		b = a
		a = a.replace('()', '')
	if len(a) == 0:
		continue
	if a[0] == '(' and a[-1] == '(':
		s1 += len(a)
	elif a[0] == ')' and a[-1] == ')':
		s2 += len(a)
	else:
		x = len(a.replace('(', ''))
		y = len(a) - x
		if x < y:
			s3.append([x, y])
		elif x == y:
			s4.append(x)
		else:
			s5.append([x, y])
s3.sort(key=lambda x: x[0])
s5.sort(key=lambda x: x[1])
flag = 0
limit = s1
if len(s3) != 0:
	for i in range(len(s3)):
		if limit >= s3[i][0]:
			limit = limit - s3[i][0] + s3[i][1]
		else:
			flag = 1
			print('No')
			break
x = limit
if len(s4) != 0:
	if limit < max(s4):
		print('No')
		flag = 1
limit = s2
if len(s5) != 0:
	if flag == 0:
		for i in range(len(s5)):
			if limit >= s5[i][1]:
				limit = limit - s5[i][1] + s5[i][0]
			else:
				flag = 1
				print('No')
				break
if flag == 0 and len(s4) != 0:
	if limit < max(s4):
		print('No')
		flag = 1
if flag == 0 and x == limit:
	print('Yes')
elif flag == 0:
	print('No')


def kakko(S):
	cum = 0
	mi = 0
	for s in S:
		cum += 1 if s == '(' else -1
		mi = min(mi, cum)
	return (-mi, cum - mi)
N = int(input())
S = [kakko(input()) for _ in range(N)]
plus = []
minus = []
for (l, r) in S:
	if l <= r:
		plus.append((l, r))
	else:
		minus.append((l, r))
plus.sort(key=lambda a: a[0])
minus.sort(key=lambda a: a[1], reverse=True)
M = 0
for (l, r) in plus + minus:
	M -= l
	if M < 0:
		print('No')
		exit()
	M += r
print('Yes' if M == 0 else 'No')


def count_scan(s):
	max_min = 0
	compare = 0
	for c in s:
		if c == '(':
			compare += 1
		elif c == ')':
			compare -= 1
			max_min = min(max_min, compare)
	return (min(max_min, compare), compare)

def key(lst):
	(m, c) = lst
	if c > 0:
		return (1, m)
	else:
		return (-1, c - m)

def main():
	n = int(input())
	lst = [input() for _ in range(n)]
	txt = 'No'
	ans = 0
	for (max_min, c) in sorted([count_scan(s) for s in lst], reverse=True, key=key):
		if max_min + ans < 0:
			break
		ans += c
	else:
		if ans == 0:
			txt = 'Yes'
	print(txt)

def __starting_point():
	main()
__starting_point()


import sys
n = int(input())
jsum = 0
gjlist = []
ejlist = []
esum = 0

def jouka(t):
	global jsum
	global gjlist
	global ejlist
	global esum
	tmpj = 0
	tmpe = 0
	for i in range(len(t)):
		if t[i] == '(':
			tmpj += 1
		elif tmpj == 0:
			tmpe += 1
		else:
			tmpj -= 1
	if tmpe == 0:
		jsum += tmpj
	elif tmpj == 0:
		esum += tmpe
	elif tmpj > tmpe:
		gjlist.append([tmpe, tmpj])
	else:
		ejlist.append([tmpe, tmpj])
for j in range(n):
	jouka(input())
gjlist.sort(key=lambda x: x[0])
ejlist.sort(reverse=True, key=lambda x: x[0])
streak = jsum
for gj in gjlist:
	if gj[0] > streak:
		print('No')
		sys.exit()
	else:
		streak += gj[1] - gj[0]
for ej in ejlist:
	if ej[0] > streak:
		print('No')
		sys.exit()
	else:
		streak += ej[1] - ej[0]
if streak == esum:
	print('Yes')
else:
	print('No')


def count(i, s):
	left = right = 0
	for e in s:
		if e == '(':
			left += 1
		elif left == 0:
			right += 1
		else:
			left -= 1
	return (left - right, left, right, i)
n = int(input())
S = [input() for i in range(n)]
s1 = []
s2 = []
for (i, e) in enumerate(S):
	c = count(i, e)
	if c[0] >= 0:
		s1.append((c[2], c[3]))
	else:
		s2.append((-c[1], c[3]))
s1.sort()
s2.sort()
s = []
for e in s1:
	s.append(S[e[1]])
for e in s2:
	s.append(S[e[1]])
(_, left, right, _) = count(0, ''.join(s))
if left == right == 0:
	print('Yes')
else:
	print('No')


n = int(input())
s = ['' for _ in range(n)]
for i in range(n):
	s[i] = input()
(hp, hm) = (0, 0)
p = []
m = []
for i in range(n):
	t = s[i]
	lv = 0
	mn = 0
	for u in t:
		if u == '(':
			lv += 1
		elif u == ')':
			lv -= 1
		mn = min(mn, lv)
	if lv >= 0:
		p.append([lv, mn])
		hp += lv
	else:
		lv *= -1
		m.append([lv, mn + lv])
		hm += lv
if hp != hm:
	print('No')
	exit()
p.sort(key=lambda x: x[1], reverse=1)
m.sort(key=lambda x: x[1], reverse=1)
h = 0
(lv, mn) = (0, 0)
for (a, b) in p:
	lv += a
	mn += b
	if mn < 0:
		print('No')
		exit()
	mn = lv
(lv, mn) = (0, 0)
for (a, b) in m:
	lv += a
	mn += b
	if mn < 0:
		print('No')
		exit()
	mn = lv
print('Yes')


def main():
	if 'get_ipython' in globals():
		n = 4
		S = ['((()))', '((((((', '))))))', '()()()']
	else:
		n = int(input())
		S = [input() for _ in range(n)]

	def CNT(A):
		(tmp, Min) = (0, 0)
		for a in A:
			if a == '(':
				tmp += 1
			else:
				tmp -= 1
			Min = min(Min, tmp)
		return (-Min, tmp - Min)
	T = [CNT(s) for s in S]
	pls = []
	mis = []
	for (l, r) in T:
		if l <= r:
			pls.append((l, r))
		else:
			mis.append((l, r))
	pls.sort(key=lambda a: a[0])
	mis.sort(key=lambda a: a[1], reverse=True)
	total = pls + mis
	levl = 0
	for (l, r) in total:
		levl -= l
		if levl < 0:
			print('No')
			return
		levl += r
	print('Yes' if levl == 0 else 'No')
main()


n = int(input())
A = []
B = []
c = 0
for i in range(n):
	S = input()
	a = 0
	b = 0
	for j in range(len(S)):
		if S[j] == '(':
			a = a + 1
		else:
			a = a - 1
			if a < b:
				b = a
	b = b * -1
	if b == 0:
		c = c + a
	elif a >= 0:
		B.append([b, -1 * a])
	else:
		A.append([b + a, a])
A.sort()
B.sort()
f = 0
for i in range(len(B)):
	if c - B[i][0] < 0:
		f = 1
	c = c - B[i][1]
d = 0
for i in range(len(A)):
	if d - A[i][0] < 0:
		f = 1
	d = d - A[i][1]
if c != d:
	f = 1
if f == 0:
	print('Yes')
else:
	print('No')


n = int(input())
left = []
right = []
for i in range(n):
	s = input()
	l = len(s)
	a = 0
	b = 0
	a1 = 0
	b1 = 0
	for j in range(l):
		if s[j] == ')':
			a1 += 1
		else:
			a1 -= 1
		a = max(a1, a)
	for j in range(l):
		if s[l - 1 - j] == '(':
			b1 += 1
		else:
			b1 -= 1
		b = max(b1, b)
	if a > b:
		right.append((a, b))
	else:
		left.append((a, b))
right.sort(key=lambda x: x[1], reverse=True)
left.sort(key=lambda x: x[0])
all = left + right
if all[0][0] > 0:
	print('No')
	exit()
st = all[0][1]
for i in range(1, n):
	st -= all[i][0]
	if st < 0:
		print('No')
		exit()
	st += all[i][1]
if st > 0:
	print('No')
else:
	print('Yes')


import bisect
n = int(input())
opens = 0
closes = 0
ls = []
rs = []
for _ in range(n):
	s = input()
	cnt1 = 0
	cnt2 = 0
	for i in range(len(s)):
		if s[i] == '(':
			cnt1 += 1
		elif cnt1 == 0:
			cnt2 += 1
		else:
			cnt1 -= 1
	if cnt2 == 0:
		opens += cnt1
	elif cnt1 == 0:
		closes += cnt2
	else:
		ls.append(cnt2)
		rs.append(cnt1)
ls = sorted(ls)
rs = sorted(rs)
lefts = sum(ls)
rights = sum(rs)
if opens - closes != lefts - rights:
	print('No')
else:
	flag = False
	if len(ls) == 0:
		flag = True
	for a in ls:
		if opens < a or closes < a + (rights - lefts):
			continue
		pos = bisect.bisect_right(rs, a + (rights - lefts))
		if pos != 0:
			flag = True
			break
	for b in rs:
		if opens < b + (lefts - rights) or closes < b:
			continue
		pos = bisect.bisect_right(ls, b + (lefts - rights))
		if pos != 0:
			flag = True
			break
	if flag == True:
		print('Yes')
	else:
		print('No')


import sys
input = sys.stdin.readline
n = int(input())
s = [input().strip() for _ in range(n)]
plus = []
minus = []
for i in s:
	min_v = 0
	sum_v = 0
	for j in i:
		sum_v += 1 if j == '(' else -1
		min_v = min(min_v, sum_v)
	if sum_v > 0:
		plus.append((min_v, sum_v))
	else:
		minus.append((min_v - sum_v, -sum_v))
plus.sort(key=lambda x: x[0], reverse=True)
minus.sort(key=lambda x: x[0], reverse=True)
sum_plus = 0
for (min_v, sum_v) in plus:
	if sum_plus + min_v < 0:
		print('No')
		exit()
	sum_plus += sum_v
sum_minus = 0
for (min_v, sum_v) in minus:
	if sum_minus + min_v < 0:
		print('No')
		exit()
	sum_minus += sum_v
if sum_plus == sum_minus:
	print('Yes')
else:
	print('No')


from itertools import accumulate
from bisect import bisect_left
import sys
readlines = sys.stdin.buffer.readlines
readline = sys.stdin.buffer.readline
N = int(readline())
memo = [None] * N
for (i, s) in enumerate(readlines()):
	s = s.rstrip().decode()
	memo[i] = (min(0, min(accumulate((1 if c == '(' else -1 for c in s)))), sum((1 if c == '(' else -1 for c in s)))
positives = sorted(((m, d) for (m, d) in memo if d >= 0))
negatives = sorted(((m - d, -d) for (m, d) in memo if d < 0))

def solve():
	left = 0
	for (m, d) in reversed(positives):
		if left + m < 0:
			return False
		left += d
	right = 0
	for (m, d) in reversed(negatives):
		if right + m < 0:
			return False
		right += d
	return left == right
print('Yes' if solve() else 'No')


n = int(input())
plus = []
minus = []
for i in range(n):
	min_v = 0
	sum_v = 0
	s = input()
	for j in s:
		sum_v += 1 if j == '(' else -1
		min_v = min(min_v, sum_v)
	if sum_v > 0:
		plus.append((min_v, sum_v))
	else:
		minus.append((min_v - sum_v, -sum_v))
plus.sort(key=lambda x: x[0], reverse=True)
minus.sort(key=lambda x: x[0], reverse=True)
sum_plus = 0
for (min_v, sum_v) in plus:
	if sum_plus + min_v < 0:
		print('No')
		exit()
	sum_plus += sum_v
sum_minus = 0
for (min_v, sum_v) in minus:
	if sum_minus + min_v < 0:
		print('No')
		exit()
	sum_minus += sum_v
if sum_plus == sum_minus:
	print('Yes')
else:
	print('No')


N = int(input())
L = []
R = []
for _ in range(N):
	S = list(str(input()))
	len_S = len(S)
	L_d = 0
	L_min_d = 0
	R_d = 0
	R_min_d = 0
	for i in range(len_S):
		if S[i] == '(':
			L_d += 1
		else:
			L_d -= 1
			L_min_d = min(L_min_d, L_d)
		if S[len_S - i - 1] == ')':
			R_d += 1
		else:
			R_d -= 1
			R_min_d = min(R_min_d, R_d)
	if L_d >= 0:
		L.append((L_d, L_min_d))
	else:
		R.append((R_d, R_min_d))
L.sort(key=lambda x: x[1], reverse=True)
R.sort(key=lambda x: x[1], reverse=True)
ans = 'Yes'
l_now = 0
for (d, min_d) in L:
	if l_now + min_d < 0:
		ans = 'No'
		break
	else:
		l_now += d
r_now = 0
for (d, min_d) in R:
	if r_now + min_d < 0:
		ans = 'No'
		break
	else:
		r_now += d
if l_now != r_now:
	ans = 'No'
print(ans)


(N, *S) = open(0)
L = [s.count('(') for s in S]
R = [s.count(')') for s in S]
X = sorted(((s[:-1], r) for (s, l, r) in zip(S, L, R) if l - r >= 0), key=lambda x: x[1])
Y = sorted(((s[:-1], l) for (s, l, r) in zip(S, L, R) if l - r < 0), key=lambda x: -x[1])
cnt = 0
for (s, _) in X + Y:
	for si in s:
		cnt += 1 if si == '(' else -1
		if cnt < 0:
			print('No')
			quit()
if cnt == 0:
	print('Yes')
else:
	print('No')


N = int(input())
lremain = []
rremain = []
for i in range(N):
	S = input()
	left = 0
	right = 0
	for c in S:
		if c == '(':
			left += 1
		elif left == 0:
			right += 1
		else:
			left -= 1
	if left == 0 and right == 0:
		continue
	lremain.append(left)
	rremain.append(right)
N = len(lremain)
if sum(lremain) != sum(rremain):
	print('No')
	exit()
head = 0
tail = 0
removei = set()
for i in range(N):
	if rremain[i] == 0:
		head += lremain[i]
		removei.add(i)
	if lremain[i] == 0:
		tail += rremain[i]
		removei.add(i)
lnr = []
for i in range(N):
	if not i in removei:
		lnr.append((lremain[i], rremain[i]))
N = len(lnr)
lnr.sort(key=lambda x: x[1] - x[0])
for i in range(N):
	if head < lnr[i][1]:
		print('No')
		exit()
	head -= lnr[i][1]
	head += lnr[i][0]
if head == tail:
	print('Yes')
else:
	print('No')


N = int(input())
S = list((input() for _ in range(N)))
X = []
for s in S:
	(l, r) = (0, 0)
	for c in s:
		if c == ')':
			if r > 0:
				r -= 1
			else:
				l += 1
		else:
			r += 1
	X.append([l, r])
X.sort(key=lambda x: x[0])
Y = []
L = 0
for (p, q) in X:
	if p > L:
		print('No')
		quit()
	if q >= p:
		L -= p
		L += q
	else:
		Y.append([p, q])
Y.sort(key=lambda x: -x[1])
for (a, b) in Y:
	if a > L:
		print('No')
		quit()
	L -= a
	L += b
if L:
	print('No')
else:
	print('Yes')


import sys
input = sys.stdin.readline

def main():
	N = int(input())
	S = [input().strip() for _ in range(N)]
	Up = []
	Down = []
	for s in S:
		now = 0
		m = 0
		for t in s:
			if t == '(':
				now += 1
			else:
				now -= 1
			if now < m:
				m = now
		if now >= 0:
			Up.append((m, now))
		else:
			Down.append((m - now, -now))
	up = 0
	Up.sort(reverse=True)
	for (m, inc) in Up:
		if up + m < 0:
			print('No')
			return
		up += inc
	down = 0
	Down.sort(reverse=True)
	for (m, dec) in Down:
		if down + m < 0:
			print('No')
			return
		down += dec
	if up != down:
		print('No')
		return
	print('Yes')

def __starting_point():
	main()
__starting_point()


n = int(input())
lipp = []
lipm = []
limm = []
allcnt = 0
for _ in range(n):
	s = input()
	cnt = 0
	mi = 0
	for x in s:
		if x == ')':
			cnt -= 1
			mi = min(mi, cnt)
		else:
			cnt += 1
	if mi == 0:
		lipp.append(cnt)
	elif cnt >= 0:
		lipm.append([mi, cnt])
	else:
		limm.append([cnt - mi, mi, cnt])
	allcnt += cnt
if allcnt != 0:
	print('No')
	exit()
now = sum(lipp)
lipm.sort(reverse=True)
limm.sort()
for (mi, cnt) in lipm:
	if mi + now < 0:
		print('No')
		exit()
	now += cnt
cnt2 = 0
for (sa, mi, cnt) in limm:
	if sa > cnt2:
		print('No')
		exit()
	cnt2 += -cnt
print('Yes')


import heapq
import sys
input = sys.stdin.readline
n = int(input())
sss = [input()[:-1] for _ in range(n)]
a = []
b = []
ss = 0
ii = 0
for i in sss:
	mi = 0
	ma = 0
	s = 0
	for j in i:
		if j == '(':
			s += 1
		else:
			s -= 1
		mi = min(mi, s)
		ma = max(ma, s)
	ss += s
	if s >= 0:
		a.append([s, mi, ma, ii])
	else:
		mi = 0
		ma = 0
		s = 0
		for j in reversed(i):
			if j == ')':
				s += 1
			else:
				s -= 1
			mi = min(mi, s)
			ma = max(ma, s)
		b.append([s, mi, ma, ii])
	ii += 1
if ss != 0:
	print('No')
	exit()
a.sort(reverse=1, key=lambda x: x[1])
b.sort(reverse=1, key=lambda x: x[1])

def ok(a):
	s = 0
	for (i, j, _, _) in a:
		if s + j < 0:
			print('No')
			exit()
		s += i
ok(a)
ok(b)
print('Yes')


n = int(input())
s = []
for _ in range(n):
	s.append(input())
a = []
b = []
c = []
for i in s:
	mi = 0
	tmp = 0
	for j in i:
		if j == '(':
			tmp += 1
		else:
			tmp -= 1
		if tmp < mi:
			mi = tmp
	if tmp > 0:
		a.append((mi, tmp))
	elif tmp == 0:
		b.append((mi, tmp))
	else:
		c.append((mi, tmp))
a.sort(key=lambda x: -x[0])
c.sort(key=lambda x: x[0] - x[1])
tmp = 0
for (i, j) in a + b + c:
	if tmp + i < 0:
		print('No')
		break
	tmp += j
else:
	if tmp == 0:
		print('Yes')
	else:
		print('No')


import sys
from operator import itemgetter

def _s():
	return sys.stdin.readline().strip()

def _i():
	return int(sys.stdin.readline().strip())

def parse(s):
	(l, r) = (0, 0)
	for c in s:
		if c == '(':
			r += 1
		elif r > 0:
			r -= 1
		else:
			l += 1
	return (l, r)

def main():
	n = _i()
	p = list(filter(lambda x: x != (0, 0), [parse(_s()) for _ in range(n)]))
	(u, d) = ([], [])
	for (l, r) in p:
		u.append((l, r)) if l <= r else d.append((l, r))
	u.sort(key=itemgetter(0))
	d.sort(key=itemgetter(1), reverse=True)
	r = 0
	for (ll, rr) in u + d:
		if ll > r:
			return 'No'
		r += rr - ll
	return 'Yes' if r == 0 else 'No'
print(main())


def check(li):
	b = 0
	for (eb, eh) in li:
		if b + eb < 0:
			return False
		b += eh
	return True
n = int(input())
s = [input() for _ in range(n)]
l = []
r = []
sm = 0
for word in s:
	h = 0
	b = 0
	for e in word:
		if e == '(':
			h += 1
		else:
			h -= 1
		b = min(b, h)
	if h > 0:
		l.append([b, h])
	else:
		r.append([b - h, -h])
	sm += h
l.sort(reverse=True)
r.sort(reverse=True)
if check(l) and check(r) and (sm == 0):
	ans = 'Yes'
else:
	ans = 'No'
print(ans)


from heapq import heappush, heappop
import sys
input = sys.stdin.readline
N = int(input())
pos = []
zero = []
neg = []
for _ in range(N):
	S = input().rstrip()
	m = s = 0
	for c in S:
		if c == '(':
			s += 1
		else:
			s -= 1
			m = min(m, s)
	if s > 0:
		heappush(pos, (-m, s))
	elif s == 0:
		heappush(zero, (m, s))
	else:
		heappush(neg, (m - s, s))
num = 0
while pos:
	(m, s) = heappop(pos)
	if num - m < 0:
		print('No')
		exit()
	num += s
while zero:
	(m, s) = heappop(zero)
	if num + m < 0:
		print('No')
		exit()
while neg:
	(m, s) = heappop(neg)
	m += s
	if num + m < 0:
		print('No')
		exit()
	num += s
if num == 0:
	print('Yes')
else:
	print('No')


N = int(input())
S = [input() for i in range(N)]
LR = []
for s in S:
	(left, right) = (0, 0)
	for ss in s:
		if ss == '(':
			left += 1
		elif left:
			left -= 1
		else:
			right += 1
	LR.append([left, right])
plus = []
minus = []
for (l, r) in LR:
	if l >= r:
		plus.append([l, r])
	else:
		minus.append([l, r])
plus.sort(key=lambda x: x[1])
minus.sort(key=lambda x: -x[0])
cnt = 0
for (l, r) in plus + minus:
	cnt -= r
	if cnt < 0:
		print('No')
		exit()
	cnt += l
if cnt == 0:
	print('Yes')
else:
	print('No')


N = int(input())
list_plus = []
list_minus = []
for i in range(N):
	S = input()
	state = 0
	min_state = 0
	for s in S:
		if s == '(':
			state += 1
		else:
			state -= 1
		min_state = min(min_state, state)
	if state > 0:
		list_plus.append((min_state, state))
	else:
		list_minus.append((min_state - state, -state))

def compute(arr):
	total_state = 0
	for (min_state, state) in arr[::-1]:
		if total_state + min_state < 0:
			print('No')
			return
		total_state += state
	return total_state
list_plus.sort()
total_state_plus = compute(list_plus)
list_minus.sort()
total_state_minus = compute(list_minus)
print('Yes' if total_state_plus == total_state_minus else 'No')


from cython import longlong

def main():
	n: longlong
	n = int(input())
	(lp, lm) = ([], [])
	for i in range(n):
		s = input()
		x: longlong
		y: longlong
		(x, y) = (0, 0)
		for j in s:
			if j == '(':
				x += 1
			else:
				x -= 1
			y = min(y, x)
		if x > 0:
			lp.append((y, x))
		else:
			lm.append((y - x, -x))
	lp.sort()
	s: longlong
	s = 0
	i: longlong
	j: longlong
	for (i, j) in lp[::-1]:
		if s + i < 0:
			print('No')
			return
		s += j
	lm.sort()
	t: longlong
	t = 0
	i: longlong
	j: longlong
	for (i, j) in lm[::-1]:
		if t + i < 0:
			print('No')
			return
		t += j
	if s == t:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


import sys
from operator import itemgetter
N = int(input())
S = sys.stdin.read().split()
HT_high = []
HT_low = []
for s in S:
	t = 0
	h = 0
	for c in s:
		if c == '(':
			h += 1
		else:
			h -= 1
			if h < t:
				t = h
	if h >= 0:
		HT_high.append((h, t))
	else:
		HT_low.append((h, t))
HT_high.sort(key=itemgetter(1), reverse=True)
HT_low.sort(key=lambda x: x[0] - x[1], reverse=True)
h0 = 0
for (h, t) in HT_high + HT_low:
	if h0 + t < 0:
		print('No')
		exit()
	h0 += h
if h0 != 0:
	print('No')
else:
	print('Yes')


import sys
input = lambda : sys.stdin.readline().rstrip()
N = int(input())

def I():
	s = input()
	mi = 0
	su = 0
	t = 0
	for a in s:
		if a == '(':
			t += 1
		else:
			t -= 1
			mi = min(mi, t)
	return (mi, t)
X = [I() for _ in range(N)]
if sum([x[1] for x in X]):
	print('No')
	exit()
X = sorted(X, key=lambda x: -10 ** 10 if x[0] == 0 else -10 ** 9 - x[0] if x[1] > 0 else x[0] - x[1])
T = 0
for (mi, t) in X:
	if T + mi < 0:
		print('No')
		exit()
	T += t
print('Yes')


N = int(input())
X = [input() for _ in range(N)]
ctr = [[], []]
for s in X:
	t = [0] * (len(s) + 1)
	for j in range(len(s)):
		t[j + 1] = t[j] + int(s[j] == '(') - int(s[j] == ')')
	if t[-1] > 0:
		ctr[0].append((min(t), t[-1]))
	else:
		ctr[1].append((min(t) - t[-1], -t[-1]))
flag = True
tmp = 0
for i in range(2):
	cur = 0
	for (m, c) in sorted(ctr[i], reverse=True):
		flag &= cur + m >= 0
		cur += c
	if i == 0:
		tmp = cur
	else:
		flag &= tmp == cur
if flag:
	print('Yes')
else:
	print('No')


n = int(input())
(po, ne) = ([], [])
now = 0
for i in range(n):
	p = 0
	mp = 0
	for j in input():
		p += (1, -1)[j == ')']
		mp = min(p, mp)
	if mp:
		if p >= 0:
			po.append((mp, p))
		else:
			ne.append((mp - p, -p))
	else:
		now += p
po.sort(reverse=1)
ne.sort(reverse=1)
for (m, p) in po:
	if m + now < 0:
		print('No')
		return
	else:
		now += p
nnow = 0
for (m, p) in ne:
	if m + nnow < 0:
		print('No')
		return
	else:
		nnow += p
print('Yes' if now == nnow else 'No')


N = int(input())
plus = []
minus = []
for _ in range(N):
	S = input()
	tmp = 0
	min_tmp = 0
	for i in range(len(S)):
		if S[i] == '(':
			tmp += 1
		else:
			tmp -= 1
		if tmp < 0:
			min_tmp = min(min_tmp, tmp)
	tmp = 0
	max_tmp = 0
	for i in range(len(S)):
		i = len(S) - i - 1
		if S[i] == '(':
			tmp += 1
		else:
			tmp -= 1
		if tmp > 0:
			max_tmp = max(max_tmp, tmp)
	if tmp >= 0:
		plus.append((tmp, max_tmp, -min_tmp))
	else:
		minus.append((tmp, max_tmp, -min_tmp))
plus.sort(key=lambda x: x[2])
minus.sort(reverse=True, key=lambda x: x[1])
ans = 0
for (i, j, k) in plus:
	if ans < k:
		print('No')
		exit()
	ans += i
for (i, j, k) in minus:
	if ans < k:
		print('No')
		exit()
	ans += i
if ans == 0:
	print('Yes')
else:
	print('No')


def main():
	N = int(input())
	up_lines = []
	down_lines = []
	for i in range(N):
		s = input()
		height = 0
		bottom = 0
		for c in s:
			if c == '(':
				height += 1
			else:
				height -= 1
				bottom = min(bottom, height)
		if height > 0:
			up_lines.append((bottom, height))
		else:
			down_lines.append((bottom - height, -height))
	up_lines.sort(reverse=True, key=lambda line: line[0])
	down_lines.sort(reverse=True, key=lambda line: line[0])
	left = 0
	for (bottom, height) in up_lines:
		if left + bottom < 0:
			print('No')
			return
		left += height
	right = 0
	for (bottom, height) in down_lines:
		if right + bottom < 0:
			print('No')
			return
		right += height
	if left == right:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


N = int(input())
tleft = 0
tright = 0
a = []
b = []
for _ in range(N):
	s = input()
	left = 0
	lmin = 0
	for c in s:
		if c == '(':
			left += 1
		else:
			left -= 1
			lmin = min(lmin, left)
	if left >= 0:
		if lmin == 0:
			tleft += left
		else:
			a.append([left, lmin])
	elif left <= lmin:
		tright += left
	else:
		b.append([left, lmin])
a.sort(key=lambda x: -x[1])
b.sort(key=lambda x: x[1])
for (left, lmin) in a:
	if tleft + lmin < 0:
		print('No')
		return
	else:
		tleft += left
for (left, lmin) in b:
	if tleft + lmin < 0:
		print('No')
		return
	else:
		tleft += left
if tleft + tright == 0:
	print('Yes')
else:
	print('No')


N = int(input())
S = [input() for _ in [0] * N]
A = [(0, 0)] * N
B = [(0, 0)] * N
for (i, T) in enumerate(S):
	m = 0
	M = 0
	tot = len(T) - T.count(')') * 2
	if tot >= 0:
		tot = 0
		for t in T:
			if t == ')':
				tot -= 1
				m = min(m, tot)
			else:
				tot += 1
		A[i] = (m, tot)
	else:
		tot = 0
		for t in T[::-1]:
			if t == ')':
				tot += 1
			else:
				tot -= 1
				m = min(m, tot)
		B[i] = (m, tot)
A.sort(reverse=True)
B.sort(reverse=True)
totA = 0
ans = True
for (m, t) in A:
	if totA + m < 0:
		ans = False
		break
	totA += t
totB = 0
if ans:
	for (m, t) in B:
		if totB + m < 0:
			ans = False
			break
		totB += t
	if totA != totB:
		ans = False
if ans:
	print('Yes')
else:
	print('No')


N = int(input())
S = [input() for _ in range(N)]
T = []
for s in S:
	while '()' in s:
		s = s.replace('()', '')
	T.append(s)
l1 = []
l2 = []
for t in T:
	op = t.find('(')
	if op < 0:
		op = len(t)
	cl = len(t) - op
	if cl + op != 0:
		if cl <= op:
			l1.append((cl, op))
		else:
			l2.append((op, cl))
l1.sort()
l2.sort(reverse=True)
result = False
x = 0
if len(l1) + len(l2) == 0:
	result = True
elif len(l1) > 0 and len(l2) > 0 and (l1[0][0] + l2[-1][0] == 0):
	for (cl, op) in l1:
		x -= cl
		if x < 0:
			break
		x += op
	if x >= 0:
		for (op, cl) in l2:
			x -= cl
			if x < 0:
				break
			x += op
		if x == 0:
			result = True
print('Yes' if result else 'No')


import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
from itertools import count, permutations, chain
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda : int(input())
mis = lambda : list(map(int, input().split()))
lmis = lambda : list(mis())
INF = float('inf')
N1097 = 10 ** 9 + 7

def meg(f, ok, ng):
	while abs(ok - ng) > 1:
		mid = (ok + ng) // 2
		if f(mid):
			ok = mid
		else:
			ng = mid
	return ok

def get_inv(n, modp):
	return pow(n, modp - 2, modp)

def factorials_list(n, modp):
	fs = [1]
	for i in range(1, n + 1):
		fs.append(fs[-1] * i % modp)
	return fs

def invs_list(n, fs, modp):
	invs = [get_inv(fs[-1], modp)]
	for i in range(n, 1 - 1, -1):
		invs.append(invs[-1] * i % modp)
	invs.reverse()
	return invs

def comb(n, k, modp):
	num = 1
	for i in range(n, n - k, -1):
		num = num * i % modp
	den = 1
	for i in range(2, k + 1):
		den = den * i % modp
	return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
	return fs[n] * invs[n - k] * invs[k] % modp

class UnionFindEx:

	def __init__(self, size):
		self.roots = [-1] * size

	def getRootID(self, i):
		r = self.roots[i]
		if r < 0:
			return i
		else:
			r = self.getRootID(r)
			self.roots[i] = r
			return r

	def getGroupSize(self, i):
		return -self.roots[self.getRootID(i)]

	def connect(self, i, j):
		(r1, r2) = (self.getRootID(i), self.getRootID(j))
		if r1 == r2:
			return False
		if self.getGroupSize(r1) < self.getGroupSize(r2):
			(r1, r2) = (r2, r1)
		self.roots[r1] += self.roots[r2]
		self.roots[r2] = r1
		return True
Yes = 'Yes'
No = 'No'

def main():
	N = ii()
	up = []
	down = []
	for _ in range(N):
		S = input()
		h = 0
		b = 0
		for s in S:
			if s == '(':
				h += 1
			else:
				h -= 1
				b = min(b, h)
		if h >= 0:
			up.append((h, b))
		else:
			down.append((h, b))
	up.sort(key=lambda t: t[1], reverse=True)
	down.sort(key=lambda t: t[0] - t[1], reverse=True)
	H = 0
	for (h, b) in up:
		if H + b >= 0:
			H += h
		else:
			print(No)
			return
	for (h, b) in down:
		if H + b >= 0:
			H += h
		else:
			print(No)
			return
	if H == 0:
		print(Yes)
	else:
		print(No)
main()


n = int(input())
x = []
y = []
for _ in range(n):
	s = input()
	b = c = 0
	for t in s:
		if t == '(':
			b += 1
		elif b == 0:
			c += 1
		else:
			b -= 1
	if b >= c:
		x.append((c, b))
	else:
		y.append((-b, c))
x.sort()
y.sort()
now = 0
for (a, b) in x:
	now -= a
	if now < 0:
		print('No')
		exit()
	now += b
for (a, b) in y:
	now -= b
	if now < 0:
		print('No')
		exit()
	now -= a
if now == 0:
	print('Yes')
else:
	print('No')


from itertools import chain
n = int(input())
ss = [input() for i in range(n)]

def parse(s):
	(cl, op) = (0, 0)
	for c in s:
		if c == '(':
			op += 1
		elif op > 0:
			op -= 1
		else:
			cl += 1
	return (cl, op)
inc = []
dec = []
close = []
for s in ss:
	(cl, op) = parse(s)
	if cl < op:
		inc.append((cl, op))
	elif op == 0:
		close.append((cl, op))
	else:
		dec.append((cl, op))
peak = 0
inc.sort()
dec.sort(reverse=True)
for (cl, op) in chain(inc, dec, close):
	if peak < cl:
		print('No')
		return
	peak += op - cl
print('Yes' if peak == 0 else 'No')


N = int(input())
S = []
L = 0
R = 0
for i in range(N):
	s = input()
	n = len(s)
	data = [s[j] for j in range(n)]
	l = 0
	r = 0
	yabai = float('inf')
	for j in range(n):
		l += s[j] == '('
		r += s[j] == ')'
		yabai = min(l - r, yabai)
	S.append([yabai, l - r])
	L += l
	R += r
if L != R:
	print('No')
else:
	first = []
	last = []
	for i in range(N):
		(yabai, gap) = S[i]
		if gap > 0:
			first.append(S[i])
		else:
			last.append([gap - yabai, yabai])
	first.sort(reverse=True)
	last.sort(reverse=True)
	G = 0
	for i in range(len(first)):
		(yabai, gap) = first[i]
		if 0 > G + yabai:
			print('No')
			exit()
		else:
			G += gap
	for j in range(len(last)):
		(gapminus, yabai) = last[j]
		if 0 > G + yabai:
			print('No')
			break
		else:
			G += gapminus + yabai
	else:
		print('Yes')


N = int(input())
S = [input() for _ in range(N)]
D = [[0] * 2 for _ in range(N)]
for i in range(N):
	s = S[i]
	ma = 0
	l = 0
	r = 0
	for j in s:
		if j == ')':
			r += 1
		else:
			l += 1
		ma = max(ma, r - l)
	D[i] = [l - r, ma, s]
cnt = 0
P = []
M = []
for (x, y, s) in D:
	if x >= 0:
		if y <= 0:
			cnt += x
		else:
			P.append([x, y])
	else:
		M.append([x, y, s])
P.sort(key=lambda x: x[1])
for (x, y) in P:
	if y > cnt:
		print('No')
		exit()
	cnt += x
if len(M) == 0:
	if cnt == 0:
		print('Yes')
	else:
		print('No')
	exit()
M.sort(reverse=True)
M.sort(key=lambda x: x[1], reverse=True)
for i in range(len(M)):
	(x, y, s) = M[i]
	if x == -y:
		g = x
		M[i] = [0, 0, '']
		break
else:
	print('No')
	exit()
for (x, y, s) in M:
	if cnt < y:
		print('No')
		exit()
	cnt += x
if cnt + g == 0:
	print('Yes')
else:
	print('No')


from itertools import chain
N = int(input())
zenhan = []
kouhan = []
for _ in range(N):
	si = input()
	last = 0
	low = last
	high = last
	for s in si:
		if s == '(':
			last += 1
		else:
			last -= 1
		low = min(low, last)
		high = max(high, last)
	if last >= 0:
		zenhan.append((-low, last, low))
	else:
		kouhan.append((low - last, last, low))
zenhan.sort()
kouhan.sort()
open_close = 0
for (key, last, low) in chain(zenhan, kouhan):
	if open_close + low < 0:
		ans = 'No'
		break
	else:
		open_close += last
else:
	if open_close:
		ans = 'No'
	else:
		ans = 'Yes'
print(ans)


from collections import deque, defaultdict
import bisect
N = int(input())
S = [input() for i in range(N)]

def bend(s):
	res = 0
	k = 0
	for th in s:
		if th == '(':
			k += 1
		else:
			k -= 1
		res = min(k, res)
	return res
species = [(bend(s), s.count('(') - s.count(')')) for s in S]
ups = [(b, u) for (b, u) in species if u > 0]
flats = [(b, u) for (b, u) in species if u == 0]
downs = [(b, u) for (b, u) in species if u < 0]
ups.sort()
high = 0
for (b, u) in ups[::-1] + flats:
	if high + b < 0:
		print('No')
		exit()
	high += u
rdowns = [(b - u, -u) for (b, u) in downs]
rdowns.sort()
high2 = 0
for (b, u) in rdowns[::-1]:
	if high2 + b < 0:
		print('No')
		exit()
	high2 += u
if high == high2:
	print('Yes')
else:
	print('No')


N = int(input())
As = []
Bs = []
for _ in range(N):
	c = 0
	m = 0
	for s in input():
		if s == '(':
			c += 1
		elif s == ')':
			c -= 1
		m = min(m, c)
	if c >= 0:
		As.append((-m, c - m))
	else:
		Bs.append((-m, c - m))
As.sort(key=lambda x: x[0])
Bs.sort(key=lambda x: x[1], reverse=True)
f = True
c = 0
for (l, r) in As:
	if c < l:
		f = False
		break
	c += r - l
if f:
	for (l, r) in Bs:
		if c < l:
			f = False
			break
		c += r - l
f = f and c == 0
if f:
	print('Yes')
else:
	print('No')


def resolve():

	def check(arr):
		height = 0
		for (b, h) in arr:
			if height + b < 0:
				return False
			height += h
		return True
	N = int(input())
	plus = []
	minus = []
	total = 0
	for _ in range(N):
		S = input()
		cum = 0
		bottom = 0
		for s in S:
			if s == '(':
				cum += 1
			else:
				cum -= 1
			bottom = min(bottom, cum)
		total += cum
		if cum > 0:
			plus.append((bottom, cum))
		else:
			minus.append((bottom - cum, -cum))
	plus.sort(reverse=True)
	minus.sort(reverse=True)
	if check(plus) and check(minus) and (total == 0):
		print('Yes')
	else:
		print('No')
resolve()


import sys
n = int(input())
a = []
b = []
for i in range(n):
	s = input()
	overall = 0
	minimum = 0
	current = 0
	for j in range(len(s)):
		if s[j] == '(':
			current += 1
		else:
			current -= 1
		minimum = min(current, minimum)
	overall = current
	if overall >= 0:
		a.append([overall, minimum])
	else:
		b.append([overall, minimum])
finalsum = 0
a = sorted(a, key=lambda t: t[1])
a.reverse()
b = sorted(b, key=lambda t: t[0] - t[1])
b.reverse()
for i in a:
	if finalsum + i[1] < 0:
		print('No')
		sys.exit()
	finalsum += i[0]
for i in b:
	if finalsum + i[1] < 0:
		print('No')
		sys.exit()
	finalsum += i[0]
if finalsum == 0:
	print('Yes')
else:
	print('No')


from itertools import chain
import sys

def main():
	N = int(input())
	S = sys.stdin.read().split('\n')
	print(solve(S))

def get_count(args):
	(s, result) = args
	cum_sum = 0
	for c in s:
		if c == ')':
			cum_sum -= 1
		else:
			cum_sum += 1
		result[0] = max(result[0], -cum_sum)
	result[1] = result[0] + cum_sum
	return result

def silly_sort(array, value_min, value_max, get_value):
	if len(array) == 0:
		return
	cache = [None for _ in range(value_max - value_min + 1)]
	for elem in array:
		value = get_value(elem) - value_min
		if cache[value] is None:
			cache[value] = []
		cache[value].append(elem)
	for values in cache:
		if values is None:
			continue
		for value in values:
			yield value

def solve(S):
	counts = [[0, 0] for _ in range(len(S))]
	counts = list(map(get_count, list(zip(S, counts))))
	first_group = []
	second_group = []
	min_first_group = float('inf')
	max_first_group = 0
	min_second_group = float('inf')
	max_second_group = 0
	for c in counts:
		if c[0] - c[1] <= 0:
			first_group.append(c)
			max_first_group = max(max_first_group, c[0])
			min_first_group = min(min_first_group, c[0])
		else:
			second_group.append(c)
			max_second_group = max(max_second_group, c[1])
			min_second_group = min(min_first_group, c[1])
	first_group = silly_sort(first_group, min_first_group, max_first_group, lambda c: c[0])
	second_group = reversed(list(silly_sort(second_group, min_second_group, max_second_group, lambda c: c[1])))
	order = chain(first_group, second_group)
	cum_sum = 0
	for c in order:
		cum_sum -= c[0]
		if cum_sum < 0:
			return 'No'
		cum_sum += c[1]
	if cum_sum == 0:
		return 'Yes'
	return 'No'

def __starting_point():
	main()
__starting_point()


import sys

def input():
	return sys.stdin.readline().rstrip()
n = int(input())
(ls, rs) = ([], [])
total = 0
for i in range(n):
	s = input()
	(h, b) = (0, 0)
	for c in s:
		if c == '(':
			h += 1
		else:
			h -= 1
		b = min(b, h)
	if h > 0:
		ls.append((b, h))
	else:
		rs.append((b - h, -h))
	total += h
ls.sort(reverse=True)
rs.sort(reverse=True)

def chk(s):
	h = 0
	for (sb, sh) in s:
		b = h + sb
		if b < 0:
			return False
		h += sh
	return True
if chk(ls) and chk(rs) and (total == 0):
	print('Yes')
else:
	print('No')


N = int(input())
C_p = []
C_m = []
for i in range(N):
	kakko = input()
	temp = 0
	temp_min = 0
	for s in kakko:
		if s == '(':
			temp += 1
		else:
			temp -= 1
			temp_min = min(temp, temp_min)
	if temp >= 0:
		C_p.append((temp_min, temp))
	else:
		C_m.append((temp_min - temp, temp_min, temp))
C_p.sort(reverse=True)
flag = 0
final = 0
for (l, f) in C_p:
	if final + l < 0:
		flag = 1
	final += f
C_m.sort()
for (_, l, f) in C_m:
	if final + l < 0:
		flag = 1
	final += f
if final != 0:
	flag = 1
print('Yes' if flag == 0 else 'No')


n = int(input())
(lr1, lr2) = ([], [])
res = 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)
	res += r - l
	if r - l >= 0:
		lr1.append((l, r))
	else:
		lr2.append((l, r))
if res != 0:
	print('No')
	exit()
lr1.sort(key=lambda x: x[0])
lr2.sort(key=lambda x: x[1])
(flg1, flg2) = (True, True)
(n1, n2) = (len(lr1), len(lr2))
tmp = 0
for i in range(n1):
	(l, r) = lr1[i]
	tmp -= l
	if tmp < 0:
		flg1 = False
	tmp += r
tmp = 0
for i in range(n2):
	(l, r) = lr2[i]
	tmp -= r
	if tmp < 0:
		flg2 = False
	tmp += l
if flg1 and flg2:
	print('Yes')
else:
	print('No')


n = int(input())
brackets_plus = []
brackets_minus = []
right = 0
left = 0
for i in range(n):
	s = input()
	cur = 0
	m = 0
	for j in range(len(s)):
		if s[j] == '(':
			left += 1
			cur += 1
		else:
			right += 1
			cur -= 1
			m = min(m, cur)
	if cur >= 0:
		brackets_plus.append((m, cur))
	else:
		brackets_minus.append((m, cur, m - cur))
if right != left:
	print('No')
	exit()
cur = 0
brackets_plus.sort(reverse=True)
for i in brackets_plus:
	if i[0] + cur < 0:
		print('No')
		exit()
	cur += i[1]
brackets_minus.sort(key=lambda x: x[2])
for i in brackets_minus:
	if i[0] + cur < 0:
		print('No')
		exit()
	cur += i[1]
print('Yes')


def check(vec):
	if len(vec) == 0:
		return True
	h = 0
	for v in sorted(vec, reverse=True):
		if h + v[0] < 0:
			return False
		h += v[1]
	return True

def parse(brackets):
	m = 0
	f = 0
	for c in brackets:
		if c == '(':
			f += 1
		else:
			f -= 1
		if m > f:
			m = f
	return (m, f)

def main():
	N = int(input())
	ls = []
	rs = []
	tot = 0
	for _ in range(N):
		(m, f) = parse(input().strip())
		tot += f
		if f >= 0:
			ls.append([m, f])
		else:
			rs.append([m - f, -f])
	return tot == 0 and check(ls) and check(rs)

def __starting_point():
	print('Yes' if main() else 'No')
__starting_point()


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:
				if stack[-1] == ')':
					stack.append('(')
					l = 1
				else:
					stack.append('(')
					l += 1
			else:
				stack.append('(')
				l = 1
		elif stack:
			if stack[-1] == '(':
				stack.pop()
				l -= 1
			else:
				stack.append(')')
				r += 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, r)
		min_revl = min(min_revl, l)
if min_revr == 10 ** 20:
	min_revr = 0
if min_revl == 10 ** 20:
	min_revl = 0
if left + rev_l == rev_r + right and left >= min_revr and (right >= min_revl):
	print('Yes')
else:
	print('No')


def kton(S):
	r = 0
	m = 0
	for s in S:
		r += 1 if s == '(' else -1
		m = min(m, r)
	return (r, m)

def main():
	N = int(input())
	RM = [kton(input()) for _ in range(N)]
	pos = 0
	negp = []
	negn = []
	posn = []
	for (r, m) in RM:
		if m < 0:
			if r >= 0:
				negp.append((-m, r))
			else:
				negn.append((-(r - m), -r, m))
		else:
			pos += r
	negp.sort()
	for (m, r) in negp:
		if pos - m < 0:
			return False
		pos += r
	negn.sort()
	for (_, r, m) in negn:
		if pos + m < 0:
			return False
		pos -= r
	return pos == 0
print('Yes' if main() else 'No')


n = int(input())
s = [list(input()) for i in range(n)]
r = []
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
	elif now >= 0:
		l.append([c, now])
	else:
		r.append([c - now, now])
l.sort(reverse=True)
r.sort()
for (i, j) in l:
	if ans + i < 0:
		print('No')
		exit()
	else:
		ans += j
for (i, j) in r:
	if ans + i + j < 0:
		print('No')
		exit()
	else:
		ans += j
if ans == 0:
	print('Yes')
else:
	print('No')


def f():
	N = int(input())
	UP = []
	DOWN = []
	for _ in range(N):
		S = input()
		c = 0
		minC = 0
		for s in S:
			if s == '(':
				c += 1
			else:
				c -= 1
				minC = min(minC, c)
		if c >= 0:
			UP.append((minC, c))
		else:
			DOWN.append((c - minC, c))
	c = 0
	for up in sorted(UP, reverse=True):
		if c + up[0] < 0:
			return False
		c += up[1]
	for down in sorted(DOWN, reverse=True):
		if c + down[1] - down[0] < 0:
			return False
		c += down[1]
	if c != 0:
		return False
	return True
if f():
	print('Yes')
else:
	print('No')


import sys
input = lambda : sys.stdin.readline().rstrip()
n = int(input())
s = [input() for _ in range(n)]

def bracket(x):
	f = m = 0
	for i in range(len(x)):
		if x[i] == '(':
			f += 1
		else:
			f -= 1
		m = min(m, f)
	return (f, m)

def func(l):
	l.sort(key=lambda x: -x[1])
	v = 0
	for (fi, mi) in l:
		if v + mi >= 0:
			v += fi
		else:
			return -1
	return v
l1 = []
l2 = []
for i in range(n):
	(fi, mi) = bracket(s[i])
	if fi >= 0:
		l1.append((fi, mi))
	else:
		l2.append((-fi, mi - fi))
v1 = func(l1)
v2 = func(l2)
if v1 == -1 or v2 == -1:
	ans = 'No'
else:
	ans = 'Yes' if v1 == v2 else 'No'
print(ans)


import sys
n = int(input())
s = [list(input()) for i in range(n)]
L1 = []
L2 = []
ct1 = 0
ct2 = 0
for i in range(n):
	ct3 = 0
	l = [0]
	for j in range(len(s[i])):
		if s[i][j] == '(':
			ct1 += 1
			ct3 += 1
			l.append(ct3)
		else:
			ct2 += 1
			ct3 -= 1
			l.append(ct3)
	if l[-1] >= 0:
		L1.append((min(l), l[-1]))
	else:
		L2.append((min(l) - l[-1], -l[-1]))
if ct1 != ct2:
	print('No')
	sys.exit()
L1.sort()
L1.reverse()
ct4 = 0
for i in range(len(L1)):
	if ct4 + L1[i][0] < 0:
		print('No')
		sys.exit()
	ct4 += L1[i][1]
L2.sort()
L2.reverse()
ct5 = 0
for i in range(len(L2)):
	if ct5 + L2[i][0] < 0:
		print('No')
		sys.exit()
	ct5 += L2[i][1]
print('Yes')


import sys
input = sys.stdin.readline
N = int(input())
css = [0] * N
mns = [0] * N
t = [x for x in range(N)]
for x in range(N):
	S = list(input())[:-1]
	for i in range(len(S)):
		if S[i] == '(':
			css[x] += 1
		if S[i] == ')':
			css[x] -= 1
			mns[x] = min(mns[x], css[x])
if sum(css):
	print('No')
	exit(0)
t.sort(key=lambda x: -10 ** 7 if mns[x] == 0 else -10 ** 7 - mns[x] if css[x] >= 0 else mns[x] - css[x])
l = 0
for i in t:
	if l + mns[i] < 0:
		print('No')
		exit(0)
	l += css[i]
print('Yes')


import sys

def parse(s):
	bottom = 0
	delta = 0
	for c in s:
		delta += 1 if c == '(' else -1
		bottom = min(bottom, delta)
	return (bottom, delta)

def compute(pars):
	pars.sort(reverse=True)
	h = 0
	for (bottom, delta) in pars:
		if h + bottom < 0:
			print('No')
			return
		h += delta
	return h

def main():
	N = int(input())
	incs = []
	decs = []
	for _ in range(N):
		(bottom, delta) = parse(input())
		if delta > 0:
			incs.append((bottom, delta))
		else:
			decs.append((bottom - delta, -delta))
	inc_tot = compute(incs)
	dec_tot = compute(decs)
	if inc_tot == dec_tot:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	a = []
	b = []
	for i in range(n):
		s = input()
		(c1, c2) = (0, 0)
		for i in range(len(s)):
			if s[i] == '(':
				c2 += 1
			if s[i] == ')':
				if c2:
					c2 -= 1
				else:
					c1 += 1
		if c1 >= c2:
			b.append((c2, c1))
		else:
			a.append((c1, c2))
	a.sort()
	b.sort(reverse=True)
	ans = False
	sub = 0
	for value in a:
		(k1, k2) = (value[0], value[1])
		if sub < k1:
			ans = True
			break
		sub += k2 - k1
	for value in b:
		(k2, k1) = (value[0], value[1])
		if ans or sub < k1:
			ans = True
			break
		sub += k2 - k1
	print('No' if ans or sub else 'Yes')
main()


n = int(input())
S = [input() for i in range(n)]
AB = []
for i in range(n):
	(a, b) = (0, 0)
	for c in S[i]:
		if c == '(':
			b += 1
		if c == ')':
			if b > 0:
				b -= 1
			else:
				a += 1
	AB.append((a, b))
AB0 = [(a, b) for (a, b) in AB if -a + b >= 0]
AB1 = [(a, b) for (a, b) in AB if -a + b < 0]
AB0.sort()
AB1.sort(key=lambda x: -x[1])
AB = AB0 + AB1
x = 0
for (a, b) in AB:
	x -= a
	if x < 0:
		print('No')
		exit()
	x += b
if x == 0:
	print('Yes')
else:
	print('No')


def check(pt, brackets):
	brackets.sort(reverse=True)
	for (m, t) in brackets:
		if pt + m < 0:
			return -1
		pt += t
	return pt
n = int(input())
brackets_p = []
brackets_m = []
pt = 0
mt = 0
for _ in range(n):
	s = input()
	total = 0
	mini = 0
	for c in s:
		if c == '(':
			total += 1
		else:
			total -= 1
			mini = min(mini, total)
	if total >= 0:
		if mini == 0:
			pt += total
		else:
			brackets_p.append((mini, total))
	else:
		(total, mini) = (-total, mini - total)
		if mini == 0:
			mt += total
		else:
			brackets_m.append((mini, total))
pt = check(pt, brackets_p)
mt = check(mt, brackets_m)
if pt == -1 or mt == -1 or pt != mt:
	print('No')
else:
	print('Yes')


N = int(input())
ls = []
rs = []
tot = 0
for i in range(N):
	s = input()
	h = 0
	b = 0
	for c in s:
		if c == '(':
			h += 1
		else:
			h -= 1
		b = min(b, h)
	if h >= 0:
		ls.append([b, h])
	else:
		rs.append([b - h, -h])
	tot += h
ls.sort(key=lambda x: x[0], reverse=True)
rs.sort(key=lambda x: x[0], reverse=True)

def check(s):
	h = 0
	for p in s:
		if h + p[0] < 0:
			return False
		h += p[1]
	return True
if check(ls) and check(rs) and (tot == 0):
	print('Yes')
else:
	print('No')


N = int(input())
mem = [0] * N
for i in range(N):
	s = input()
	x = 0
	y = 0
	for j in range(len(s)):
		if s[j] == '(':
			x += 1
		else:
			x -= 1
			y = min(x, y)
	mem[i] = (x, y)
mem_a = sorted([ele for ele in mem if ele[0] > 0], key=lambda x: -x[1])
mem_b = sorted([ele for ele in mem if ele[0] <= 0], key=lambda x: x[1] - x[0])
ans = True
val = 0
for ele in mem_a:
	if val + ele[1] < 0:
		ans = False
	val += ele[0]
for ele in mem_b:
	if val + ele[1] < 0:
		ans = False
	val += ele[0]
YesNo = lambda tf: print('Yes') if tf else print('No')
YesNo(ans and val == 0)


def main():
	import sys
	from collections import deque
	sys.setrecursionlimit(10 ** 6)
	input = sys.stdin.readline
	N = int(input())
	pq = deque([])
	mq = deque([])
	for n in range(N):
		s = input().strip()
		mini = 0
		dst = 0
		for ss in [1 if ss == '(' else -1 for ss in s]:
			dst += ss
			mini = min(dst, mini)
		if dst >= 0:
			pq.append((mini, dst))
		elif dst < 0:
			mq.append((mini - dst, mini, dst))
	pq = sorted(pq, reverse=True)
	mq = sorted(mq)
	p = 0
	for (mini, dst) in pq:
		if p + mini < 0:
			print('No')
			return
		p += dst
	for (minimini, mini, dst) in mq:
		if p + mini < 0:
			print('No')
			return
		p += dst
	if p != 0:
		print('No')
	else:
		print('Yes')

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
n = int(input())
sp = []
sn = []
for i in range(n):
	s = input().rstrip()
	m = 0
	temp = 0
	for j in s:
		if j == '(':
			temp += 1
		else:
			temp -= 1
		m = min(m, temp)
	m = -m
	if temp >= 0:
		sp.append((m, temp))
	else:
		sn.append((m + temp, -temp))
sp.sort()
sn.sort()
vp = 0
for i in sp:
	if vp - i[0] < 0:
		print('No')
		sys.exit()
	else:
		vp += i[1]
vn = 0
for i in sn:
	if vn - i[0] < 0:
		print('No')
		sys.exit()
	else:
		vn += i[1]
if vp == vn:
	print('Yes')
else:
	print('No')


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

def solve():
	T = []
	for s in S:
		open = 0
		close = 0
		for c in s:
			if c == ')':
				if open > 0:
					open -= 1
				else:
					close += 1
			else:
				open += 1
		T.append((open, close))
	if sum((op - cl for (op, cl) in T)) != 0:
		return 'No'
	inc = []
	dec = []
	for (op, cl) in T:
		if op >= cl:
			inc.append((cl, op))
		else:
			dec.append((op, cl))
	inc.sort()
	open = 0
	for (cl, op) in inc:
		if open >= cl:
			open += op - cl
		else:
			return 'No'
	close = 0
	dec.sort()
	for (op, cl) in dec:
		if close >= op:
			close += cl - op
		else:
			return 'No'
	return 'Yes'
print(solve())


n = int(input())
ara = [input() for _ in range(n)]
pref = []
suff = []
for s in ara:
	pos = neg = 0
	for ch in s:
		if ch == '(':
			pos += 1
		elif pos == 0:
			neg += 1
		else:
			pos -= 1
	if pos >= neg:
		pref.append([pos, neg])
	else:
		suff.append([pos, neg])
pref.sort(key=lambda x: x[1])
suff.sort(key=lambda x: x[0], reverse=True)
totPos = 0
ara = pref + suff
for (pos, neg) in ara:
	if neg > totPos:
		print('No')
		exit(0)
	totPos -= neg
	totPos += pos
if totPos != 0:
	print('No')
else:
	print('Yes')


n = int(input())
A = []
for i in range(n):
	s = input()
	l = 0
	o = 0
	for c in s:
		if c == '(':
			l += 1
		else:
			l -= 1
			o = min(o, l)
	A.append((l, o))
A.sort(key=lambda x: -10 ** 9 if x[1] == 0 else -x[1] if x[0] >= 0 else 10 ** 7 + x[1] - x[0])
last = 0
for (l, o) in A:
	if last + o < 0:
		print('No')
		exit(0)
	last += l
print('Yes' if last == 0 else 'No')


def main():
	n = int(input())
	l_0 = []
	r_0 = []
	ls_plus = []
	ls_minus = []
	sum_l = 0
	sum_r = 0
	for i in range(n):
		s = input()
		(left, right) = (0, 0)
		for j in range(len(s)):
			if s[j] == '(':
				right += 1
			elif right > 0:
				right -= 1
			else:
				left += 1
		if left == right == 0:
			continue
		if left == 0:
			l_0.append((left, right))
		elif right == 0:
			r_0.append((left, right))
		elif left < right:
			ls_plus.append((left, right))
		else:
			ls_minus.append((left, right))
		sum_l += left
		sum_r += right
	if len(ls_plus) == len(ls_minus) == len(l_0) == len(r_0) == 0:
		print('Yes')
		return
	if len(l_0) == 0 or len(r_0) == 0:
		print('No')
		return
	if sum_l != sum_r:
		print('No')
		return
	ls_plus.sort(key=lambda x: x[1] - x[0], reverse=True)
	ls_plus.sort(key=lambda x: x[0])
	ls_minus.sort(key=lambda x: x[0] - x[1])
	ls_minus.sort(key=lambda x: x[0], reverse=True)
	now_r = 0
	for ll in l_0:
		now_r += ll[1]
	for _ in ls_plus:
		r = _[1]
		x = now_r - _[0]
		if x >= 0:
			now_r = x + r
		else:
			print('No')
			return
	for _ in ls_minus:
		r = _[1]
		x = now_r - _[0]
		if x >= 0:
			now_r = x + r
		else:
			print('No')
			return
	print('Yes')
main()


import sys
n = int(input())
(x, y) = ([], [])
for _i in range(n):
	s = input()
	p = 0
	m = 0
	for j in range(len(s)):
		if s[j] == '(':
			p += 1
		else:
			p -= 1
			(m, _m) = sorted([p, m])
	if p > 0:
		x.append([m, p])
	else:
		y.append([m - p, -p])
x.sort(reverse=True)
y.sort(reverse=True)
r = 0
for (s, t) in x:
	if r + s < 0:
		print('No')
		sys.exit()
	r += t
result = 0
for (s, t) in y:
	if result + s < 0:
		print('No')
		sys.exit()
	result += t
if r - result == 0:
	print('Yes')
else:
	print('No')


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

def calc(T):
	now = 0
	ret = 0
	for s in T:
		if s == '(':
			now += 1
		elif s == ')':
			now -= 1
		ret = min(ret, now)
	return ret
H = []
P = []
L = []
for s in S:
	less = -calc(s)
	if less == 0:
		H.append(s)
	else:
		l = s.count('(')
		r = len(s) - l
		if l >= r:
			P.append((less, s))
		else:
			L.append((less - (r - l), s))
P.sort(key=lambda a: a[0])
L.sort(key=lambda a: a[0], reverse=True)
P = [s for (_, s) in P]
L = [s for (_, s) in L]
ans = ''.join(H) + ''.join(P) + ''.join(L)
l = ans.count('(')
r = len(ans) - l
if l != r or calc(ans) < 0:
	print('No')
else:
	print('Yes')


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
n = int(readline())
inc = []
dec = []
for _ in range(n):
	s = readline().rstrip().decode()
	(d, r) = (0, 0)
	for m in s:
		d += 1 if m == '(' else -1
		r = max(r, -d)
	(dec if d < 0 else inc).append((d, r))
inc.sort(key=lambda x: x[1])
dec.sort(key=lambda x: x[0] + x[1])
p1 = 0
p2 = 0
flag = True
for i in inc:
	flag &= i[1] <= p1
	p1 += i[0]
for d in dec:
	flag &= p2 >= d[0] + d[1]
	p2 -= d[0]
flag &= p1 == p2
print('Yes' if flag else 'No')


def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	plus = []
	minus = []
	total = 0
	for _ in range(N):
		s = input().strip()
		h = 0
		bottom = 0
		for ss in s:
			if ss == '(':
				h += 1
			else:
				h -= 1
				bottom = min(bottom, h)
		total += h
		if h > 0:
			plus.append((bottom, h))
		else:
			minus.append((bottom - h, -h))
	if total != 0:
		print('No')
		sys.exit()
	plus.sort(reverse=True)
	minus.sort(reverse=True)
	height = 0
	for (b, h) in plus:
		if height + b < 0:
			print('No')
			sys.exit()
		height += h
	height = 0
	for (b, h) in minus:
		if height + b < 0:
			print('No')
			sys.exit()
		height += h
	print('Yes')
main()


import sys
N = int(input())
dif = [0] * N
mini = [0] * N
for _ in range(N):
	S = input()
	m = 0
	now = 0
	for i in range(len(S)):
		if S[i] == '(':
			now += 1
		else:
			now -= 1
		m = min(m, now)
	dif[_] = now
	mini[_] = m
sum = 0
for i in range(N):
	sum += dif[i]
if sum != 0:
	print('No')
	sys.exit()
plus = []
minus = []
for i in range(N):
	if dif[i] >= 0:
		plus.append((mini[i], dif[i]))
	else:
		minus.append((dif[i] - mini[i], mini[i], dif[i]))
plus.sort(reverse=True)
minus.sort(reverse=True)
now = 0
for i in range(len(plus)):
	if now + plus[i][0] < 0:
		print('No')
		sys.exit()
	now += plus[i][1]
for i in range(len(minus)):
	if now + minus[i][1] < 0:
		print('No')
		sys.exit()
	now += minus[i][2]
print('Yes')


import sys
n = int(input())
ss = []
for _ in range(n):
	ss.append(input())
cnt_p = []
cnt_m = []
for i in range(n):
	left = 0
	right = 0
	for s in ss[i]:
		if s == '(':
			left += 1
		elif s == ')':
			if left > 0:
				left -= 1
			else:
				right += 1
	if left - right > 0:
		cnt_p.append((right, left))
	else:
		cnt_m.append((right, left))
cnt_p = sorted(cnt_p, key=lambda x: x[0])
cnt_m = sorted(cnt_m, key=lambda x: x[1] * -1)
cnt = cnt_p + cnt_m
num = 0
for (right, left) in cnt:
	num -= right
	if num < 0:
		print('No')
		sys.exit()
	num += left
if num == 0:
	print('Yes')
else:
	print('No')


n = int(input())
left = []
mid = []
midminus = []
right = []
L = []
R = []
for i in range(n):
	s = input()
	l = 0
	r = 0
	for x in s:
		if x == '(':
			l += 1
		elif l > 0:
			l -= 1
		else:
			r += 1
	if l > 0 and r == 0:
		left.append((l, r))
	elif l > 0 and r > 0:
		if l > r:
			mid.append((r, l))
		else:
			midminus.append((r, l))
	elif l == 0 and r > 0:
		right.append((l, r))
	L.append(l)
	R.append(r)
if sum(L) != sum(R):
	print('No')
	exit()
A = 0
B = 0
for x in left:
	A += x[0]
for x in right:
	B += x[1]
mid = sorted(mid, key=lambda x: (x[0], -x[1]))
midminus = sorted(midminus, key=lambda x: x[0] - x[1])
mid += midminus
l = A
r = 0
for (a, b) in mid:
	if l < a:
		print('No')
		exit()
	l -= a
	l += b
print('Yes')


def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	S = [input().rstrip() for _ in range(N)]
	neg = []
	pos = []
	total = 0
	for (i, s) in enumerate(S):
		t = 0
		tmn = 0
		for c in s:
			if c == '(':
				t += 1
			else:
				t -= 1
			tmn = min(tmn, t)
		if t >= 0:
			pos.append((tmn, t))
		else:
			neg.append((tmn - t, -t))
		total += t
	if total != 0:
		print('No')
		return
	t = 0
	for (tmn, tsm) in sorted(pos, reverse=True, key=lambda x: x[0]):
		if t + tmn < 0:
			print('No')
			return
		t += tsm
	t = 0
	for (tmn, tsm) in sorted(neg, reverse=True, key=lambda x: x[0]):
		if t + tmn < 0:
			print('No')
			return
		t += tsm
	print('Yes')
main()


import sys
readline = sys.stdin.readline

def main():
	N = int(readline())
	Sp = []
	Sm = []
	total = 0
	bm = 0
	for i in range(N):
		l = readline().strip()
		b = 0
		h = 0
		for j in range(len(l)):
			if l[j] == '(':
				h += 1
			else:
				h -= 1
			b = min(b, h)
		if h == 0 and b != 0:
			bm = min(bm, b)
		elif h > 0:
			Sp.append([b, h])
		elif h < 0:
			Sm.append([b - h, -h])
		total += h
	Sp.append([bm, 0])
	if total != 0:
		print('No')
		exit()
	Sp.sort(key=lambda x: x[0], reverse=True)
	p = check(Sp)
	if p < 0:
		print('No')
		exit()
	Sm.sort(key=lambda x: x[0], reverse=True)
	m = check(Sm)
	if m < 0:
		print('No')
		exit()
	print('Yes')

def check(S):
	h = 0
	for i in range(len(S)):
		if h + S[i][0] < 0:
			return -1
		else:
			h += S[i][1]
	return h
main()


from sys import exit
from itertools import accumulate, chain
(n, *s) = open(0).read().split()
t = [2 * i.count('(') - len(i) for i in s]
if sum(t) != 0:
	print('No')
	exit()
st = [[min(accumulate(s_, lambda a, b: a + (1 if b == '(' else -1), initial=0)), t_] for (s_, t_) in zip(s, t)]
now = 0
for c in chain(sorted([x for x in st if x[1] >= 0])[::-1], sorted([x for x in st if x[1] < 0], key=lambda z: z[1] - z[0])[::-1]):
	if now + c[0] < 0:
		print('No')
		exit()
	now += c[1]
print('Yes')


import sys
input = sys.stdin.readline
n = int(input())
cnt = 0
decrement = 0
profit = []
loss = []
for _ in range(n):
	s = input().rstrip()
	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
	elif min_p == end_p:
		decrement += end_p
	elif 0 <= end_p:
		profit.append((min_p, end_p))
	else:
		loss.append((min_p, end_p))
profit.sort()
while profit:
	(min_p, end_p) = profit.pop()
	if cnt + min_p < 0:
		print('No')
		return
	cnt += end_p
loss.sort(reverse=True)
while loss:
	(min_p, end_p) = loss.pop()
	if cnt + min_p < 0:
		print('No')
		return
	cnt += end_p
print('Yes' if cnt + decrement == 0 else 'No')


def main():
	n = int(input())
	S = [input() for _ in [0] * n]
	s2 = []
	s3 = []
	for s in S:
		temp = [0, 0]
		m = 0
		now = 0
		for i in s:
			if i == '(':
				now += 1
			else:
				now -= 1
			m = min(m, now)
		temp[0] = -m
		temp[1] = s.count('(') - s.count(')') - m
		if temp[0] < temp[1]:
			s2.append(temp)
		else:
			s3.append(temp)
	s2.sort(key=lambda x: x[0])
	s3.sort(key=lambda x: -x[1])
	cnt = 0
	for (i, j) in s2:
		cnt -= i
		if cnt < 0:
			print('No')
			return
		cnt += j
	for (i, j) in s3:
		cnt -= i
		if cnt < 0:
			print('No')
			return
		cnt += j
	if cnt != 0:
		print('No')
		return
	print('Yes')
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')
main()


n = int(input())
s = [input() for i in range(n)]
(p, m) = ([], [])
res = 0
for i in range(n):
	lr = 0
	min_ = 0
	for c in s[i]:
		if c == '(':
			lr += 1
		else:
			lr -= 1
		min_ = min(min_, lr)
	if lr >= 0:
		p.append([lr, min_])
	else:
		m.append([lr, min_])
	res += lr
if res != 0:
	print('No')
	exit()
p.sort(key=lambda x: x[1], reverse=True)
m.sort(key=lambda x: x[0] - x[1], reverse=True)
cum = 0
for (x, y) in p:
	if cum + y < 0:
		print('No')
		exit()
	cum += x
for (x, y) in m:
	if cum + y < 0:
		print('No')
		exit()
	cum += x
print('Yes')


import sys
input = sys.stdin.readline
n = int(input())
s = [input().strip('\n') for _ in range(n)]
p = []
m = []
for si in s:
	cnt = 0
	a = 0
	b = 0
	for i in range(len(si)):
		if si[i] == ')':
			cnt += 1
		else:
			cnt -= 1
		a = max(a, cnt)
	cnt = 0
	for i in range(len(si) - 1, -1, -1):
		if si[i] == '(':
			cnt += 1
		else:
			cnt -= 1
		b = max(b, cnt)
	if b - a >= 0:
		p.append((a, si))
	else:
		m.append((-b, si))
p.sort()
m.sort()
ss = ''
for (_, si) in p:
	ss += si
for (_, si) in m:
	ss += si
cnt = 0
for i in range(len(ss)):
	if ss[i] == '(':
		cnt += 1
	else:
		cnt -= 1
	if cnt < 0:
		print('No')
		exit()
if cnt:
	print('No')
else:
	print('Yes')


import sys
readline = sys.stdin.readline

def resolve():
	N = int(readline())
	S = [readline().strip() for i in range(N)]
	(L, R) = ([], [])
	D = []
	for s in S:
		(l, r) = (0, 0)
		for c in s:
			if c == ')':
				if l > 0:
					l -= 1
				else:
					r += 1
			else:
				l += 1
		if l == 0 and r == 0:
			pass
		elif l == 0:
			R.append(r)
		elif r == 0:
			L.append(l)
		else:
			D.append((l, r))
	L = sum(L)
	R = sum(R)
	inc = []
	dec = []
	for (l, r) in D:
		if l > r:
			inc.append((l, r))
		else:
			dec.append((l, r))
	inc.sort(key=lambda x: x[1])
	dec.sort(key=lambda x: -x[1])
	D = inc + dec
	for (i, (l, r)) in enumerate(D):
		L -= r
		if L < 0:
			print('No')
			return
		L += l
	if L == R:
		print('Yes')
	else:
		print('No')
resolve()


N = int(input())
S_s = [input() for _ in range(N)]
S_data1 = []
S_data2 = []
for S in S_s:
	life = 0
	A = 0
	for c in S:
		if c == '(':
			life += 1
		elif life > 0:
			life -= 1
		else:
			A += 1
	if A < life:
		S_data1.append(tuple([A, life]))
	else:
		S_data2.append(tuple([A, life]))
S_data1.sort()
S_data2 = sorted(S_data2, key=lambda x: -x[1])
S_data1.extend(S_data2)
S_data = tuple(S_data1)
life = 0
answer = True
for S_datum in S_data:
	life -= S_datum[0]
	if life < 0:
		answer = False
		break
	life += S_datum[1]
else:
	if life != 0:
		answer = False
if answer:
	print('Yes')
else:
	print('No')


n = int(input())
slist = []
for i in range(n):
	slist.append(input())
first_list = []
last_list = []
first = ''
last = ''
for i in range(n):
	s = slist[i]
	a_count = 0
	b_count = 0
	min_s = ''
	for c in s:
		if c == ')':
			b_count -= 1
		else:
			b_count += 1
		if b_count < 0:
			b_count = 0
			a_count += 1
	min_s = ')' * a_count + '(' * b_count
	ai = a_count
	bi = b_count
	if ai == 0:
		first += min_s
	elif bi == 0:
		last += min_s
	elif bi - ai >= 0:
		first_list.append([min_s, ai])
	else:
		last_list.append([min_s, bi])
first_list = sorted(first_list, key=lambda s: s[1])
last_list = sorted(last_list, key=lambda s: s[1], reverse=True)
ans = first
for i in range(len(first_list)):
	ans += first_list[i][0]
for i in range(len(last_list)):
	ans += last_list[i][0]
ans += last
a_count = 0
b_count = 0
min_s = ''
for c in ans:
	if c == ')':
		b_count -= 1
	else:
		b_count += 1
	if b_count < 0:
		b_count = 0
		a_count += 1
if a_count == 0 and b_count == 0:
	print('Yes')
else:
	print('No')


import sys
import heapq
input = sys.stdin.readline
N = int(input())
forward = []
backward = []
for _ in range(N):
	pos = 0
	Min = 0
	for c in input()[:-1]:
		if c == '(':
			pos += 1
		else:
			pos -= 1
			Min = min(Min, pos)
	if pos >= 0:
		heapq.heappush(forward, (-Min, pos))
	else:
		heapq.heappush(backward, (pos - Min, -pos))
ansflg = True
pos1 = 0
while forward:
	(down, pos) = heapq.heappop(forward)
	if pos1 >= down:
		pos1 += pos
	else:
		ansflg = False
		break
pos2 = 0
if ansflg:
	while backward:
		(down, pos) = heapq.heappop(backward)
		if pos2 >= down:
			pos2 += pos
		else:
			ansflg = False
			break
if pos1 != pos2:
	ansflg = False
if ansflg:
	print('Yes')
else:
	print('No')


n = int(input())
pos = []
neg = []
for i in range(n):
	s = input()
	bal = 0
	mn = 0
	for ch in s:
		if ch == '(':
			bal += 1
		if ch == ')':
			bal -= 1
		mn = min(mn, bal)
	if -mn < bal - mn:
		pos.append((-mn, bal - mn))
	else:
		neg.append((-mn, bal - mn))

def cmp(it):
	return -it[1]
pos.sort()
neg.sort(key=cmp)
bal = 0
ok = True
for (m, b) in pos:
	if bal - m < 0:
		ok = False
	bal += b - m
for (m, b) in neg:
	if bal - m < 0:
		ok = False
	bal += b - m
if bal != 0:
	ok = False
if ok:
	print('Yes')
else:
	print('No')


n = int(input())
d = []
e = []
for i in range(n):
	p = [0, 0]
	s = input()
	for j in s:
		if j == ')':
			if p[0] != 0:
				p[0] -= 1
			else:
				p[1] += 1
		else:
			p[0] += 1
	if p[0] - p[1] >= 0:
		d.append(p)
	else:
		e.append(p)
d = sorted(d, key=lambda x: x[1])
e = sorted(e, key=lambda x: -x[0])
s = d + e
q = 0
for (i, j) in s:
	if q < j:
		print('No')
		exit()
	else:
		q = q - j + i
if q == 0:
	print('Yes')
else:
	print('No')


N = int(input())
(G2, G4) = ([], [])
cnt2 = 0
cnt3 = 0
for _ in range(N):
	S = input()
	MIN = 0
	cnt = 0
	for s in list(S):
		if s == '(':
			cnt += 1
		else:
			cnt -= 1
		MIN = min(MIN, cnt)
	if MIN == 0 and cnt >= 0:
		cnt2 += cnt
	if MIN < 0 and cnt >= 0:
		G2.append([MIN, cnt])
	if MIN == cnt and cnt < 0:
		cnt3 += cnt
	if MIN < cnt and cnt < 0:
		G4.append([MIN, cnt])
G2.sort(reverse=True)
for L in G2:
	if cnt2 + L[0] < 0:
		print('No')
		return
	else:
		cnt2 += L[1]
for L in G4:
	if cnt2 + L[0] < 0:
		print('No')
		return
	else:
		cnt2 += L[1]
cnt2 += cnt3
if cnt2 == 0:
	print('Yes')
else:
	print('No')


def main():
	import sys
	input = sys.stdin.readline
	sys.setrecursionlimit(10 ** 7)
	from collections import Counter, deque
	from itertools import combinations, permutations, accumulate, groupby, product
	from bisect import bisect_left, bisect_right
	from heapq import heapify, heappop, heappush
	import math
	n = int(input())
	a = []
	b = []
	for _ in range(n):
		s = input().rstrip()
		(up, down) = (0, 0)
		for i in s:
			if i == ')':
				up -= 1
				if down > up:
					down = up
			else:
				up += 1
		if up >= 0:
			a.append((down, up))
		else:
			b.append((down - up, -up))
	a.sort(key=lambda a: a[0], reverse=True)
	b.sort(key=lambda a: a[0], reverse=True)
	(left, right) = (0, 0)
	for (d, u) in a:
		if left + d < 0:
			print('No')
			break
		else:
			left += u
	else:
		for (d, u) in b:
			if right + d < 0:
				print('No')
				break
			else:
				right += u
		else:
			if left == right:
				print('Yes')
			else:
				print('No')

def __starting_point():
	main()
__starting_point()


def solve():
	n = int(input())
	s = [input() for _ in range(n)]
	l = []
	for si in s:
		lparen = rparen = 0
		for c in si:
			if c == '(':
				lparen += 1
			elif lparen > 0:
				lparen -= 1
			else:
				rparen += 1
		l.append((rparen, lparen))
	inc = []
	dec = []
	for (p, q) in l:
		if p < q:
			inc.append((p, q))
		else:
			dec.append((p, q))
	inc.sort()
	k = 0
	for (p, q) in inc:
		if k < p:
			print('No')
			return
		k = k - p + q
	dec.sort(key=lambda x: x[1])
	t = 0
	for (p, q) in dec:
		if t < q:
			print('No')
			return
		t = t - q + p
	if k == t:
		print('Yes')
	else:
		print('No')
	return
solve()


n = int(input())
(sp, sm) = ([], [])
res = 0
for _ in range(n):
	s = input()
	m = len(s)
	(l, tl) = (0, 0)
	for i in range(m):
		if s[i] == ')':
			tl -= 1
		else:
			tl += 1
		l = min(l, tl)
	(r, tr) = (0, 0)
	for i in range(m):
		if s[m - 1 - i] == '(':
			tr -= 1
		else:
			tr += 1
		r = min(r, tr)
	res += tl
	if tl >= 0:
		sp.append((l, tl))
	else:
		sm.append((r, tr))
if res != 0:
	print('No')
	exit()
sp.sort(key=lambda x: -x[0])
sm.sort(key=lambda x: -x[0])
xp = len(sp)
cur = 0
for (l, tl) in sp:
	if cur + l < 0:
		print('No')
		exit()
	cur += tl
cur = 0
for (r, tr) in sm:
	if cur + r < 0:
		print('No')
		exit()
	cur += tr
print('Yes')


import sys
read = sys.stdin.read
readline = sys.stdin.readline
n = int(input())
s = read().split()
dp = []
dn = []
for i in s:
	a = 0
	d = 0
	for j in i:
		if j == '(':
			d += 1
		else:
			d -= 1
		if d < a:
			a = d
	if d >= 0:
		dp.append((a, d))
	else:
		dn.append((a - d, -d))
dp.sort()
dn.sort()

def check(dp):
	D = 0
	while dp:
		(a, d) = dp.pop()
		if D + a < 0:
			return False
		D += d
	return [D]
a = check(dp)
b = check(dn)
if a == False or b == False:
	print('No')
elif a[0] != b[0]:
	print('No')
else:
	print('Yes')


def f(s):
	m = 0
	n = 0
	for i in s:
		if i == ')':
			n -= 1
		else:
			n += 1
		m = min(m, n)
	return [m, n]
n = int(input())
s = [input() for _ in range(n)]
mab = []
pab = []
for i in range(n):
	ab = f(s[i])
	if sum(ab) > 0:
		pab.append(ab)
	else:
		mab.append(ab)
pab.sort(reverse=1)
mab.sort(key=lambda x: x[0] - x[1])
ab = pab + mab
n = 0
for (a, b) in ab:
	if n + a < 0:
		exit(print('No'))
	n += b
	if n < 0:
		exit(print('No'))
if n == 0:
	print('Yes')
else:
	print('No')


import sys
input = sys.stdin.readline

def check(s):
	h = 0
	for p in s:
		b = h + p[0]
		if b < 0:
			return False
		h += p[1]
	return True
N = int(input())
S = [input().strip() for i in range(N)]
total = 0
(ps, ms) = ([], [])
for s in S:
	(h, b) = (0, 0)
	for c in s:
		if c == '(':
			h += 1
		else:
			h -= 1
		b = min(b, h)
	if h > 0:
		ps.append([b, h])
	else:
		ms.append([b - h, -h])
	total += h
ps.sort(reverse=True)
ms.sort(reverse=True)
if check(ps) and check(ms) and (total == 0):
	print('Yes')
else:
	print('No')


import sys
input = sys.stdin.readline
n = int(input())
cnt = []
for i in range(n):
	s = input().rstrip()
	l = s.count('(')
	r = s.count(')')
	minus = 0
	now = 0
	for j in range(len(s)):
		if s[j] == '(':
			now += 1
		else:
			now -= 1
			minus = min(minus, now)
	cnt.append((l, r, s, minus))
cnt.sort(key=lambda x: x[1] - x[0])
plus = []
minus = []
first = []
for x in cnt:
	if x[3] >= 0:
		first.append(x)
	elif x[0] - x[1] > 0:
		plus.append(x)
	else:
		minus.append(x)
plus.sort(key=lambda x: -x[3])
minus.sort(key=lambda x: -(x[0] - x[1] - x[3]))
l = r = 0
for x in first + plus + minus:
	for i in range(len(x[2])):
		if x[2][i] == '(':
			l += 1
		else:
			r += 1
		if l < r:
			print('No')
			exit()
if l == r:
	print('Yes')
else:
	print('No')


N = int(input())
left = []
right = []
last = None
for i in range(N):
	s = input()
	l = len(s)
	r_needed = 0
	l_needed = 0
	for j in range(l):
		if s[j] == '(':
			r_needed += 1
		elif s[j] == ')' and r_needed > 0:
			r_needed -= 1
		if s[l - 1 - j] == ')':
			l_needed += 1
		elif s[l - 1 - j] == '(' and l_needed > 0:
			l_needed -= 1
	if r_needed < l_needed:
		right.append((-r_needed, l_needed))
	else:
		left.append((l_needed, -r_needed))
left.sort()
right.sort()
rest_l = 0
for l in left:
	l_needed = l[0]
	r_needed = -l[1]
	if rest_l < l_needed:
		print('No')
		exit()
	rest_l += r_needed - l_needed
for r in right:
	l_needed = r[1]
	r_needed = -r[0]
	if rest_l < l_needed:
		print('No')
		exit()
	rest_l -= l_needed
	rest_l += r_needed
if rest_l != 0:
	print('No')
else:
	print('Yes')


n = int(input())
s = []
for i in range(n):
	s.append(input())
ls = []
rs = []
tot = 0
for si in s:
	b = 0
	h = 0
	for c in si:
		if c == '(':
			h += 1
		else:
			h -= 1
		b = min(h, b)
	if h > 0:
		ls.append((b, h))
	else:
		rs.append((b - h, -h))
	tot += h
ls.sort(reverse=True)
rs.sort(reverse=True)

def check(x):
	t = 0
	for (bx, hx) in x:
		if t + bx < 0:
			return False
		t += hx
	return True
if check(ls) and check(rs) and (tot == 0):
	print('Yes')
else:
	print('No')


N = int(input())
S1 = []
S2 = []
for i in range(N):
	S = input()
	(mid, end) = (0, 0)
	for s in S:
		if s == '(':
			end += 1
		else:
			end -= 1
		mid = min(mid, end)
	if end > 0:
		S1.append((mid, end))
	else:
		S2.append((mid - end, -end))
S1.sort(reverse=True)
S2.sort(reverse=True)
mid = end1 = 0
out = False
for t in S1:
	(a, b) = t
	mid = end1 + a
	end1 = end1 + b
	if mid < 0 or end1 < 0:
		out = True
		break
mid = end2 = 0
for t in S2:
	(a, b) = t
	mid = end2 + a
	end2 = end2 + b
	if mid < 0 or end2 < 0:
		out = True
		break
if not end1 == end2 or out:
	print('No')
else:
	print('Yes')


def main():
	from sys import stdin
	input = stdin.readline
	n = int(input())
	S = [input() for _ in [0] * n]
	s2 = []
	s3 = []
	for s in S:
		temp = [0, 0]
		m = 0
		now = 0
		for i in s.strip('\n'):
			if i == '(':
				now += 1
			else:
				now -= 1
			m = min(m, now)
		temp = [-m, s.count('(') - s.count(')') - m]
		if temp[0] < temp[1]:
			s2.append(temp)
		else:
			s3.append(temp)
	s2.sort(key=lambda x: x[0])
	s3.sort(key=lambda x: -x[1])
	cnt = 0
	for (i, j) in s2:
		cnt -= i
		if cnt < 0:
			print('No')
			return
		cnt += j
	for (i, j) in s3:
		cnt -= i
		if cnt < 0:
			print('No')
			return
		cnt += j
	if cnt != 0:
		print('No')
		return
	print('Yes')
main()


N = int(input())
S = [input() for i in range(N)]
(a, b) = (0, 0)
X = []
Y = []
for i in range(N):
	(a, b) = (0, 0)
	for j in range(len(S[i])):
		if S[i][j] == '(':
			a += 1
		else:
			a -= 1
		b = min(b, a)
	b = -b
	a = a + b
	if a > b:
		X.append((b, a))
	else:
		Y.append((b, a))
P = 0
Q = 0
X.sort()
for i in range(len(X)):
	P = max(P, Q + X[i][0])
	Q = Q + X[i][0] - X[i][1]
Y.sort(key=lambda x: -x[1])
for i in range(len(Y)):
	P = max(P, Q + Y[i][0])
	Q = Q + Y[i][0] - Y[i][1]
if P or Q:
	print('No')
else:
	print('Yes')


import sys
N = int(input())
B1 = []
B2 = []

def change(S):
	MIN = 0
	SUM = 0
	for i in S:
		if i == ')':
			SUM -= 1
			MIN = min(MIN, SUM)
		else:
			SUM += 1
	MIN *= -1
	return (MIN, SUM)
for _ in range(N):
	b = change(input())
	if b[1] > 0:
		B1.append(b)
	else:
		B2.append((b[0] + b[1], b[0], b[1]))
B1.sort()
B2.sort(reverse=True)
cnt = 0
for b in B1:
	if cnt < b[0]:
		print('No')
		sys.exit()
	cnt += b[1]
for b in B2:
	if cnt < b[1]:
		print('No')
		sys.exit()
	cnt += b[2]
if cnt == 0:
	print('Yes')
else:
	print('No')


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
Tp = []
Tn = []
for i in range(N):
	if T[i] >= 0:
		Tp.append([M[i], T[i]])
	else:
		Tn.append([M[i] - T[i], -T[i]])
Tp.sort(reverse=True)
Tn.sort(reverse=True)
res = 0
for (m, t) in Tp:
	if res < -m:
		print('No')
		exit()
	res += t
resn = 0
for (m, t) in Tn:
	if resn < -m:
		print('No')
		exit()
	resn += t
if res == resn:
	print('Yes')
else:
	print('No')


import sys
from functools import reduce
n = int(input())
s = [input() for i in range(n)]
t = [2 * i.count('(') - len(i) for i in s]
if sum(t) != 0:
	print('No')
	sys.exit()
st = [[t[i]] for i in range(n)]
for i in range(n):
	(now, mi) = (0, 0)
	for j in s[i]:
		if j == '(':
			now += 1
		else:
			now -= 1
		mi = min(mi, now)
	st[i].append(mi)
now2 = sum(map(lambda x: x[0], filter(lambda x: x[1] >= 0, st)))
(v, w) = (list(filter(lambda x: x[1] < 0 and x[0] >= 0, st)), list(filter(lambda x: x[1] < 0 and x[0] < 0, st)))
v.sort(reverse=True, key=lambda z: z[1])
w.sort(key=lambda z: z[0] - z[1], reverse=True)
for vsub in v:
	if now2 + vsub[1] < 0:
		print('No')
		sys.exit()
	now2 += vsub[0]
for wsub in w:
	if now2 + wsub[1] < 0:
		print('No')
		sys.exit()
	now2 += wsub[0]
print('Yes')


import sys
readline = sys.stdin.readline
N = int(input())
L = []
R = []
for _ in range(N):
	S = readline().strip()
	sums = 0
	mins = 0
	for s in S:
		if s == '(':
			sums += 1
		else:
			sums -= 1
			mins = min(mins, sums)
	if sums >= 0:
		L.append((sums, mins))
	else:
		R.append((-sums, mins - sums))
L.sort(key=lambda x: x[1], reverse=True)
R.sort(key=lambda x: x[1], reverse=True)
ans = 'Yes'
l_now = 0
for (d, min_d) in L:
	if l_now + min_d < 0:
		ans = 'No'
		break
	else:
		l_now += d
r_now = 0
for (d, min_d) in R:
	if r_now + min_d < 0:
		ans = 'No'
		break
	else:
		r_now += d
if l_now != r_now:
	ans = 'No'
print(ans)


import sys
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
N = ir()
S = [[1 if x == '(' else -1 for x in list(sr())] for _ in range(N)]
pos = []
neg = []
for s in S:
	temp = 0
	mi = 0
	for x in s:
		temp += x
		if temp < mi:
			mi = temp
	if temp >= 0:
		pos.append((mi, temp))
	else:
		neg.append((mi - temp, -temp))
pos.sort(reverse=True)
neg.sort(reverse=True)
cur_p = 0
cur_n = 0
bl = True
for (m, p) in pos:
	if cur_p + m < 0:
		bl = False
	cur_p += p
for (m, n) in neg:
	if cur_n + m < 0:
		bl = False
	cur_n += n
if cur_p != cur_n:
	bl = False
print('Yes' if bl else 'No')


N = int(input())
S_list = []
for _ in range(N):
	S_list.append(input().strip())
data = []
for s in S_list:
	bottom = 0
	height = 0
	for c in s:
		if c == ')':
			height -= 1
			if bottom > height:
				bottom = height
		else:
			height += 1
	if height == 0:
		k1 = 1
		k2 = 0
	elif height > 0:
		k1 = 0
		k2 = -bottom
	else:
		k1 = 2
		k2 = bottom - height
	data.append((k1, k2, bottom, height, s))
data.sort()

def check():
	height = 0
	for (k1, k2, b, h, s) in data:
		if height + b < 0:
			return False
		height += h
	return height == 0
if check():
	print('Yes')
else:
	print('No')


n = int(input())
left = []
right = []
for i in range(n):
	s = input()
	n0 = 0
	n1 = 0
	l = len(s)
	for j in range(l):
		if s[j] == '(':
			n0 += 1
		elif n0 == 0:
			n1 += 1
		else:
			n0 -= 1
	if n0 > 0 or n1 > 0:
		if n0 >= n1:
			left.append([n1, n0])
		else:
			right.append([n0, n1])
left.sort()
right.sort()

def solve():
	nl0 = 0
	nl1 = 0
	for p in left:
		if p[0] > nl0:
			return False
		nl0 -= p[0]
		nl0 += p[1]
	nr0 = 0
	nr1 = 0
	for p in right:
		if p[0] > nr1:
			return False
		nr1 -= p[0]
		nr1 += p[1]
	if nl0 == nr1:
		return True
	else:
		return False
if solve():
	print('Yes')
else:
	print('No')


def score(a, b):
	if b - a >= 0:
		return a
	else:
		return 10 ** 6 + 1 - b
N = int(input())
A = [0] * N
B = [0] * N
for i in range(N):
	S = input()
	a = 0
	b = 0
	for j in range(len(S)):
		if S[j] == ')':
			a += 1
		else:
			a -= 1
		A[i] = max(A[i], a)
		if S[-j - 1] == '(':
			b += 1
		else:
			b -= 1
		B[i] = max(B[i], b)
L = sorted(list(range(N)), key=lambda i: score(A[i], B[i]))
SA = 0
SB = 0
for i in range(N):
	if SB > A[L[i]]:
		SB += B[L[i]] - A[L[i]]
	else:
		SA += A[L[i]] - SB
		SB = B[L[i]]
if SA == 0 and SB == 0:
	print('Yes')
else:
	print('No')


n = int(input())
s = [input() for i in range(n)]
ruiseki = [[None] * (len(s[i]) + 1) for i in range(n)]
for i in range(n):
	ruiseki[i][0] = 0
	for (j, char) in enumerate(s[i]):
		if char == '(':
			ruiseki[i][j + 1] = ruiseki[i][j] + 1
		else:
			ruiseki[i][j + 1] = ruiseki[i][j] - 1
first = []
second = []
now_m = 0
now_s = 0
for i in range(n):
	(min_, sum_) = (min(ruiseki[i]), ruiseki[i][-1])
	if sum_ > 0:
		first.append((min_, sum_))
	else:
		second.append((min_, sum_))
now_m = 0
now_s = 0
first = sorted(first, reverse=True)
for (mi, su) in first:
	if now_s + mi < 0:
		print('No')
		exit()
	now_s += su
second = sorted(second, key=lambda x: x[0] - x[1])
for (mi, su) in second:
	if now_s + mi < 0:
		print('No')
		exit()
	now_s += su
if now_s == 0:
	print('Yes')
else:
	print('No')


n = int(input())
plus_bracket = []
minus_bracket = []
for _ in range(n):
	mini = 0
	cur = 0
	for bracket in input():
		if bracket == '(':
			cur += 1
		else:
			cur -= 1
		if cur < mini:
			mini = cur
	if cur > 0:
		plus_bracket.append([-mini, cur])
	else:
		minus_bracket.append([cur - mini, -cur])
success = True
cur = 0
plus_bracket.sort()
minus_bracket.sort()
for bracket in plus_bracket:
	if cur < bracket[0]:
		success = False
		break
	cur += bracket[1]
back_cur = 0
for bracket in minus_bracket:
	if back_cur < bracket[0]:
		success = False
		break
	back_cur += bracket[1]
if cur != back_cur:
	success = False
if success:
	print('Yes')
else:
	print('No')


n = int(input())
sl = 0
sr = 0
sz = 0
lr = []
rl = []
for i in range(n):
	s = input()
	l = 0
	r = 0
	z = 0
	for j in s:
		if j == '(':
			l += 1
		else:
			r += 1
		z = max(z, r - l)
	sl += l
	sr += r
	if z == 0 and l >= r:
		sz += l - r
	elif l >= r:
		lr.append((z, l - r))
	else:
		rl.append((z, l - r))
if sl != sr:
	print('No')
	quit()
lr.sort()
for (z, i) in lr:
	if sz < z:
		print('No')
		quit()
	else:
		sz += i
rl.sort(key=lambda x: x[0] + x[1], reverse=True)
for (z, i) in rl:
	if sz < z:
		print('No')
		quit()
	else:
		sz += i
print('Yes')


def check(ar):
	h = 0
	for (bottom, up) in ar:
		if h + bottom < 0:
			return False
		h += up
	return True

def __starting_point():
	N = int(input())
	pos = []
	neg = []
	equal = 0
	for _ in range(N):
		cnt = 0
		bottom = 0
		for s in input():
			if s == '(':
				cnt += 1
			else:
				cnt -= 1
			bottom = min(bottom, cnt)
		equal += cnt
		if cnt >= 0:
			pos.append((bottom, cnt))
		else:
			cnt = -cnt
			bottom += cnt
			neg.append((bottom, cnt))
	pos.sort(reverse=True)
	neg.sort(reverse=True)
	if check(pos) and check(neg) and (equal == 0):
		print('Yes')
	else:
		print('No')
__starting_point()


n = int(input())
a = []
b = []
for i in range(n):
	s = input()
	x = 0
	y = 0
	for c in s:
		if c == '(':
			y += 1
		elif y >= 1:
			y -= 1
		else:
			x += 1
	if x < y:
		a.append([x, abs(x - y)])
	else:
		b.append([y, abs(x - y)])

def calc(a):
	ret = 0
	for (x, y) in sorted(a):
		if ret < x:
			return -1
		ret += y
	return ret
res1 = calc(a)
res2 = calc(b)
print('Yes' if res1 >= 0 and res1 == res2 else 'No')


n = int(input())
(ls, rs) = ([], [])

def check(sl):
	h = 0
	for t in sl:
		b = h + t[0]
		if b < 0:
			return False
		h += t[1]
	return True
total = 0
for i in range(n):
	(b, h) = (0, 0)
	S = input()
	for s in S:
		if s == '(':
			h += 1
		else:
			h -= 1
		b = min(b, h)
	if h > 0:
		ls.append((b, h))
	else:
		rs.append((b - h, -h))
	total += h
ls.sort(key=lambda x: x[0], reverse=True)
rs.sort(key=lambda x: x[0], reverse=True)
print('Yes' if check(ls) and check(rs) and (total == 0) else 'No')


import sys
input = sys.stdin.readline
N = int(input())
S = [input()[:-1] for _ in range(N)]
high = []
low = []
for i in range(N):
	cnt = 0
	m = 0
	for j in range(len(S[i])):
		if S[i][j] == '(':
			cnt += 1
		else:
			cnt -= 1
		m = min(m, cnt)
	if cnt > 0:
		high.append([m, cnt])
	else:
		low.append([m - cnt, -cnt])
high.sort(reverse=True)
low.sort(reverse=True)
h = 0
for (m, up) in high:
	if h + m < 0:
		print('No')
		sys.exit()
	h += up
p = 0
for (m, down) in low:
	if p + m < 0:
		print('No')
		sys.exit()
	p += down
if h == p:
	print('Yes')
else:
	print('No')


import sys
input = sys.stdin.readline
N = int(input())
S = [input()[:-1] for _ in range(N)]
(rl1, rl2) = ([], [])
for Si in S:
	(l, r) = (0, 0)
	for i in range(len(Si)):
		if Si[i] == '(':
			l += 1
		elif l == 0:
			r += 1
		else:
			l -= 1
	if l - r >= 0:
		rl1.append((r, l))
	else:
		rl2.append((r, l))
rl1.sort(key=lambda t: t[0])
rl2.sort(key=lambda t: t[1], reverse=True)
rl3 = rl1 + rl2
bal = 0
for (r, l) in rl3:
	bal -= r
	if bal < 0:
		print('No')
		exit()
	bal += l
if bal == 0:
	print('Yes')
else:
	print('No')


N = int(input())
memo1 = []
memo2 = []
for i in range(N):
	S = input()
	sum_n = min_n = 0
	for s in S:
		if s == ')':
			sum_n -= 1
			min_n = min(min_n, sum_n)
		else:
			sum_n += 1
	if sum_n >= 0:
		memo1.append([min_n, sum_n])
	else:
		memo2.append([min_n - sum_n, -sum_n])
memo1.sort(reverse=True)
left = 0
for (min_n, sum_n) in memo1:
	if left + min_n < 0:
		print('No')
		quit()
	else:
		left += sum_n
memo2.sort(reverse=True)
right = 0
for (min_n, sum_n) in memo2:
	if right + min_n < 0:
		print('No')
		quit()
	else:
		right += sum_n
if left == right:
	print('Yes')
else:
	print('No')


import bisect

def main():
	N = int(input())
	SS = []
	for _ in range(N):
		SS.append(input())
	S = []
	for s in SS:
		while '()' in s:
			s = s.replace('()', '')
		S.append(s)
	S = [s for s in S if s != '']
	sum_op = 0
	sum_cl = 0
	S_both_op = []
	S_both_cl = []
	for s in S:
		if not ')' in s:
			sum_op += len(s)
		elif not '(' in s:
			sum_cl += len(s)
		else:
			pos = s.find('(')
			if pos <= len(s) - pos:
				S_both_op.append((pos, len(s) - pos))
			else:
				S_both_cl.append((pos, len(s) - pos))
	S_both_op.sort(key=lambda x: x[0])
	S_both_cl.sort(key=lambda x: -x[1])
	for p in S_both_op:
		sum_op -= p[0]
		if sum_op < 0:
			print('No')
			return
		sum_op += p[1]
	for p in S_both_cl:
		sum_op -= p[0]
		if sum_op < 0:
			print('No')
			return
		sum_op += p[1]
	if sum_op == sum_cl:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


def encode(s):
	val = 0
	min_val = 0
	for c in s:
		if c == '(':
			val += 1
		else:
			val -= 1
			min_val = min(min_val, val)
	return [min_val, val]

def check(s):
	h = 0
	for p in s:
		b = h + p[0]
		if b < 0:
			return False
		h += p[1]
	return True
n = int(input())
ls = []
rs = []
l_total = 0
r_total = 0
for _ in range(n):
	si = encode(input())
	if si[1] > 0:
		ls.append(si)
		l_total += si[1]
	else:
		si[0] -= si[1]
		si[1] *= -1
		rs.append(si)
		r_total += si[1]
list.sort(ls, reverse=True)
list.sort(rs, reverse=True)
if check(ls) and check(rs) and (l_total == r_total):
	print('Yes')
else:
	print('No')


def countpars(s):
	lc = 0
	rc = 0
	for ch in s:
		if ch == '(':
			lc += 1
		elif lc > 0:
			lc -= 1
		else:
			rc += 1
	return (lc, rc)
n = int(input())
ssl = []
ssr = []
for i in range(n):
	(clc, crc) = countpars(input())
	if clc >= crc:
		ssl.append((clc, crc))
	else:
		ssr.append((clc, crc))
ssl.sort(key=lambda x: x[1])
ssr.sort(reverse=True)
lc = 0
rc = 0
for (clc, crc) in ssl:
	lc -= crc
	if lc < 0:
		print('No')
		exit()
	lc += clc
for (clc, crc) in ssr:
	lc -= crc
	if lc < 0:
		print('No')
		exit()
	lc += clc
if lc == 0:
	print('Yes')
else:
	print('No')


N = int(input())
S = [input() for _ in range(N)]
B1 = []
B2 = []
for s in S:
	ncl = nop = 0
	for c in s:
		if c == '(':
			nop += 1
		elif nop > 0:
			nop -= 1
		else:
			ncl += 1
	if nop - ncl >= 0:
		B1.append((nop, ncl))
	else:
		B2.append((nop, ncl))
Bl = sorted(B1, key=lambda x: x[1])
Br = sorted(B2, key=lambda x: x[0])
xl = 0
for (nop, ncl) in Bl:
	if ncl > xl:
		print('No')
		exit()
	xl += nop - ncl
xr = 0
for (nop, ncl) in Br:
	if nop > xr:
		print('No')
		exit()
	xr += ncl - nop
print('Yes' if xl == xr else 'No')


Yes = 'Yes'
No = 'No'

def main():
	N = int(input())
	up = []
	down = []
	for _ in range(N):
		S = input()
		(h, b) = (0, 0)
		for s in S:
			if s == '(':
				h += 1
			else:
				h -= 1
				b = min(b, h)
		if h >= 0:
			up.append((h, b))
		else:
			down.append((h, b))
	up.sort(key=lambda t: t[1], reverse=True)
	down.sort(key=lambda t: t[0] - t[1], reverse=True)
	H = 0
	for (h, b) in up + down:
		if H + b >= 0:
			H += h
		else:
			print(No)
			return
	if H == 0:
		print(Yes)
	else:
		print(No)
main()


def Yes():
	print('Yes')
	exit()

def No():
	print('No')
	exit()

def parse(s):
	ret = [0, 0]
	for c in s:
		if c == ')':
			if ret[0]:
				ret[0] -= 1
			else:
				ret[1] -= 1
		else:
			ret[0] += 1
	return ret
N = int(input())
S = [parse(input()) for _ in range(N)]
S.sort(key=lambda a: -a[0] - a[1])
S1 = []
S2 = []
for s in S:
	if s[0] + s[1] >= 0:
		S1 += [s]
	else:
		S2 += [s]
S1.sort(key=lambda a: -a[1])
S2.sort(key=lambda a: -a[0])
cnt = 0
for s in S1:
	cnt += s[1]
	if cnt < 0:
		No()
	cnt += s[0]
for s in S2:
	cnt += s[1]
	if cnt < 0:
		No()
	cnt += s[0]
if cnt:
	No()
Yes()


def LI():
	return list(map(int, input().split()))
N = int(input())
S = [input() for _ in range(N)]

def judge(v):
	x = 0
	for (b, l) in v:
		if x + b < 0:
			return False
		x += l
	return True

def main():
	(pl, ml) = ([], [])
	t = 0
	for i in range(N):
		l = 0
		b = 0
		for s in S[i]:
			if s == '(':
				l += 1
			else:
				l -= 1
				b = min(b, l)
		t += l
		if l > 0:
			pl.append((b, l))
		else:
			ml.append((b - l, -l))
	pl.sort(reverse=True)
	ml.sort(reverse=True)
	if judge(pl) and judge(ml) and (t == 0):
		print('Yes')
	else:
		print('No')
main()


n = int(input())
lst_left = []
lst_right = []
total = 0
for i in range(n):
	s = input()
	h = 0
	b = 0
	for e in s:
		if e == '(':
			h += 1
		else:
			h -= 1
			b = min(b, h)
	if h >= 0:
		lst_left.append((b, h))
	else:
		lst_right.append((b - h, -h))
	total += h
lst_left = sorted(lst_left, key=lambda x: x[0], reverse=True)
lst_right = sorted(lst_right, key=lambda x: x[0], reverse=True)
if total != 0:
	print('No')
	exit()
h = 0
for i in range(len(lst_left)):
	if h + lst_left[i][0] < 0:
		print('No')
		exit()
	else:
		h += lst_left[i][1]
h = 0
for i in range(len(lst_right)):
	if h + lst_right[i][0] < 0:
		print('No')
		exit()
	else:
		h += lst_right[i][1]
print('Yes')


n = int(input())
(L, R) = ([], [])
for i in range(n):
	(a, b) = (0, 0)
	for c in input():
		if c == '(':
			b += 1
		if c == ')':
			if b > 0:
				b -= 1
			else:
				a += 1
	if -a + b > 0:
		L.append((a, b))
	else:
		R.append((a, b))
L.sort(key=lambda x: x[0])
R.sort(key=lambda x: x[1], reverse=True)
x = 0
for (a, b) in L + R:
	x -= a
	if x < 0:
		print('No')
		exit()
	x += b
if x == 0:
	print('Yes')
else:
	print('No')


n = int(input())
s = [input() for _ in range(n)]
d0 = {}
d1 = {}
ma = 0
mab = 0
for si in s:
	(a, b) = (0, 0)
	for x in si:
		if x == '(':
			b += 1
		else:
			b -= 1
		a = min(b, a)
	a = -a
	if b >= 0:
		if a in d0:
			d0[a].append([a, b])
		else:
			d0[a] = [[a, b]]
		ma = max(ma, a)
	else:
		if a + b in d1:
			d1[a + b].append([a, b])
		else:
			d1[a + b] = [[a, b]]
		mab = max(mab, a + b)
now = 0
for i in range(ma + 1):
	if i not in d0:
		continue
	if now >= i:
		for (a, b) in d0[i]:
			now += b
	else:
		print('No')
		exit()
for i in range(mab, -1, -1):
	if i not in d1:
		continue
	for (a, b) in d1[i]:
		if now >= a:
			now += b
		else:
			print('No')
			exit()
if now == 0:
	print('Yes')
else:
	print('No')


(n, inc, dec) = (int(input()), [], [])
for _ in range(n):
	(s, d, r) = (input(), 0, 0)
	for c in s:
		d = d + (1 if c == '(' else -1)
		r = max(r, -d)
	(dec if d < 0 else inc).append((d, r))
inc.sort(key=lambda x: x[1])
dec.sort(key=lambda x: x[0] + x[1])
(p1, p2, ok) = (0, 0, True)
for s in inc:
	ok &= s[1] <= p1
	p1 += s[0]
for s in dec:
	ok &= p2 >= s[0] + s[1]
	p2 -= s[0]
ok &= p1 == p2
print('Yes' if ok else 'No')


n = int(input())
pos = []
neg = []
for _ in range(n):
	s = input()
	low_pos = 0
	increase = 0
	for v in s:
		if v == '(':
			increase += 1
		else:
			increase -= 1
			low_pos = min(low_pos, increase)
	if increase >= 0:
		pos.append((low_pos, increase))
	else:
		(low_pos, increase) = (low_pos - increase, -increase)
		neg.append((low_pos, increase))
pos.sort()
pos.reverse()
neg.sort()
neg.reverse()
now_pos = 0
for (low_pos, increase) in pos:
	if now_pos + low_pos < 0:
		print('No')
		return
	else:
		now_pos += increase
right_pos = 0
for (low_pos, increase) in neg:
	if right_pos + low_pos < 0:
		print('No')
		return
	else:
		right_pos += increase
if right_pos != now_pos:
	print('No')
	return
else:
	print('Yes')


N = int(input().strip())
S_list = [input().strip() for _ in range(N)]

def count_brackets(brackets):
	cnt = 0
	min_cnt = 0
	for i in range(len(brackets)):
		if brackets[i] == '(':
			cnt += 1
		else:
			cnt -= 1
			min_cnt = min(min_cnt, cnt)
	return (cnt, min_cnt)
tmp_cnt_plus = []
tmp_cnt_minus = []
for S in S_list:
	(cnt, min_cnt) = count_brackets(S)
	if cnt == min_cnt == 0:
		continue
	if cnt >= 0:
		tmp_cnt_plus.append((cnt, min_cnt))
	else:
		tmp_cnt_minus.append((cnt, min_cnt))
tmp_cnt_plus.sort(key=lambda x: -x[1])
tmp_cnt_minus.sort(key=lambda x: -(x[0] - x[1]))
sum_bracket = 0
flag = True
for (cnt, min_cnt) in tmp_cnt_plus + tmp_cnt_minus:
	if sum_bracket + min_cnt < 0:
		flag = False
		break
	sum_bracket += cnt
if sum_bracket == 0 and flag:
	print('Yes')
else:
	print('No')


import sys
input = lambda : sys.stdin.readline().rstrip()
N = int(input())
S_p = []
S_n = []
for i in range(N):
	end = 0
	mim = 0
	for c in input():
		if c == '(':
			end += 1
		else:
			end -= 1
		mim = min(mim, end)
	if end >= 0:
		S_p.append([mim, end])
	else:
		S_n.append([end - mim, end])
S_p.sort(reverse=True)
S_n.sort(reverse=True)
check = 0
flag = True
for i in range(len(S_p)):
	if check + S_p[i][0] >= 0:
		check += S_p[i][1]
	else:
		flag = False
		break
for i in range(len(S_n)):
	if check + S_n[i][1] - S_n[i][0] >= 0:
		check += S_n[i][1]
	else:
		flag = False
		break
if check == 0 and flag:
	flag = True
else:
	flag = False
print('Yes' if flag else 'No')


def main():
	n = int(input())
	if n == 0:
		print('Yes')
		return
	slup = []
	sldown = []
	for i in range(n):
		(height, mi) = (0, 0)
		for si in input():
			if si == '(':
				height += 1
			else:
				height -= 1
				mi = min(mi, height)
		if height >= 0:
			slup.append([mi, height])
		else:
			sldown.append([mi - height, -height])
	slup.sort(reverse=True)
	sldown.sort(reverse=True)
	if sum([si[1] for si in slup]) + sum([-si[1] for si in sldown]):
		print('No')
		return
	h = 0
	for si in slup:
		if h + si[0] < 0:
			print('No')
			return
		h += si[1]
	h = 0
	for si in sldown:
		if h + si[0] < 0:
			print('No')
			return
		h += si[1]
	print('Yes')
main()


n = int(input())
arr1 = []
arr2 = []
for _ in range(n):
	s = input()
	cnt1 = 0
	cnt2 = 0
	for i in range(len(s)):
		if s[i] == '(':
			cnt1 += 1
		elif cnt1 == 0:
			cnt2 += 1
		else:
			cnt1 -= 1
	if cnt1 - cnt2 >= 0:
		arr1.append([cnt1, cnt2])
	else:
		arr2.append([cnt1, cnt2])
arr1 = sorted(arr1, key=lambda x: x[1])
arr2 = sorted(arr2, reverse=True, key=lambda x: x[0])
arr = arr1 + arr2
cnt = 0
for (a, b) in arr:
	if b > cnt:
		print('No')
		break
	else:
		cnt += a - b
else:
	if cnt == 0:
		print('Yes')
	else:
		print('No')


n = int(input())
s = [0] * n
info = [0] * n
for i in range(n):
	ss = list(input())
	now = 0
	mi = 0
	for sss in ss:
		if sss == '(':
			now += 1
		else:
			now -= 1
			mi = min(mi, now)
	info[i] = (mi, now)
info0 = []
info1 = []
for (i, inf) in enumerate(info):
	if inf[1] >= 0:
		info0.append(inf)
	else:
		info1.append(inf)
info0.sort(key=lambda x: x[0], reverse=True)
info1.sort(key=lambda x: x[1] - x[0], reverse=True)
ans = True
now = 0
for inf in info0:
	if now + inf[0] >= 0:
		now += inf[1]
	else:
		ans = False
for inf in info1:
	if now + inf[0] >= 0:
		now += inf[1]
	else:
		ans = False
if now != 0:
	ans = False
print(ans * 'Yes' + (1 - ans) * 'No')


def check(s):
	h = 0
	for p in s:
		b = h + p[0]
		if b < 0:
			return False
		h += p[1]
	return True
N = int(input())
S = []
left = []
right = []
total = 0
for i in range(N):
	last = 0
	bottom = 0
	s = input().strip()
	for c in s:
		if c == '(':
			last += 1
		else:
			last -= 1
		bottom = min(bottom, last)
	if last > 0:
		left.append((bottom, last))
	else:
		right.append((bottom - last, -last))
	total += last
left.sort(key=lambda x: -x[0])
right.sort(key=lambda x: -x[0])
if check(left) and check(right) and (total == 0):
	print('Yes')
else:
	print('No')


import sys
input = sys.stdin.readline
n = int(input())
chk = []
res = []
for i in range(n):
	s = input().strip()
	m = 0
	e = 0
	for i in s:
		if i == ')':
			e -= 1
			m = min(m, e)
		else:
			e += 1
	if e >= 0:
		chk.append((m, e))
	else:
		res.append((-1 * e + m, -1 * e))
from operator import itemgetter
res.sort(key=itemgetter(0), reverse=True)
chk.sort(key=itemgetter(0), reverse=True)
ans = 'Yes'
st = 0
for (i, j) in chk:
	if i + st < 0:
		ans = 'No'
		break
	st += j
sr = 0
for (i, j) in res:
	if i + sr < 0:
		ans = 'No'
		break
	sr += j
if st != sr:
	ans = 'No'
print(ans)


n = int(input())
lipp = []
lipm = []
limm = []
allcnt = 0
for _ in range(n):
	s = input()
	cnt = 0
	mi = 0
	for x in s:
		cnt += 1 if x == '(' else -1
		mi = min(mi, cnt)
	if cnt >= 0:
		lipm.append([mi, cnt])
	else:
		limm.append([mi - cnt, -cnt])
	allcnt += cnt
if allcnt != 0:
	print('No')
	exit()
lipm.sort(reverse=True)
limm.sort(reverse=True)

def solve(l):
	now = 0
	for (mi, cnt) in l:
		if mi + now < 0:
			print('No')
			exit()
		now += cnt
solve(lipm)
solve(limm)
print('Yes')


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

def count(s):
	n = len(s)
	n1 = 0
	n2 = 0
	for i in range(n):
		if s[i] == '(':
			n2 += 1
		elif n2 > 0:
			n2 -= 1
		else:
			n1 += 1
	return (n1, n2)
l1 = []
l2 = []
m1 = 0
m2 = 0
for i in range(N):
	(n1, n2) = count(S[i])
	if n1 + n2 == 0:
		continue
	if n1 == 0:
		m1 += n2
		continue
	if n2 == 0:
		m2 += n1
		continue
	if n2 - n1 >= 0:
		l1.append([n1, n2])
	else:
		l2.append([n1, n2])
ans = 'Yes'
l1.sort()
for i in range(len(l1)):
	(n1, n2) = l1[i]
	if m1 < n1:
		ans = 'No'
		break
	m1 += n2 - n1
l2.sort(key=lambda x: x[1])
for i in range(len(l2)):
	(n1, n2) = l2[i]
	if m2 < n2:
		ans = 'No'
		break
	m2 += n1 - n2
if m1 != m2:
	ans = 'No'
print(ans)