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.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import java.util.*;

public class Main {
    long MOD = (long) 1e9 + 7;

    InputStream in;
    OutputStream out;

    public Main() {
        this(System.in, System.out);
    }

    public Main(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    void run() {
        Scanner sc = new Scanner(this.in);
        PrintWriter out = new PrintWriter(this.out);

        // write your code
        int n = sc.nextInt();
        String[] kk = new String[n];
        for (int i = 0; i < n; i++) {
            kk[i] = sc.next();
        }

        Pair[] ps = new Pair[n];
        for (int i = 0; i < n; i++) {
            int l = kk[i].length();
            int cl = 0, ml = 0, cr = 0, mr = 0;
            for (int j = 0; j < l; j++) {
                cl += kk[i].charAt(j) == '(' ? 1 : -1;
                ml = Math.min(ml, cl);
                cr += kk[i].charAt(l - j - 1) == ')' ? 1 : -1;
                mr = Math.min(mr, cr);
            }
            ps[i] = new Pair(-mr, -ml, kk[i]);
        }

        Arrays.sort(ps);
        debug(ps);

        int c = 0;
        boolean ok = true;
        for (Pair p : ps) {
            if (c - p.m < 0) {
                ok = false;
                break;
            }
            c -= p.m;
            c += p.p;
        }

        ok &= c == 0;
        out.println(ok ? "Yes" : "No");

        out.flush();
    }

    class Pair implements Comparable<Pair> {
        int p, m;
        String s;
        Pair(int p, int m, String s) {
            this.p = p;
            this.m = m;
            this.s = s;
        }

        @Override
        public int compareTo(Pair o) {
            if (p != o.p) {
                return o.p - p;
            }
            return m - o.m;
        }

        @Override
        public String toString() {
            return "(" + p + ", " + m + ")";
        }
    }

    void debug(Object... os) {
        System.err.println(Arrays.deepToString(os));
    }

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