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>
using namespace std;
typedef long long ll;
// #define endl '\n';
const int maxn=1e6+5;
const int addend = 5e5+1;
int t[4*maxn];
int a[maxn];
const int mod=1e9;
void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = tl;
    } else {
        int tm = (tl + tr) / 2;
        build(v*2, tl, tm);
        build(v*2+1, tm+1, tr);
        if(a[t[v*2]] > a[t[v*2+1]]) t[v] = t[2*v];
        else t[v] = t[2*v+1];
    }
}
int find(int v, int tl, int tr, int l, int r) {
    if (l > r) 
        return -1;
    if (l == tl && r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    int al = find(v*2, tl, tm, l, min(r, tm));
    int ar = find(v*2+1, tm+1, tr, max(l, tm+1), r);
    if(al==-1) return ar;
    if(ar==-1) return al;
    if(a[al] > a[ar]) return al;
    else return ar;
}
void update(int v, int tl, int tr, int pos) {
    if (tl == tr) {
        t[v] = pos;
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos);
        else
            update(v*2+1, tm+1, tr, pos);
        if(a[t[v*2]] > a[t[v*2+1]]) t[v] = t[2*v];
        else t[v] = t[2*v+1];
    }
}
pair<int,int> help(string &s){
	int val=0,ansval=1e9;
	for(auto &it:s){
		if(it=='(') val++;
		else val--;
		ansval = min(ansval, val);
	}
	return {ansval,val};
}
void finish(){
	cout<<"No"<<endl;
	exit(0);
}
int32_t main(){
	ios::sync_with_stdio(0); 
	cin.tie(0); 
	cout.tie(0);
	int n;
	cin>>n;
	// cout<<"asdf"<<endl;
	string s[n];
	vector<pair<int,int> >v;
	int val=0;
	for (int i = 0; i < n; ++i)
	{
		cin>>s[i];
		v.emplace_back(help(s[i]));
		val += v.back().second;
	}
	if(val!=0){
		finish();
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; ++i)
	{
		// cout<<v[i].first<<" "<<v[i].second<<endl;
		a[i] = v[i].second;
		// cout<<a[i]<<" ";
	}
	// cout<<endl;
	// cout<<endl;
	build(1,0,n-1);
	for (int i = 0; i < n; ++i)
	{
		auto it = upper_bound(v.begin(), v.end(),make_pair(-val-1,mod));
		// cout<<-val<<" "<<-maxn<<" "<<(*it).first<<" "<<(*it).second<<endl;
		if(it==v.end()) finish();
		int id=it-v.begin();
		// cout<<id<<" ";
		id = find(1,0,n-1,id,n-1);
		if(id==-1) finish();
		val += a[id];
		// cout<<val<<" val"<<endl;
		// cout<<id<<" ";
		if(val<0) finish();
		// cout<<a[id]<<" ";
		a[id] = -mod;
		// cout<<a[id]<<endl;
		update(1,0,n-1,id);
	}
	cout<<"Yes"<<endl;
	return 0;
}