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_imports)]
#![allow(unused_macros)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
#![allow(unused_assignments)]
use proconio::input;
use proconio::marker::Usize1;
use std::collections::*;
use std::cmp::*;
use std::f64::consts::*;

const MOD: u64 = 1000000007;
const INF: usize = std::usize::MAX / 4;

fn main() {
    input! {
        N: usize,
        S: [String; N],
    }

    let mut A = vec![];
    for s in &S {
        let (mut cum, mut mi): (i64, i64) = (0, 0);
        for t in s.chars() {
            if t == '(' {
                cum += 1;
            } else {
                cum -= 1;
            }
            mi = min(mi, cum);
        }
        A.push((-mi, cum - mi));
    }

    let mut P: Vec<(i64, i64)> = A.iter().filter(|(l, r)| *l <= *r).cloned().collect();
    let mut N: Vec<(i64, i64)> = A.iter().filter(|(l, r)| *l > *r).cloned().collect();

    P.sort_by(|l, r| l.0.cmp(&r.0));
    N.sort_by(|l, r| l.1.cmp(&r.1));
    N.reverse();

    let mut M: i64 = 0;

    for (l, r) in &P {
        M -= l;
        if M < 0 {
            println!("No");
            return;
        }
        M += r;
    }
    for (l, r) in &N {
        M -= l;
        if M < 0 {
            println!("No");
            return;
        }
        M += r;
    }

    if M == 0 {
        println!("Yes");
    } else {
        println!("No");
    }
}