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(non_snake_case, unused)]

use std::cmp::*;
use std::collections::*;
use std::ops::*;

// https://atcoder.jp/contests/hokudai-hitachi2019-1/submissions/10518254

macro_rules! eprint {
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = write!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! eprintln {
	() => { eprintln!(""); };
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = writeln!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! dbg {
	($v:expr) => {{
		let val = $v;
		eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val);
		val
	}}
}

macro_rules! mat {
	($($e:expr),*) => { Vec::from(vec![$($e),*]) };
	($($e:expr,)*) => { Vec::from(vec![$($e),*]) };
	($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) };
	($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) };
}

macro_rules! ok {
	($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => {
		$a$([$i])*.$f($($t),*)
	};
	($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { {
		let t = $e;
		ok!($a$([$i])*.$f($($es),*)$(@$t)*@t)
	} };
}

pub fn readln() -> String {
	let mut line = String::new();
	::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
	line
}

macro_rules! read {
	($($t:tt),*; $n:expr) => {{
		let stdin = ::std::io::stdin();
		let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
			let line = line.unwrap();
			let mut it = line.split_whitespace();
			_read!(it; $($t),*)
		}).collect::<Vec<_>>();
		ret
	}};
	($($t:tt),*) => {{
		let line = readln();
		let mut it = line.split_whitespace();
		_read!(it; $($t),*)
	}};
}

macro_rules! _read {
	($it:ident; [char]) => {
		_read!($it; String).chars().collect::<Vec<_>>()
	};
	($it:ident; [u8]) => {
		Vec::from(_read!($it; String).into_bytes())
	};
	($it:ident; usize1) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
	};
	($it:ident; [usize1]) => {
		$it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
	};
	($it:ident; [$t:ty]) => {
		$it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
	};
	($it:ident; $t:ty) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
	};
	($it:ident; $($t:tt),+) => {
		($(_read!($it; $t)),*)
	};
}

pub fn main() {
	let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join();
}

const MOD: usize = 1_000_000_007;
const INF: i64 = std::i64::MAX/2;

use std::cmp;
use std::marker::PhantomData;

pub trait Monoid<T> {
    fn id() -> Option<T> {
        None
    }
    // TxT->T
    fn tt_op(l: &Option<T>, r: &Option<T>) -> Option<T>;
    // ExE->E
    fn ee_op(l: &Option<T>, r: &Option<T>) -> Option<T>;
    // TxE->E
    fn te_op(l: &Option<T>, r: &Option<T>) -> Option<T>;
}

pub struct SegOp<T: Ord> {
    phantom: PhantomData<T>,
}

impl<T: Ord + Clone + Add<Output=T>> Monoid<T> for SegOp<T> {
    #[inline]
    // add
    fn tt_op(l: &Option<T>, r: &Option<T>) -> Option<T> {
        match (l.clone(), r.clone()) {
            (Some(l), Some(r)) => Some(l+r),
            (Some(l), None) => Some(l),
            (None, Some(r)) => Some(r),
            (None, None) => None,
        }
    }
    // overwrite
    fn ee_op(l: &Option<T>, r: &Option<T>) -> Option<T> {
        match (l.clone(), r.clone()) {
            (Some(l), Some(r)) => Some(l+r),
            (Some(l), None) => Some(l),
            (None, Some(r)) => Some(r),
            (None, None) => None,
        }
    }
    // overwrite
    fn te_op(l: &Option<T>, r: &Option<T>) -> Option<T> {
        match (l.clone(), r.clone()) {
            (Some(l), Some(r)) => Some(l+r),
            (Some(l), None) => Some(l),
            (None, Some(r)) => Some(r),
            (None, None) => None,
        }
    }
}

pub struct SegmentTree<M: Monoid<T>, T: Clone> {
    phantom: PhantomData<M>,
    data: Vec<Option<T>>,
    lazy_data: Vec<Option<T>>,
    size: usize,
    size_p2: usize,
}

