use std::collections::HashSet; use std::cmp::min; fn main() { let mut input_line = String::new(); std::io::stdin().read_line(&mut input_line).unwrap(); let mut iter = input_line.split_whitespace(); let l: i64 = iter.next().unwrap().parse().unwrap(); let n: i64 = iter.next().unwrap().parse().unwrap(); input_line.clear(); std::io::stdin().read_line(&mut input_line).unwrap(); let x: Vec = vec![0] .into_iter() .chain(input_line.split_whitespace().map(|s| s.parse().unwrap())) .chain(vec![l].into_iter()) .collect(); let mut dp = HashSet::new(); dp.insert((0, 0, 0)); for i in 1..=n+1 { let mut ndp = HashSet::new(); for &(ind, d, com) in dp.iter() { if com == 0 { ndp.insert((i, d + x[i as usize] - x[ind as usize], 1)); } else { ndp.insert((i, d - (x[i as usize] - x[ind as usize]), 0)); } } if i != n + 1 { dp = ndp.union(&dp).cloned().collect(); } else { dp = ndp; } } let mut ans = i64::MAX; for &(_, d, com) in dp.iter() { if com == 0 { ans = min(ans, d.abs()); } } println!("{}", ans); }