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: Wrong Answer)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.solve();
    }

    public void solve() {
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int N = scan.nextInt();
        String[] list = new String[N];
        for (int i = 0; i < N; i++) {
            list[i] = scan.next();
        }
        for (int i = 0; i < N; i++) {
            char[] d = list[i].toCharArray();
            int len = d.length;
            StringBuilder sb = new StringBuilder();
            int current = 0;
            int size = 0;
            while (current < len) {
                if (d[current] == '(') {
                    sb.append(d[current]);
                    current += 1;
                    size += 1;
                    continue;
                }
                if (sb.length() == 0) {
                    sb.append(d[current]);
                    current += 1;
                    size += 1;
                    continue;
                }
                char x = sb.charAt(size-1);
                if (x == '(') {
                    sb.deleteCharAt(size-1);
                    current += 1;
                    size -= 1;
                    continue;
                }
                sb.append(d[current]);
                current += 1;
                size += 1;
            }
            list[i] = sb.toString();
        }
        List<Word> words = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            if ("".equals(list[i])) {
                continue;
            }
            Word word = new Word(list[i]);
            words.add(word);
        }
        words.sort(new WordComparator());
        int len = words.size();
        int left_length = 0;
        boolean ng = false;
        for (int i = 0; i < len; i++) {
            Word word = words.get(i);
            if (left_length < word.right_length) {
                ng = true;
                break;
            }
            left_length -= word.right_length;
            left_length += word.left_length;
        }
        if (left_length != 0) {
            ng = true;
        }
        if (ng) {
            System.out.println("No");
        } else {
            System.out.println("Yes");
        }
    }
    class WordComparator implements Comparator<Word> {
        @Override
        public int compare(Word o1, Word o2) {
            if (o1.left_start != o2.left_start) {
                return -Integer.compare(o1.left_start, o2.left_start);
            }
            if (o1.right_end != o2.right_end) {
                return Integer.compare(o1.right_end, o2.right_end);
            }
            if (o1.right_length != o2.right_length) {
                return Integer.compare(o1.right_length, o2.right_length);
            }
            return Integer.compare(o1.left_length, o2.left_length);
        }
    }
    class Word {
        String S;
        // (
        int left_length;
        // )
        int right_length;
        // 1 end (
        int left_start;
        // 1 end )
        int right_end;
        Word(String S) {
            char[] d = S.toCharArray();
            int len = d.length;
            int left_length = 0;
            int left_start = d[0] == '(' ? 1 : 0;
            int right_length = 0;
            int right_end = d[len-1] == ')' ? 1 : 0;
            for (int i = 0; i < len; i++) {
                if (d[i] == '(') {
                    left_length += 1;
                    continue;
                }
                right_length += 1;
            }
            this.S = S;
            this.left_length = left_length;
            this.left_start = left_start;
            this.right_length = right_length;
            this.right_end = right_end;
        }
    }

}