use itertools::Itertools; use proconio::{input, marker::Bytes}; fn main() { input! { mut n: Bytes } n.iter_mut().for_each(|n| *n -= b'0'); let mut ans = vec![]; let mut less = false; for &n in &n { if less { ans.push(5); } else { if n > 5 { ans.push(5); less = true; } else if n < 4 { if let Some(i) = (0..ans.len()).rfind(|&i| ans[i] == 5) { ans[i] -= 1; ans.push(5); } else { ans.truncate(ans.len().saturating_sub(1)); ans.fill(5); if !ans.is_empty() { ans.push(5); } } less = true; } else { ans.push(n); } } } println!("{}", ans.iter().join("")); }