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: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
//#define int         long long
#define ull 		unsigned long long
#define ll 			long long
#define M 			1000000007
#define pb 			push_back
#define p_q 		priority_queue
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios	    	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mp 			make_pair
#define lb 			lower_bound
#define ub			upper_bound
#define F           first
#define S           second
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ini(a,n,b)	for(ll int i=0;i<n;i++) a[i]=0;
#define cset(a)		__builtin_popcountll(a)
#define hell 		(ull)1e9
#define PI          acos(-1)
#define re 			resize
#define un(a)		sort(a.begin(),a.end());a.resize(unique(a.begin(),a.end())-a.begin())

int n;
struct ele{
	string s;
	int st,en;
	ele(const string in){
		this->s=in;
		st=en=0;
		for(int i=0;i<sz(in);i++){
			if(in[i]=='(')
				en++;
			else
				st++;
		}
	}
};
vector<ele>a;


signed main(void)
{ios
	cin>>n;
	rep(i,0,n)
	{
		string s;
		cin>>s;
		stack<char> x;
		for(char c:s){
			if(c=='(')
				x.push(c);
			else{
				if(!x.empty()&&x.top()=='(')
					x.pop();
				else
					x.push(c);
			}
		}
		s="";
		if(x.empty())
			continue;
		while(!x.empty()){
			char c=x.top();
			s+=c;
			x.pop();
		}
		reverse(all(s));
		a.push_back(ele(s));
	}
	sort(all(a),[](ele l,ele r){
		if(l.st==0)
			return true;
		if(r.st==0)
			return false;
		if(l.en==0)
			return false;
		if(r.en==0)
			return true;
		return l.en>r.en;
	});
	stack<char>x;
	for(auto i:a){
		for(int j=0;j<i.st;j++){
			if(!x.empty()&&x.top()=='(')
				x.pop();
			else
				x.push(')');
		}
		for(int j=0;j<i.en;j++)
			x.push('(');
	}
	if(x.empty())
		cout<<"YES"<<endl;
	else
		cout<<"NO"<<endl;
	
}