const INF: usize = 1usize << 60; const LIMIT: usize = 10000; fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut y = String::new(); std::io::stdin().read_line(&mut y).ok(); let y: Vec = y.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut dp = vec![0; LIMIT+1]; for i in 0..n { let mut next_dp = vec![INF; LIMIT+1]; let mut prevmin = INF; for j in 0..=LIMIT { prevmin = prevmin.min(dp[j]); let diff = j.max(y[i]) - j.min(y[i]); next_dp[j] = prevmin + diff; } dp = next_dp; } println!("{}", dp.iter().min().unwrap()); }