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.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        //左右の閉じ括弧不足分(0:左、1:右、2:左－右)
        int[][] part = new int[N][3];
        //左右の閉じ括弧不足分が0の個数(0:左、1:右、2:両方)
        int[] zeroNum = new int[3];
        for(int i = 0 ; i < N ; i++){
            String str = sc.next();
            int leftPart = 0;
            int rightCount = 0;
            for(int j = 0 ; j < str.length() ; j++){
                boolean isopen = str.charAt(j) == ')';
                if(isopen){
                    if(rightCount > 0){
                        rightCount--;
                    }else{
                        leftPart++;
                    }
                }else{
                    rightCount++;
                }
            }
            part[i][0] = leftPart;
            int rightPart = 0;
            int leftCount = 0;
            for(int j = str.length()-1 ; j >= 0 ; j--){
                boolean isopen = str.charAt(j) == '(';
                if(isopen){
                    if(leftCount > 0){
                        leftCount--;
                    }else{
                        rightPart++;
                    }
                }else{
                    leftCount++;
                }
            }
            part[i][1] = rightPart;
            part[i][2] = part[i][0]-part[i][1];

            if(part[i][0] == 0){
                if(part[i][1] == 0){
                    zeroNum[2]++;
                }else{
                    zeroNum[0]++;
                }
            }else if(part[i][1] == 0){
                zeroNum[1]++;
            }
        }

        int diff = 0;
        for(int i = 0 ; i < N ; i++){
            diff += part[i][2];
        }
        if(diff != 0){
            System.out.println("No");
        }else{
            if(zeroNum[0] > 0 && zeroNum[1] > 0){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }
}