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)

use std::cmp::Ordering;
use std::cmp::max;

fn solve(mut ss: Vec<(i32, i32)>) -> bool {
    ss.sort_by(|&a, &b|{
        if (a.0 - a.1) < 0 && (b.0 - b.1) > 0 {
            return Ordering::Less;
        }
        if (a.0 - a.1) > 0 && (b.0 - b.1) < 0 {
            return Ordering::Greater;
        }
        if (a.0 - a.1) > 0 {
            max(b.0, b.1).cmp(&max(a.0, a.1))
        } else {
            max(a.0, a.1).cmp(&max(b.0, b.1))
        }
    });
    dbg!(&ss);
    if let (Some(first), Some(last)) = (ss.first(), ss.last()) {
        if first.0 != 0 || last.1 != 0 {
            return false;
        }
    }
    let mut sum = 0;
    for s in ss {
        sum -= s.0;
        if sum < 0 {
            return false;
        }
        sum += s.1;
        if sum < 0 {
            return false;
        }
    }
    if sum != 0 {
        return false;
    }
    true
}

fn main() {
    let mut io = IO::new();

    let n = io.read();
    let mut ss = Vec::new();

    for _ in 1..=n {
        let mut left = 0;
        let mut right = 0;

        loop {
            match io.read_raw_ascii() {
                b'(' => right += 1,
                b')' => if right > 0 { right -= 1 } else { left +=1 },
                b'\n' => break,
                _ => panic!(),
            }
        }
        ss.push((left, right));
    }

    let ans = solve(ss);

    io.writeln(if ans { "Yes" } else { "No" });
}
 


// PROCON LIBRARY //

pub trait Num: Sized {
    fn to_i(self) -> isize;
    fn to_u(self) -> usize { self.to_i() as usize }
    fn off<T: Num>(self, other: T) -> usize { (self.to_i() + other.to_i()).to_u() }
}
impl Num for usize { fn to_i(self) -> isize { self as isize } }
impl Num for isize { fn to_i(self) -> isize { self as isize } }
impl Num for u32 { fn to_i(self) -> isize { self as isize } }
impl Num for i32 { fn to_i(self) -> isize { self as isize } }
impl Num for u64 { fn to_i(self) -> isize { self as isize } }
impl Num for i64 { fn to_i(self) -> isize { self as isize } }

pub struct IdxIterMut<'a, T> {
    iter: std::iter::Enumerate<std::slice::IterMut<'a, T>>,
}

impl<'a, T> Iterator for IdxIterMut<'a, T> {
    type Item=(usize, &'a mut T);
    fn next(&mut self) -> Option<(usize, &'a mut T)> {
        self.iter.next().map(|(i, v)| (i+1, v))
    }
}

pub struct IdxIter<'a, T> {
    iter: std::iter::Enumerate<std::slice::Iter<'a, T>>,
}

impl<'a, T> Iterator for IdxIter<'a, T> {
    type Item=(usize, &'a T);
    fn next(&mut self) -> Option<(usize, &'a T)> {
        self.iter.next().map(|(i, v)| (i+1, v))
    }
}

pub trait SliceExt<T> {
    fn at<I: Num>(&mut self, i: I) -> &mut T;
    fn iter_idx(&self) -> IdxIter<'_, T>;
    fn iter_idx_mut(&mut self) -> IdxIterMut<'_, T>;
}
impl<T> SliceExt<T> for [T] {
    fn at<I: Num>(&mut self, i: I) -> &mut T {
        &mut self[i.to_u() + 1]
    }
    fn iter_idx(&self) -> IdxIter<'_, T> {
        IdxIter {
            iter: self.iter().enumerate(),
        }
    }
    fn iter_idx_mut(&mut self) -> IdxIterMut<'_, T> {
        IdxIterMut {
            iter: self.iter_mut().enumerate(),
        }
    }
}

pub fn gen<T, F: FnMut() -> T>(n: usize, mut f: F) -> impl Iterator<Item=T> {
    (0..n).map(move |_| f())
}

pub struct IO(
    std::io::BufReader<std::io::StdinLock<'static>>,
    std::io::BufWriter<std::io::StdoutLock<'static>>
);
 
impl IO {
    pub fn new() -> IO {
        IO(
            std::io::BufReader::new(Box::leak(Box::new(std::io::stdin())).lock()),
            std::io::BufWriter::new(Box::leak(Box::new(std::io::stdout())).lock())
        )
    }
    pub fn write<D: std::fmt::Display>(&mut self, v: D) {
        use std::io::Write;
        write!(self.1, "{}", v).unwrap();
    }
    pub fn writeln<D: std::fmt::Display>(&mut self, v: D) {
        use std::io::Write;
        writeln!(self.1, "{}", v).unwrap();
    }
    pub fn write_many<D: std::fmt::Display>(&mut self, vs: impl std::iter::IntoIterator<Item=D>) {
        for v in vs {
            self.writeln(v);
        }
    }
    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 read_ascii(&mut self) -> u8 {
        use std::io::Read;
        self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .next()
            .expect("Parse error")
    }
    pub fn read_raw_ascii(&mut self) -> u8 {
        use std::io::Read;
        self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .next()
            .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()
    }
    pub fn pairs<T: std::str::FromStr>(&mut self, n: usize) -> Vec<(T, T)> {
        (0..n).map(|_| (self.read(), self.read())).collect()
    }
}