fn main() { let vec: Vec = read_vec(); let index = vec[1]; let text: String = read(); let mut chars: Vec = text.chars().collect(); let mut ans = hoge(index, chars.clone()); if ans == 0 { chars.reverse(); ans = vec[0] - hoge(index, chars); } println!("{}", ans); } fn hoge(index: usize, chars: Vec) -> usize { let mut counter = 0; for i in 0..index { match chars[i] { '(' => { counter += 1; }, ')' => { counter -= 1; }, _ => {}, } } let registor = counter; for i in index..chars.len() { match chars[i] { '(' => { counter += 1; }, ')' => { counter -= 1; }, _ => {}, } if counter == registor - 1 { return i + 1; } } return 0; } fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() }