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: Accepted)

//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,
    """
    3
    (
    )
    ))((
    """,
    "No")
    test(6,
    """
    4
    (
    )
    ))(
    )((
    """,
    "Yes")
    test(7,
    """
    4
    (
    ))
    )(
    )((
    """,
    "Yes")
    test(8,
    """
    4
    ((
    )
    ))(
    )(
    """,
    "Yes")
    test(9,
    """
    4
    (((
    )))
    )(
    ))))))))))((((((((((
    """,
    "No"
    )
    test(10,
    """
    5
    (((
    )))
    ))((((((((((
    ))))))))))(((
    ))))))))))(((((((((
    """,
    "Yes"
    )
    test(11,
    """
    4
    ((((((((((
    )))(
    )))))))))((
    )
    """,
    "Yes"
    )
    test(12,
    """
    4
    ((((((((((
    )))((((
    )))))))))))(
    )
    """,
    "Yes"
    )
    test(13,
    """
    4
    ((((((((((
    )((
    )))))))))))(((((
    )))))
    """,
    "Yes"
    )
    test(14,
    """
    5
    ((((((((((
    )((
    )))))(((
    )))))))))))(((((
    )))
    """,
    "Yes"
    )
}

typealias Seq = Pair<Int, Int>

fun doSolve(): Boolean {

    val N = nextInt()

    var total = 0
    var totalL = 0
    var totalR = 0

    val Mp = mutableListOf<Seq>()
    val Mm = mutableListOf<Seq>()

    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)) {
            totalR += r
        }
        if ((r == 0) && (l > 0)) {
            totalL += l
        }

        if ((l > 0) && (r > 0)) {
            if (r < l) {
                Mp.add(Seq(r, l))
            } else {
                Mm.add(Seq(r, l))
            }
        }

        //dp("[$l, $r]: $totalL - $totalR")
    }

    if (total != 0) {
        dp("pat1(NG)")
        return false
    }
    if (Mp.isEmpty() && Mm.isEmpty()) {
        dp("pat2(OK)")
        return true
    }

    Mp.sortWith(compareBy(Seq::first))
    Mm.sortWith(compareByDescending(Seq::first)
        .thenByDescending(Seq::second))

    //dp(Mp)
    //dp(Mm)

    total = totalL
    for (seq in Mp) {
        //dp("seq(p): $total, $seq")

        if (total < seq.first) {
            dp("pat3(NG)")
            return false
        }

        total -= seq.first
        total += seq.second
    }
    for (seq in Mm) {
        //dp("seq(m): $total, $seq")

        if (total < seq.first) {
            dp("pat3(NG)")
            return false
        }

        total -= seq.first
        total += seq.second
    }
    //dp("$total")

    if (total == totalR) {
        dp("pat4(OK)")
        return true
    }

    dp("pat5(NG)")
    return false
}

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