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>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define mod 998244353
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define prec(y,x) fixed<<setprecision(y)<<x
#define inf 1e18
#define pi 3.1415926535
typedef tree<int, null_type, less<int>, rb_tree_tag,                    //if using ll change int to ll
        tree_order_statistics_node_update> pbset;
#define fob(x) find_by_order(x)
#define ook(x) order_of_key(x)

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n;string s;
	cin>>n;vector<pair<int,int>> v;int u=1e7;
	for(int ii=0;ii<n;ii++)
	{
	    cin>>s;
	    int a,b;a=0;b=0;
	    for(int i=0;i<s.size();i++)
	    {
	        if(s[i]=='(')
	        {
	            a++;
	        }
	        else
	        {
	            if(a==0)
	            b++;
	            else
	            a--;
	        }
	    }
	    if(a==0&&b==0)
	    continue;
	    else
	    {
	        v.pb(mk(b,u-a));
	    }
	}
	sort(v.begin(),v.end());
	int f=0;int a,b,pa,pbe;a=0;b=0;pa=0;pbe=0;
	for(int i=0;i<v.size();i++)
	{
	    b=v[i].ff;a=u-v[i].ss;
	    if(b>pa)
	    {
	        f=1;break;
	    }
	    else
	    {
	        pa+=a;
	        pa-=b;
	        
	    }
	}
	if(f==1||pa>0)
	cout<<"No";
	else
	cout<<"Yes";
	 return 0;
}