use std::collections::BTreeSet; use std::io; use std::ops::Bound; #[derive(Debug, Clone)] struct Input { move_len: usize, moves: BTreeSet, dest: (i64, i64), mt_len: usize, } impl Input { fn search_dest(&self) -> isize { if self.dest == (0, 0) { 0 } else if self.moves.contains(&self.mt_len) { 1 } else if self.search_dest_with_2_moves() { 2 } else { -1 } } fn search_dest_with_2_moves(&self) -> bool { // 足して行けるパターン for i in self .moves .range((Bound::Unbounded, Bound::Excluded(self.mt_len))) { for j in self.moves.range(( Bound::Included(self.mt_len - i), Bound::Excluded(self.mt_len), )) { if (i + j) == self.mt_len { return true; } } } // 引いて行けるパターン for i in self .moves .range((Bound::Excluded(self.mt_len), Bound::Unbounded)) { for j in self .moves .range((Bound::Unbounded, Bound::Included(i - self.mt_len))) { if (i - j) == self.mt_len { return true; } } } false } } fn get_input() -> Input { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let move_len: usize = buf.clone().trim().parse().unwrap(); buf.clear(); io::stdin().read_line(&mut buf).unwrap(); let moves: BTreeSet = buf .clone() .trim() .splitn(move_len, ' ') .map(|s: &str| s.parse().unwrap()) .collect(); buf.clear(); io::stdin().read_line(&mut buf).unwrap(); let dest: Vec = buf .trim() .splitn(2, ' ') .map(|s: &str| s.parse().unwrap()) .collect(); Input { move_len, moves, dest: (dest[0], dest[1]), mt_len: (dest[0].abs() + dest[1].abs()) as usize, } } fn main() { let input = get_input(); println!("{}", input.search_dest()); }