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: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Objects;
import java.util.List;
import java.util.Scanner;
import java.util.Comparator;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BracketSequencing solver = new BracketSequencing();
        solver.solve(1, in, out);
        out.close();
    }

    static class BracketSequencing {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();

            List<Pair> positivePair = new ArrayList<>();
            List<Pair> negativePair = new ArrayList<>();
            int total = 0;

            for (int i = 0; i < n; i++) {
                String line = in.next();
                int m = 0;
                int s = 0;
                for (char c : line.toCharArray()) {
                    s += ('(' == c) ? 1 : -1;
                    m = Math.min(m, s);
                }
                if (s >= 0) {
                    positivePair.add(new Pair((long) m, (long) s));
                } else {
                    negativePair.add(new Pair((long) m - s, (long) -s));
                }

                total += s;
            }
            if (total != 0) {
                out.append("No\n");
                return;
            }

            positivePair.sort(Comparator.reverseOrder());
            negativePair.sort(Comparator.reverseOrder());

            if (!check(positivePair) || !check(negativePair)) {
                out.append("No\n");
                return;
            }
            out.append("Yes\n");

        }

        public boolean check(List<Pair> pairs) {
            int s = 0;
            for (Pair p : pairs) {
                int m = s + p.first.intValue();
                if (m < 0) {
                    return false;
                }
                s += p.second.intValue();
            }
            return true;

        }

        class Pair implements Comparable<BracketSequencing.Pair> {
            public final Long first;
            public final Long second;

            Pair(Long first, Long second) {
                this.first = first;
                this.second = second;
            }

            public boolean equals(Object obj) {
                if (!(obj instanceof E.Pair))
                    return false;
                E.Pair pair = (E.Pair) obj;
                return pair.first.equals(first) && pair.second.equals(second);
            }

            public int hashCode() {
                return Objects.hash(first, second);
            }

            public int compareTo(Pair o) {
                int res = Long.compare(first, o.first);
                return res != 0 ? res : Long.compare(second, o.second);
            }

        }

    }

    static class E {
        class Pair implements Comparable<E.Pair> {
            public final Long first;
            public final Long second;

            Pair(Long first, Long second) {
                this.first = first;
                this.second = second;
            }

            public boolean equals(Object obj) {
                if (!(obj instanceof Pair))
                    return false;
                Pair pair = (Pair) obj;
                return pair.first.equals(first) && pair.second.equals(second);
            }

            public int hashCode() {
                return Objects.hash(first, second);
            }

            public int compareTo(Pair o) {
                int res = Long.compare(first, o.first);
                return res != 0 ? res : Long.compare(second, o.second);
            }

        }

    }
}