#![allow( non_snake_case, unused_variables, unused_assignments, unused_mut, unused_imports, unused_macros, dead_code, static_mut_refs )] // use ac_library::*; // use ascii::AsciiChar::N; use itertools::Itertools; use proconio::{derive_readable, input, marker::*}; // use rustc_hash::*; use std::{cmp::*, collections::*, fmt::*, hash::*, marker::PhantomData, ops::*, println}; fn main() { input! { mut S: Chars, } let mut ans = Vec::new(); if S.len() == 1 && S[0] == '4' { println!("{}", '4'); return; } // 最初が4未満なら確定 if S[0].to_digit(10).unwrap() < 4 { println!("{}", "5".repeat(S.len() - 1)); return; } const INF: usize = 3_000_000_000_000_000_000; let mut mode = false; let mut lsfiv = INF; for i in 0..S.len() { if S[i].to_digit(10).unwrap() > 5 || mode { ans.push('5'); mode = true; lsfiv = i; } else if S[i].to_digit(10).unwrap() < 4 { if lsfiv != INF { ans[lsfiv] = '4'; // dbg!(&) for j in (lsfiv + 1)..i { ans[j] = '5'; } ans.push('5'); mode = true; } else { println!("{}", "5".repeat(S.len() - 1)); return; } } else if S[i] == '5' { ans.push('5'); lsfiv=i; } else { assert!(S[i]=='4'); ans.push('4'); } } for a in ans { print!("{}",a); } println!(); } trait Pipe: Sized { fn pipe(self, f: F) -> T where F: FnOnce(Self) -> T, { f(self) } } impl Pipe for T {} fn yesno(b: bool) { if b { println!("Yes"); } else { println!("No"); } }