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)]

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 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();
    
    /*
    '(' を +1, ')' を -1 として，一度もマイナスにならず最終的に和が 0 になる足し算の
    順序を作れるか？

    数字のの大きいものから順に足し算すればよい？

    2相 (increase, decrease) で考える

    increase:
        1. minzero: min = 0, sum >= 0 であるもの
        2. positive:  min < 0, sum >= 0 であるもの (min が大きい順)
    decrease
        1. negative: sum < 0 であるもの
           (min が小さい順 = 大きな減少に耐えられるうちに処理する)
    */

    let mut minzero = vec![];
    let mut positive = vec![];
    let mut negative = vec![];
    for s in ss {
        let (sum, min) = s.iter().fold((0, 0), |(sum, min), &c| {
            let sum_ = sum + if c == '(' { 1 } else { -1 };
            (sum_, std::cmp::min(min, sum))
        });

        if sum >= 0 {
            if min >= 0 {
                minzero.push((sum, min));
            } else {
                positive.push((sum, min));
            }
        } else {
            negative.push((sum, min));
        }
    }

    positive.sort_unstable_by_key(|&(sum, min)| (Reverse(min), Reverse(sum)));
    negative.sort_unstable_by_key(|&(sum, min)| (min, Reverse(sum)));

    if Some(0) == minzero.iter()
        .chain(positive.iter())
        .chain(negative.iter())
        .fold(Some(0), |a, &(sum, min)| {
            a.and_then(|x| if x + min < 0 { None } else { Some(x + sum) })
        })
    {
        println!("Yes");
    } else {
        println!("No");
    }
}