#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::Read; #[allow(dead_code)] fn getline() -> String { let mut ret = String::new(); std::io::stdin().read_line(&mut ret).ok().unwrap(); ret } fn get_word() -> String { let stdin = std::io::stdin(); let mut stdin=stdin.lock(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = String::from_utf8(buf).unwrap(); return ret; } } } #[allow(dead_code)] fn get() -> T { get_word().parse().ok().unwrap() } const INF: i32 = 1 << 28; fn f(c: i32) -> i32 { if c < 0 { INF } else { c / 10 + (c % 10) / 5 + c % 5 } } // Tags: digital-dp fn main() { let s: Vec<_> = get_word().bytes().collect(); let n = s.len(); let mut dp = vec![[INF; 3]; n + 1]; dp[n][0] = 0; for i in (0..n).rev() { let c = (s[i] - b'0') as i32; for b in 0..2 { let c = c + b as i32; for j in 0..11 { dp[i][0] = min(dp[i][0], dp[i + 1][b] + f(j - c) + f(j)); dp[i][1] = min(dp[i][1], dp[i + 1][b] + f(j) + f(j - c + 10)); } } } println!("{}", dp[0][0]); }