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 11.0.6) (judge verdict: Runtime Error)

import java.io.*;
import java.util.*;

public class Main {
//    static PrintWriter out = new PrintWriter(System.out);
//    static Scanner sc = new Scanner(System.in);
//    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] rec = new int[n][];
        ArrayList<int[]> list = new ArrayList<>();
        int total = 0;
        for(int i=0;i<n;i++){
            String s = sc.next();
            int left=0, mini = 0;
            for(char c:s.toCharArray()){
                if(c=='(') left++;
                else left--;
                mini = Math.min(mini,left);
            }
            total += left;
//            rec[i] = new int[]{left, mini};
            list.add(new int[]{left, mini});
        }
        if(total!=0){
            System.out.println("No"); System.exit(0);
        }
//        Arrays.sort(rec,(a,b)->(Integer.compare(Math.min(b[1],b[0]+a[1]),Math.min(a[1],a[0]+b[1]))));
        Collections.sort(list,(a,b)->(Integer.compare(Math.min(b[1],b[0]+a[1]),Math.min(a[1],a[0]+b[1]))));
        int left = 0;
        String ans;
        for(int[] e:list){
            if(left+e[1]<0){
                System.out.println("No"); System.exit(0);
            }
            left += e[0];
        }
        ans = left==0?"Yes":"No";
        System.out.println(ans);
    }
    /*
    String solve(){
        int n = sc.nextInt();
        int[][] rec = new int[n][];
        for(int i=0;i<n;i++){
            String s = sc.next();
            rec[i] = helper(s,i);
        }
        Arrays.sort(rec,new myC());
        int left = 0;
        for(int[] e:rec){
            if(left+e[1]<0) return "No";
            left += e[0];
        }
        return left==0?"Yes":"No";
    }
    int[] helper(String s, int idx){
        int left = 0, mini = 100;
        for(char c:s.toCharArray()){
            if(c=='(') left++;
            else left--;
            mini = Math.min(mini,left);
        }
        return new int[]{left, mini, idx};
    }
    class myC implements Comparator<int[]>{
        public int compare(int[] a, int[] b){
            int res1 = Math.min(a[1], a[0]+b[1]);
            int res2 = Math.min(b[1],b[0]+a[1]);
            return Integer.compare(res2, res1); // the bigger, the fronter
        }
    }*/
}