use std::io::*; use std::str::FromStr; use utils::*; pub fn main() { let i = stdin(); let mut o = Vec::new(); run(i.lock(), &mut o); stdout().write_all(&o).unwrap(); } fn run(i: R, o: &mut W) { let mut i = CpReader::new(i); let n = i.read::(); writeln!(o, "{}", solve(n)).unwrap(); } fn solve(mut n: usize) -> usize { let mut ns = Vec::new(); let mut maxs = Vec::new(); let mut max_val = 0; let mut max_i = 0; while n != 0 { let val = n % 10; if val > max_val { max_val = val; max_i = ns.len(); } maxs.push((max_val, max_i)); ns.push(val); n /= 10; } for i in (1..ns.len()).rev() { if maxs[i - 1].0 > ns[i] { ns.swap(i, maxs[i - 1].1); break; } } let mut b = 1; let mut result = 0; for &n in &ns { result += n * b; b *= 10; } result } mod utils { use super::*; pub struct CpReader { r: R, s: String, } impl CpReader { pub fn new(r: R) -> Self { CpReader { r: r, s: String::new(), } } pub fn read_line(&mut self) -> &str { self.s.clear(); self.r.read_line(&mut self.s).unwrap(); self.s.trim() } pub fn read(&mut self) -> T { self.read_line().parse().ok().unwrap() } } }