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: Wrong Answer)

#[allow(unused_imports)]
use proconio::{input, marker::*};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

fn main() {
    input! {
        n: usize,
        s: [String;n],
    }

    use core::cmp::Reverse;

    macro_rules ! min { ( $ x : expr ) => ( $ x ) ; ( $ x : expr , $ ( $ z : expr ) ,+ ) => { { std :: cmp :: min ( $ x , min ! ( $ ( $ z ) ,* ) ) } } }
    let s = s
        .into_iter()
        .map(|e| {
            let mut low = 0isize;
            let mut pos = 0isize;
            for c in e.chars() {
                if c == '(' {
                    pos += 1;
                } else {
                    pos -= 1;
                }
                low = min!(low, pos);
            }
            (low, pos)
        })
        .collect::<Vec<_>>();

    let mut pos = s
        .clone()
        .into_iter()
        .filter(|e| e.1 >= 0)
        .collect::<Vec<_>>();
    let mut neg = s.into_iter().filter(|e| e.1 < 0).collect::<Vec<_>>();

    pos.sort_by_key(|e| Reverse(e.0));
    neg.sort_by_key(|e| e.0);

    debug!(pos, neg);
    let mut current = 0;
    for (low, cur) in pos.into_iter().chain(neg.into_iter()) {
        if current + low < 0 {
            println!("No");
            return;
        }
        current += cur;
        if current < 0 {
            println!("No");
            return;
        }
    }
    if current == 0 {
        println!("Yes");
    } else {
        println!("No");
    }
}