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)

#![allow(unused)]

use std::cmp::Reverse;

//
// Based on EbTech's Scanner https://stackoverflow.com/q/56449027
//
struct Scanner<'a, R: std::io::BufRead> {
    reader: R,
    buf: String,
    iter: std::str::SplitWhitespace<'a>,
}

impl<'a, R: std::io::BufRead> Scanner<'a, R> {
    fn new(reader: R) -> Self {
        Self { reader, buf: String::new(), iter: "".split_whitespace() }
    }

    fn get_str(&mut self) -> &str {
        if let Some(t) = self.iter.next() {
            return t;
        }
        self.buf.clear();
        self.reader.read_line(&mut self.buf).expect("I/O error");
        self.iter = unsafe { std::mem::transmute(self.buf.split_whitespace()) };
        self.get_str()
    }

    fn get<T: std::str::FromStr>(&mut self) -> T {
        self.get_str().parse().ok().expect("Parse error")
    }
}

fn main() {
    let stdin = std::io::stdin();
    solve(Scanner::new(stdin.lock()));
}

fn count_paren(s: &Vec<char>) -> (i64, i64) {
    s.iter().fold((0, 0), |(c, o), &p| {
        match p {
            '('        => (c, o + 1),
            _ if o > 0 => (c, o - 1),
            _          => (c + 1, o),
        }
    })
}

#[test]
fn test_count_paren() {
    assert_eq!(count_paren(&"(((((".chars().collect()), (0, 5));
    assert_eq!(count_paren(&")))))".chars().collect()), (5, 0));
    assert_eq!(count_paren(&")))((".chars().collect()), (3, 2));
    assert_eq!(count_paren(&"(()))".chars().collect()), (1, 0));
}

fn solve<R: std::io::BufRead>(mut sc: Scanner<R>) {
    let n: usize = sc.get();
    let ss: Vec<Vec<char>> =
        (0..n).map(|_| sc.get_str().chars().collect()).collect();

    let mut inc = vec![];  // 開き括弧のほうが多い
    let mut dec = vec![];  // 閉じ括弧のほうが多い
    let mut last = vec![]; // 閉じ括弧のみ
    for s in &ss {
        let (c, o) = count_paren(s);
        if o - c >= 0 {
            inc.push((c, o));
        } else if o > 0 {
            dec.push((c, o));
        } else {
            last.push((c, o));
        }
    }

    // 最初は '((..(' になる必要があるので，閉じ括弧の少ないものから順
    inc.sort_unstable_by_key(|&(c, _)| c);

    // 開き括弧の数に余裕があるうちに閉じ括弧の多いものから処理
    // 例えば '((((' '))))((' '))(' ')' は OK だが，それを並べ替えた
    //        '((((' '))(' '))))((' ')' は NG.
    // 閉じ括弧が同数なら開き括弧の多いものを先に持ってきて開き括弧の数を確保
    // 例えば '((' '))((' '))(' ')' は OK だが，それを並べ替えた
    //        '((' '))(' ))((' ')' は NG.
    dec.sort_unstable_by_key(|&(c, o)| (Reverse(c), Reverse(o)));

    // 最後は '))..)' になる必要がある．last は順不同．

    if Some(0) == inc.iter()
        .chain(dec.iter())
        .chain(last.iter())
        .fold(Some(0), |a, &(c, o)| {
            a.and_then(|x| if x - c < 0 { None } else { Some(x - c + o) })
        })
    {
        println!("Yes");
    } else {
        println!("No");
    }
}