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 Java (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main 
{
	static class C implements Comparator<Entry>
	{
		@Override
		public int compare(Entry o1, Entry o2) 
		{
			return o1.l - o2.l;
		}
	}
	static class C2 implements Comparator<Entry>
	{
		@Override
		public int compare(Entry o1, Entry o2) 
		{
			return o1.l - o2.l;
		}
	}
	
	static class Entry
	{
		int l;
		int r;
		int id;
		
		public Entry(int l0, int r0, int i) 
		{
			l = l0;
			r = r0;
			id = i;
		}
	}

	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		C c = new C();
		C2 c2 = new C2();
		PriorityQueue<Entry> q1 = new PriorityQueue<>(c);
		PriorityQueue<Entry> q2 = new PriorityQueue<>(c);
		
		for(int i = 0; i < n; i++)
		{
			String s = sc.next();
			
			int cur = 0;
			int l = 0;
			for(int j = 0; j < s.length(); j++)
			{
				if(s.charAt(j) == '(')
				{
					cur++;
				}
				else
				{
					cur--;
				}
				l = Math.min(l, cur);
			}
			l*=-1; //entry
			int r = cur; //payoff
//			System.out.println(l + " " + r + " " + s);
			
			if(r < 0)
			{ //do these later
				l = 0;
				r = 0;
				cur = 0;
				
				for(int j = s.length()-1; j >= 0; j--)
				{
					if(s.charAt(j) == ')')
					{
						cur++;
					}
					else
					{
						cur--;
					}
					l = Math.min(l, cur);
				}
				l*=-1; //entry
				r = cur;
				
				q2.add(new Entry(l,r, i));
			}
			else
			{ //do these first
				q1.add(new Entry(l,r, i));
			}
		}
		
		long has = 0;
		
		while(!q1.isEmpty())
		{
			Entry en = q1.poll();
//			System.out.println("Consider " + en.l + " " + en.r + ", has " + has);
			//take this
			if(en.l > has)
			{
				System.out.println("No");
				return;
			}
			else
			{
//				System.out.println("Take ");
				has += en.r;
			}
			if(has < 0)
			{
				System.out.println("No");
				return;
			}
		}
		
		long needtoget = has;
//		System.out.println("S2 need " + needtoget);
		has = 0;
		
		while(!q2.isEmpty())
		{
			Entry en = q2.poll();
//			System.out.println("Consider " + en.l + " " + en.r + ", has " + has);
			//take this
			if(en.l > has)
			{
				System.out.println("No");
				return;
			}
			else
			{
//				System.out.println("Take ");
				has += en.r;
			}
			if(has < 0)
			{
				System.out.println("No");
				return;
			}
		}
		
		if(has != needtoget)
		{
			System.out.println("No");
			return;
		}
		
		System.out.println("Yes");
	}
	

}