module Yuki open System let inline ret x = Some x let inline (>>=) (m: Option<'a>) f = match m with | Some x -> f x | None -> None type MaybeBuilder () = member __.Return(x) = ret x member __.Bind(m, f) = m >>= f let maybe = MaybeBuilder () let solve n = let num2list n = let rec f acc n = if n < 10 then n :: acc else f (n % 10 :: acc) (n / 10) f [] n let list2num xs = let rec f acc = function | [] -> acc | x :: xs -> f (acc * 10 + x) xs f 0 xs let swap idx1 idx2 xs = xs |> List.mapi (fun i v -> match i with | _ when i = idx1 -> xs.[idx2] | _ when i = idx2 -> xs.[idx1] | _ -> v) let digits = num2list n let indexedDigits = List.indexed digits let maxDigit = List.max digits maybe { let! swapLeftIdx = indexedDigits |> List.tryFind (fun (_, x) -> x <= maxDigit) |> function | Some (idx, _) -> Some idx | None -> None let swapRightIdx = indexedDigits |> List.findBack (fun (_, x) -> x = maxDigit) |> fun (idx, _) -> idx return digits |> swap swapLeftIdx swapRightIdx |> list2num } |> function | Some x -> x | None -> n let N = int <| Console.ReadLine() solve N |> Console.WriteLine