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"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	N := getInt()
	s := make([]string, N)
	for i := 0; i < N; i++ {
		s[i] = getString()
	}
	c := make([][2]int, N)
	for i := 0; i < N; i++ {
		for _, v := range s[i] {
			if v == '(' {
				c[i][0]++
			} else {
				if c[i][0] == 0 {
					c[i][1]++
				} else {
					c[i][0]--
				}
			}
		}
	}
	sort.Slice(c, func(i, j int) bool {
		if c[i][1] == 0 {
			return true
		}
		if c[j][1] == 0 {
			return false
		}
		if c[i][0] == 0 {
			return false
		}
		if c[j][0] == 0 {
			return true
		}
		if c[i][0] == c[j][0] {
			return c[i][1] < c[j][1]
		}
		return c[i][0] > c[j][0]
	})

	if c[0][1] != 0 {
		out("No")
		return
	}

	l := c[0][0]
	r := c[0][1]
	cntl := l
	cntr := r
	for i := 1; i < N; i++ {
		cntl += c[i][0]
		cntr += c[i][1]
		x := min(l, c[i][1])
		y := min(r, c[i][0])
		l = l - x + c[i][0] - y
		r = r - y + c[i][1] - x
		// out(c[i], x, y, l, r)
	}
	if l == 0 && r == 0 && cntl == cntr {
		out("Yes")
	} else {
		out("No")
	}
}