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 Go (1.14.1) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)
	s := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &s[i])
	}

	l, r := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		var prev byte
		var cnt int
		for j := 0; j < len(s[i]); j++ {
			if prev != s[i][j] && s[i][j] == '(' && cnt < 0 {
				cnt = 0
			}
			if s[i][j] == '(' {
				cnt++
			} else {
				cnt--
			}
			prev = s[i][j]
		}
		if cnt > 0 {
			l[i] = cnt
		}
		cnt = 0
		for j := len(s[i]) - 1; j >= 0; j-- {
			if prev != s[i][j] && s[i][j] == ')' && cnt < 0 {
				cnt = 0
			}
			if s[i][j] == ')' {
				cnt++
			} else {
				cnt--
			}
			prev = s[i][j]
		}
		if cnt > 0 {
			r[i] = cnt
		}
	}

	var sum [2]int
	var ok [2]bool
	for i := 0; i < n; i++ {
		sum[0] += l[i]
		sum[1] += r[i]
		if l[i] == 0 && r[i] > 0 {
			ok[0] = true
		}
		if r[i] == 0 && l[i] > 0 {
			ok[1] = true
		}
	}

	if sum[0] == sum[1] {
		if sum[0] == 0 || (ok[0] && ok[1]) {
			fmt.Println("Yes")
			return
		}
	}
	fmt.Println("No")
}