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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define INF 1000000021
#define MOD 1000000007
#define pb push_back
#define sqr(a) (a)*(a)
#define M(a, b) make_pair(a,b)
#define F first
#define S second
#define all(x) (x.begin(), x.end())
#define deb(x) cerr << #x << " = " << x << '\n'
#define N 222222

using namespace std;
using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

const ld pi = 2 * acos(0.0);
template<class T> bool umin(T& a, T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool umax(T& a, T b){if(a<b){a=b;return 1;}return 0;}
template<class T, class TT> bool pal(T a, TT n){int k=0;for(int i=0;i<=n/2;i++){if(a[i]!=a[n-i-1]){k=1;break;}}return k?0:1;}

//int month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

string a[N*10];
int b[N*10];
int main()
{
	int n;
	cin >> n;
	set<pair<int, pii> > s;
	int id = 0, sum = 0, ok = 0;
	for(int i = 1; i <= n; i ++)
	{
		cin >> a[i];
		int k = 0, mn = 0;
		for(int j = 0; j < a[i].size(); j ++)
		{
			char c = a[i][j];
			if(c == '(')
				k ++;
			else
				k --;
			umin(mn, k);
		}
//		deb(k);
//		deb(mn);
		b[i] = mn;
		if(mn >= 0)
		{
		for(int j = 0; j < a[i].size(); j ++)
	{
		char c = a[i][j];
		if(c == '(')
			sum ++;
		else
			sum --;
	}
	ok = 1;
		}
		else
		s.insert(M(mn, M(-k, i)));
	}
	if(!ok)
		return cout << "No\n",0;
	while(s.size())
	{
		auto it = s.lower_bound(M(-sum, M(-INF,-1)));
		if(it == s.end())
			return cout << "No", 0;
		s.erase(it);
		pair<int, pii> p = *it;
//		deb(p.S.S);
		for(int j = 0; j < a[p.S.S].size(); j ++)
		{
			char c = a[p.S.S][j];
			if(c == '(')
				sum ++;
			else
				sum --;
		}
	}
	if(sum == 0)
		cout << "Yes";
	else
		cout << "No";
	




	





	getchar();
	getchar();
	return 0;
	//ios::sync_with_stdio(false);
	//cin.tie(0);
}