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.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;

public class Main {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        int n = sc.nextInt();
        String[] s = new String[n];
        // long[][] data = new long[n][4]; // sum, max-
        PriorityQueue<Integer[]> pq1 = new PriorityQueue<>((a, b) -> b[1] - b[1]);
        PriorityQueue<Integer[]> pq2 = new PriorityQueue<>((a, b) -> b[1] - b[1]);
        PriorityQueue<Integer[]> pq3 = new PriorityQueue<>((a, b) -> b[1] - b[1]);

        for (int i = 0; i < n; i++) {
            s[i] = sc.next();
            int mp = 0;
            int mm = 0;
            int m = 0;
            int min = 0;
            for (int j = 0; j < s[i].length(); j++) {
                if(s[i].charAt(j) == '('){
                    mp++;
                    m++;
                }else{
                    mm++;
                    m--;
                }
                min = Math.min(min, m);
            }
            if(m > 0){
                pq1.add(new Integer[]{m, min});
            }else if( m < 0){
                int m2 = 0;
                int min2 = 0;
                for (int j = s[i].length()-1; j >= 0; j--) {
                    if(s[i].charAt(j) == ')'){
                        m2++;
                    }else{
                        m2--;
                    }
                    min2 = Math.min(min2, m2);
                }
                pq2.add(new Integer[]{m2, min2});
            }else{
                pq3.add(new Integer[]{m, min});
            }
        }

        boolean ok = true;
        int pos1 = 0;
        int pos2 = 0;
        while(!pq1.isEmpty()){
            Integer[] mm = pq1.poll();
            if(pos1 + mm[1] < 0) ok = false;
            pos1 += mm[0];
        }
        while(!pq2.isEmpty()){
            Integer[] mm = pq2.poll();
            if(pos1 + mm[1] < 0) ok = false;
            pos2 += mm[0];
        }
        
        if(pos1 != pos2) ok = false;
        while(!pq3.isEmpty()){
            Integer[] mm = pq3.poll();
            if(pos1 + mm[1] < 0) ok = false;
        }

        if(ok){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }


    private static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;

        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (buflen <= 0) {
                    return false;
                }
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte())
                return buffer[ptr++];
            else
                return -1;
        }

        private static boolean isPrintableChar(int c) {
            return 33 <= c && c <= 126;
        }

        public boolean hasNext() {
            while (hasNextByte() && !isPrintableChar(buffer[ptr]))
                ptr++;
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public long nextLong() {
            if (!hasNext())
                throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while (true) {
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                } else if (b == -1 || !isPrintableChar(b)) {
                    return minus ? -n : n;
                } else {
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }

        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
                throw new NumberFormatException();
            return (int) nl;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

}