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 Kotlin (1.3.71) (judge verdict: Wrong Answer)

import java.util.*

class Scanner(`in`: java.io.InputStream) {
    private val reader = java.io.BufferedReader(java.io.InputStreamReader(`in`))
    private var tokenizer: StringTokenizer? = null

    fun next(): String {
        if (tokenizer?.hasMoreTokens() != true) {
            tokenizer = StringTokenizer(reader.readLine())
        }
        return tokenizer!!.nextToken()
    }

    fun nextLine(): String {
        return if (tokenizer?.hasMoreTokens() != true) {
            reader.readLine()
        } else tokenizer!!.nextToken("\n")
    }
}

fun main(args: Array<String>) {
    val pw = java.io.PrintWriter(System.out)
    Problem.solve(Scanner(System.`in`), pw)
    pw.flush()
}

////////////////////////////////////////////////////////////////////////

fun sumList(a: Iterable<Long>): List<Long> {
    val result = mutableListOf(0L)
    var i = 0L
    a.forEach {
        i += it
        result.add(i)
    }
    return result
}

////////////////////////////////////////////////////////////////////////

object Problem {
    fun solve(sc: Scanner, pw: java.io.PrintWriter) {
        val N = sc.next().toInt()
        val S = Array(N) { sc.next() }
        val S1 = mutableListOf<Pair<Long, Long>>()
        val S2 = mutableListOf<Pair<Long, Long>>()
        S.forEach { s ->
            var min = Long.MAX_VALUE
            var sum = 0L
            s.forEach { c ->
                if (c == '(') sum++ else sum--
                if (min > sum) min = sum
            }
            if (min >= 0) {
                S1.add(Pair(sum, min))
            } else {
                S2.add(Pair(sum, min))
            }
        }
        val S3 = S1.sortedBy { it.first }.reversed() + S2.sortedBy { it.first }.reversed()
        var n = 0L
        S3.forEach {
            val sum = it.first
            val min = it.second
            if (n + min < 0) {
                pw.println("No")
                return@solve
            }
            n += sum
        }
        pw.println(if (n == 0L) "Yes" else "No")
    }
}