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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	n := scanInt()
	ss := make([]string, n)
	for i := 0; i < n; i++ {
		s := scanString()
		p := 0
		for p+1 < len(s) {
			if s[p:p+2] == "()" {
				s = s[:p] + s[p+2:]
				p = max(0, p-1)
			} else {
				p++
			}
		}
		ss[i] = s
	}
	// debug(ss)

	c := make([]pii, n)
	for i := 0; i < n; i++ {
		for _, e := range ss[i] {
			if e == ')' {
				c[i].a++
			} else {
				c[i].b++
			}
		}
	}
	sort.Slice(c, func(i, j int) bool {
		a := c[i].b - c[i].a
		b := c[j].b - c[j].a

		if a == b {
			if a >= 0 {
				return c[i].a < c[j].a
			}
			return c[i].b > c[j].b
		}
		return a > b
	})

	// debug(c)

	a := 0
	for i := 0; i < n; i++ {
		if a < c[i].a {
			fmt.Fprintln(wr, "No")
			return
		}
		a -= c[i].a
		a += c[i].b
	}
	if a != 0 {
		fmt.Fprintln(wr, "No")
	} else {
		fmt.Fprintln(wr, "Yes")
	}
}

type pii struct{ a, b int }