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)

//package abc167.F

import java.io.*
import java.math.*
import java.util.*
import kotlin.math.*

var reader: BufferedReader = System.`in`.bufferedReader()
var writer: PrintWriter = PrintWriter(System.`out`)

fun next() = reader.readLine()!!
fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextDouble() = next().toDouble()
fun nextStrings() = next().split(" ")
fun nextInts() = nextStrings().map { it.toInt() }
fun nextLongs() = nextStrings().map { it.toLong() }
fun nextDoubles() = nextStrings().map { it.toDouble() }

fun put(v: Any?) = writer.print(v)
fun puts() = writer.println()
fun puts(v: Any?) = writer.println(v)

var debug = false
fun dp(v: Any?) {
    if (debug) println(v)
}
fun dp() {
    if (debug) println()
}

fun test(case: Int, input: String, expected: String) {
    reader = input.trimIndent().reader().buffered()

    val stringWriter = StringWriter()
    writer = PrintWriter(stringWriter)

    solve()
    writer.flush()

    val actual = stringWriter.toString()

    println("[test $case]")
    if ("${expected}" == actual) {
        println("OK")
    } else {
        println("NG")
        println()
        println("expected: \"${expected}\"")
        println("actual:   \"${actual}\"")
    }
    println()
}

@Suppress("UNUSED_PARAMETER")
fun xtest(case: Int, input: String, expected: String) {}

fun main(args: Array<String>) {
    if ((args.size == 1) && (args[0] == "test")) {
        testAll()
    } else {
        solve()
        writer.flush()
    }
}

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

fun testAll() {
    debug = true

    test(1,
    """
    2
    )
    (()
    """,
    "Yes"
    )
    test(2,
    """
    2
    )(
    ()
    """,
    "No"
    )
    test(3,
    """
    4
    ((()))
    ((((((
    ))))))
    ()()()
    """,
    "Yes")
    test(4,
    """
    3
    (((
    )
    )
    """,
    "No")
    test(5,
    """
    2
    (()
    )()
    """,
    "Yes")
    test(6,
    """
    1
    (
    """,
    "No")
}

fun doSolve(): Boolean {

    val N = nextInt()

    var total = 0
    var hasLeft = false
    var hasRight = false
    var hasNotBaranced = false
    repeat(N) {
        var r = 0
        var l = 0
        var pre = true

        val str = next()
        for (c in str) {
            if (c == '(') {
                pre = false
                ++total
                ++l
            } else {
                --total
                if (pre) {
                    ++r
                } else {
                    --l
                }
            }
        }
        if (l < 0) {
            r += -l
            l = 0
        }

        if ((l != 0) || (r != 0)) {
            hasNotBaranced = true
        }
        if ((l == 0) && (r != 0)) {
            hasRight = true
        }
        if ((r == 0) && (l != 0)) {
            hasLeft = true
        }
        //dp("[$l, $r]: $total($hasNotBaranced, $hasLeft, $hasRight)")
    }

    if (total != 0) {
        return false
    }
    if (!hasNotBaranced) {
        return true
    }
    if (hasLeft && hasRight) {
        return true
    }
    return false
}

fun solve() {
    if (doSolve()) {
        put("Yes")
    } else {
        put("No")
    }
}