Problem:
Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

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

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n,lsum=0,rsum=0;
string s[1000005];
struct A
{
	int l,r,del;
};
A data0[1000005],data1[1000005];
bool f(A x,A y)
{
	if (x.r<y.r) return true;
	return false;
}
int main()
{
	cin>>n;
	for (int i=1;i<=n;i++)
		cin>>s[i];
	for (int i=1;i<=n;i++)
	{
		data0[i].l=data0[i].r=0;
		for (int j=0;j<s[i].length();j++)
		{
			if (s[i][j]=='(') data0[i].l++;
			if (s[i][j]==')') data0[i].r++;
		}
		int left=0;
		int right=s[i].length()-1;
		while(left<=right)
		{
			while(s[i][left]!='('&&left<=s[i].length()-1) left++;
			while(s[i][right]!=')'&&right>=0) right--;
			if (left<=right&&s[i][left]=='('&&s[i][right]==')')
			{
				data0[i].l--;
				data0[i].r--;
			}
			left++;
			right--;
		}
	}
	//for (int i=1;i<=n;i++) cout<<l[i]<<' '<<r[i]<<endl;
	for (int i=1;i<=n;i++)
	{
		lsum+=data0[i].l;
		rsum+=data0[i].r;
		data0[i].del=data0[i].l-data0[i].r;
	}
	if (lsum!=rsum) 
	{
		cout<<"No"<<endl;
		return 0;
	}
	int m=0;
	for (int i=1;i<=n;i++)
	{
		if (data0[i].del>=0)
		{
			m++;
			data1[m]=data0[i];
		}
	}
	sort(data1+1,data1+m+1,f);
	//for (int i=1;i<=m;i++)
	//	cout<<data1[i].l<<' '<<data1[i].r;
	int cur=0;
	int flag=1;
	for (int i=1;i<=m;i++)
	{
	if (cur<data1[i].r){
		cout<<"No"<<endl;
		flag=0;
		break;
	}else{
		cur=cur+data1[i].l-data1[i].r;
	}
	}
	if (flag==1) cout<<"Yes"<<endl;
	return 0;
}