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