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)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::suspicious_arithmetic_impl)]

// using Rust(1.42.0)
// My Library Repo is at "https://github.com/Loptall/prcn_lib"
// check available crates on AtCoder at "https://atcoder.jp/contests/language-test-202001"

/*
    AtCoder Language Update 2020204 お疲れ様でした! 有難いです(p.s. 過去問対応もありがとうございます)
*/

// from std...
use std::cmp::{
    max, min,
    Ordering::{Equal, Greater, Less},
};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::mem::swap;
use std::ops::*;
use std::usize::{MAX, MIN};
use std::{i128, i16, i32, i64, i8, isize, u128, u16, u32, u64, u8, usize};

// Dep crates are...
use itertools::*;
use itertools_num::*;
use lazy_static::lazy_static;
use maplit::{btreemap, btreeset, hashmap, hashset};
use num_bigint::{BigInt, BigUint};
use num_integer::{binomial, gcd, lcm, Integer};
use permutohedron::Heap;
use petgraph::unionfind::UnionFind;
use proconio::{fastout, input, is_stdin_empty, marker::*};
use rand::random;

// code...
// #[fastout]
fn main() {
    input! {
        n: usize,
        s: [Chars; n]
    }

    // (収支, 貯金, chose?)
    let mut a: Vec<(usize, (i64, i64, bool))> = s.iter().map(an).enumerate().collect::<Vec<_>>();

    let mut stock = 0;
    let mut ans = "Yes";
    for _i in 0..n {
        let which = a
            .iter()
            .filter(|(_, x)| !x.2 && x.1 <= stock)
            .max_by_key(|(_, x)| x.0)
            .unwrap_or(&(std::usize::MAX, (0, 0, false)))
            .clone();
        if which.0 > n {
            ans = "No";
            break;
        }
        (a[which.0].1).2 = true;
        stock += (which.1).0;
        if stock < 0 {
            ans = "No";
            break;
        }
    }

    if stock > 0 {
        ans = "No";
    }

    println!("{}", ans);
}

fn an(s: &Vec<char>) -> (i64, i64, bool) {
    let mut gain = 0;
    let mut debt = 0;
    for c in s {
        match *c {
            '(' => {
                gain += 1;
            }
            ')' => {
                gain -= 1;
                debt = min(debt, gain);
            }
            _ => unreachable!(),
        }
    }
    (gain, -debt, false)
}