impl<M: Monoid<T>, T: Clone> SegmentTree<M, T> {
    pub fn from_vec(v: Vec<T>) -> SegmentTree<M, T> {
        let size = v.len();
        let mut size_p2 = 1;
        while size_p2 < v.len() {
            size_p2 *= 2;
        }
        let mut data = vec![None; size_p2 * 2];
        for (i, x) in v.into_iter().enumerate() {
            data[size_p2 + i] = Some(x);
        }
        for i in (0..size_p2).rev() {
            data[i] = M::tt_op(&data[i * 2 + 0], &data[i * 2 + 1]);
        }
        let mut lazy_data = vec![None; size_p2 * 2];
        SegmentTree {
            phantom: PhantomData,
            data: data,
            lazy_data: lazy_data,
            size: size,
            size_p2: size_p2,
        }
    }

    pub fn size(&self) -> usize {
        self.size
    }

    pub fn eval(&mut self, l: usize, r: usize, k: usize){
        if self.lazy_data[k].is_some() {
            self.data[k] = M::te_op(&self.data[k], &self.lazy_data[k]);
            if r-l>1 {
                self.lazy_data[2*k+0] = M::ee_op(&self.data[2*k+0], &self.lazy_data[k]);
                self.lazy_data[2*k+1] = M::ee_op(&self.data[2*k+1], &self.lazy_data[k]);
            }
            self.lazy_data[k] = None;
        }
    }

    pub fn lazy_update(&mut self, a: usize, b: usize, l: usize, r: usize, k: usize, value: T) {
        self.eval(l,r,k);
        if b<=l || r<=a {return;}
        if a<=l && r<=b {
            self.lazy_data[k] = M::ee_op(&self.lazy_data[k], &Some(value));
            self.eval(l,r,k);
        }
        else{
            self.lazy_update(a,b,l,(l+r)/2,2*k+0,value.clone());
            self.lazy_update(a,b,(l+r)/2,r,2*k+1,value.clone());
            self.data[k] = M::tt_op(&self.data[2*k+0], &self.data[2*k+1]);
        }
    }

    pub fn update(&mut self, a: usize, b:usize, value: T){
        self.lazy_update(a,b,0,self.size_p2,1,value);
    }

    pub fn lazy_query(&mut self, a: usize, b: usize, l: usize, r: usize, k: usize) -> Option<T> {
        self.eval(l,r,k);
        if a<=l && r<=b {return self.data[k].clone();}
        if b<=l || r<=a {return None;}
        let res1 = self.lazy_query(a, b, l, (l+r)/2, 2*k+0);
        let res2 = self.lazy_query(a, b, (l+r)/2, r, 2*k+1);
        M::tt_op(&res1, &res2)
    }

    pub fn query(&mut self, l: usize, r: usize) -> Option<T> {
        self.lazy_query(l,r,0,self.size_p2,1)
    }
}

fn solve() {
    let n = read!(usize);
    let s = read!(String;n);
    let mut total = 1000001;
    let mut v: Vec<Vec<i64>> = vec![vec![];total];
    for i in 0..n {
        let mut sum = 0;
        let mut mi = 0;
        for (j,c) in s[i].chars().enumerate() {
            if c=='(' {
                sum += 1;
            }
            else {
                sum -= 1;
            }
            mi = min(mi, sum);
        }
        v[(-mi) as usize].push(sum);
    }
    let mut idx = vec![0_i64;total+10];
    for i in 0..total {
        v[i].sort_by(|a, b| b.cmp(a));
        idx[i] = v[i].len() as i64;
    }
    let mut sum = 0_i64;
    pub type Seg<T> = SegmentTree<SegOp<T>, T>;
    let mut seg = Seg::from_vec(idx.clone());
    for i in 0..n {
        if sum < 0 {
            println!("No");
            return;
        }
        let target = sum as usize;
        let mut left = 0;
        let mut right = target+1;
        while right-left>1 {
            let mid = (left+right/2);
            let query = seg.query(mid,target+1).unwrap();
            if query>0 {
                left = mid;
            }
            else {
                right = mid;
            }
        }
        // println!("{} {}", i, left);
        if idx[left as usize] == 0 {
            println!("No");
            return;
        }
        seg.update(left,left+1,-1);
        // println!("{} {}", left, v[left as usize].len());
        sum += v[left as usize][idx[left as usize] as usize - 1];
        idx[left as usize] -= 1;
    }
    if sum!=0 {
        println!("No");
        return;
    }
    println!("Yes");
}

fn run() {
    solve();
}