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> // cin, cout, cerr
#include <algorithm> // minmax, sort, swap
#include <numeric> // iota
#include <cstdio> // printf, scanf
#include <string> // string, stoi, to_string
#include <vector> // vector
#include <queue> // queue, priority_queue
#include <deque> // deque
#include <map> // key-value pairs sorted by keys
#include <set> // set
#include <iomanip> // cout<<setprecision(n)
#include <functional> // function<void(int)>
#include <cmath>
#include <cassert>
#include <bitset>
#include <stack>

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i,n) for(int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using Graph = std::vector<std::vector<int> >;
const int INF = 1e17;
/* libraries */

struct S
{
	int a,b;
	S(int a, int b) : a(a),b(b) {}
	bool operator<(const S rhs) const {
		if(b>=0 && rhs.b<0) return true;
		if(b<0 && rhs.b>=0) return false;
		if(b>=0 && rhs.b>=0) {
			return a<rhs.a;
		}
		if(b<0 && rhs.b<0) {
			if(a==rhs.a) {
				return b>rhs.b;
			}
			return a>rhs.a;
		}
		return true;
	}
};

signed main() {
	int n;
	std::cin >> n;
	std::vector<std::string> s(n);
	rep(i,n) std::cin >> s[i];
	std::vector<int> a(n,0),b(n,0);
	int A=0,B=0;
	rep(i,n) {
		debug(i);
		std::stack<char> stack;
		for(auto c:s[i]) {
			if(stack.empty()) {
				stack.emplace(c);
				continue;
			}
			char x = stack.top();
			if(x==')' || x==c) stack.emplace(c);
			else {
				stack.pop();
			}
		}
		while(!stack.empty()) {
			char x = stack.top();
			stack.pop();
			if(x=='(') a[i]++;
			else b[i]++;
		}
	}
	// need start and end, everything needs to add up
	std::vector<S> v;
	rep(i,n) {
		v.emplace_back(b[i],a[i]-b[i]);
	}
	std::sort(all(v));
	rep(i,n) {
		debug(i,v[i].a,v[i].b);
	}
	debug(a,b);
	int curr = 0;
	rep(i,n) {
		if(v[i].a>curr) {
			print("No");
			return 0;
		}
		curr+=v[i].b;
	}
	curr = 0;
	for(int i=n-1;i>=0;i--) {
		if(v[i].a+v[i].b<curr) {
			print("No");
			return 0;
		}
		curr-=v[i].b;
	}
	if(curr==0) print("Yes");
	else print("No");

	return 0;
}