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"
	"container/heap"
	"fmt"
	"math"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 4
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}

	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	ss := &brs{}
	heap.Init(ss)
	for i := 0; i < n; i++ {
		s := getNextString(scanner)
		b := newbr(s)
		for j := 0; j < len(s); j++ {
			if s[j] == '(' {
				b.dif++
			} else {
				b.dif--
			}
			if b.min > b.dif {
				b.min = b.dif
			}
		}
		heap.Push(ss, b)
	}

	dd := 0
	for ss.Len() > 0 {
		ret := make(brs, 0)
		p := heap.Pop(ss).(br)
		for ss.Len() > 0 && dd+p.min < 0 {
			ret = append(ret, p)
			p = heap.Pop(ss).(br)
		}
		if dd+p.min < 0 {
			fmt.Fprintln(writer, "No")
			return
		}
		dd += p.dif
		for _, r := range ret {
			heap.Push(ss, r)
		}
	}
	if dd == 0 {
		fmt.Fprintln(writer, "Yes")
		return
	}
	fmt.Fprintln(writer, "No")
}

func newbr(s string) br {
	return br{
		s:   s,
		dif: 0,
		min: math.MaxInt32,
	}
}

type br struct {
	s        string
	dif, min int
}

type brs []br

func (h brs) Len() int { return len(h) }
func (h brs) Less(i, j int) bool {
	if h[i].dif == h[j].dif {
		return h[i].min > h[j].min
	}
	return h[i].dif > h[j].dif
}
func (h brs) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *brs) Push(x interface{}) {
	*h = append(*h, x.(br))
}
func (h *brs) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}