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 Rust (1.42.0) (judge verdict: Accepted)

//! Framework <https://github.com/vain0x/procon>

#![allow(non_snake_case)]
#![allow(unused_imports)]

use std::collections::*;

pub struct Scan(Box<dyn Iterator<Item = &'static str>>); // '

impl Scan {
    fn new() -> Self {
        let mut buf = String::new();
        let read_line = move || {
            std::io::stdin().read_line(&mut buf).unwrap();
            Box::leak(buf.split_off(0).into_boxed_str()).split_whitespace()
        };
        Scan(Box::new(std::iter::repeat_with(read_line).flatten()))
    }

    pub fn word<T: std::str::FromStr>(&mut self) -> T {
        self.0.next().unwrap().parse().ok().unwrap()
    }

    pub fn list<T: std::str::FromStr>(&mut self, len: usize) -> Vec<T> {
        std::iter::repeat_with(|| self.word()).take(len).collect()
    }
}

// 解説AC

use std::cmp::Reverse;

/// カッコからなる文字列を解析した結果
struct Brackets {
    /// 文字列内に対応する '(' がない ')' の個数。(editorials の A(i))
    down: i64,
    /// 文字列内に対応する ')' がない '(' の個数。(B(i))
    up: i64,
}

fn calc(s: &str) -> Brackets {
    let mut left = 0;
    let mut right = 0;
    let mut down = 0;

    for c in s.chars() {
        match c {
            '(' => left += 1,
            ')' => right += 1,
            _ => unreachable!(),
        };

        down = down.max(right - left);
    }

    let up = left - (right - down);
    Brackets { down, up }
}

fn main() {
    let mut scan = Scan::new();

    let N = scan.word::<usize>();
    let S = scan.list::<String>(N);

    // '(' が多い、なるべく先に連結したいカッコ列
    let mut good = vec![];
    // ')' が多い、なるべく後まで連結したくないカッコ列
    let mut bad = vec![];

    for s in S {
        let b = calc(&s);
        if b.up > b.down {
            good.push(b);
        } else {
            bad.push(b);
        }
    }

    good.sort_by_key(|b| b.down);
    bad.sort_by_key(|b| Reverse(b.up));

    let mut balance = 0;
    let mut ok = true;
    for &Brackets { down, up } in good.iter().chain(&bad) {
        if balance < 0 || balance < down {
            ok = false;
            break;
        }

        balance -= down;
        balance += up;
    }
    ok = ok && balance == 0;

    println!("{}", if ok { "Yes" } else { "No" });
}