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)

use std::cmp::Reverse;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();

    let mut positive = vec![];
    let mut negative = vec![];
    for _ in 0..n {
        let s = sc.chars();
        let (left, right) = compile(s);

        if left - right > 0 {
            positive.push((left, right));
        } else {
            negative.push((left, right));
        }
    }

    positive.sort_by_key(|&(_, right)| right);

    let mut left_stack = 0;
    for (left, right) in positive.into_iter() {
        if left_stack < right {
            println!("No");
            return;
        }
        left_stack -= right;
        left_stack += left;
    }

    negative.sort_by_key(|&(left, _)| left);
    let mut right_stack = 0;
    for (left, right) in negative.into_iter() {
        if right_stack < left {
            println!("No");
            return;
        }
        right_stack -= left;
        right_stack += right;
    }

    if left_stack == right_stack {
        println!("Yes");
    } else {
        println!("No");
    }
}

fn compile(s: Vec<char>) -> (i64, i64) {
    let mut left = 0;
    let mut right = 0;
    for c in s.into_iter() {
        match c {
            '(' => {
                left += 1;
            }
            ')' => {
                if left > 0 {
                    left -= 1;
                } else {
                    right += 1;
                }
            }
            _ => unimplemented!(),
        }
    }
    (left, right)
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}