const INF: usize = 1usize << 60; const LIMIT: usize = 1000; 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![vec![INF; LIMIT+1]; n]; for j in 0..=LIMIT { dp[0][j] = y[0].max(j) - y[0].min(j); } for i in 1..n { let mut prevmin = INF; for j in 0..=LIMIT { prevmin = prevmin.min(dp[i-1][j]); let diff = y[i].max(j) - y[i].min(j); dp[i][j] = prevmin + diff; } } println!("{}", dp[n-1].iter().min().unwrap()); }