(*
ocamlfind ocamlopt -package batteries -linkpkg main.ml -o a.out
*)
open Batteries

let n = Scanf.sscanf (read_line ()) "%d" ( fun n -> n )
let lst = List.init n ( fun _ -> Scanf.sscanf (read_line ()) "%d" ( fun a -> a ))

let rec f lst mmax max =
        match lst with
                  [] -> []
                | first :: rest -> 
                        let rlst = f rest mmax max in
                                if first = max then
                                        mmax :: rlst
                                else
                                        max :: rlst

let ans =
        let max = List.max lst in
        let mmax = List.max (List.remove lst max) in
                List.iter (fun elmnt ->
                        Printf.printf "%d\n" elmnt
                ) (f lst mmax max)
