let print_array l =
  Array.fold_left (fun x y -> x^" "^(string_of_int y)) "" l |> print_endline;;

let rec max_heapify h a j =
  let l = j*2 and
      r = j*2+1 and
      largest = ref j
  in
  if l <= h then
    begin
      if a.(l-1) > a.(j-1) then largest := l;
      if r <= h && a.(r-1) > a.(!largest-1) then largest := r;
      if !largest != j then
        begin
          let temp = a.(!largest-1) in
          a.(!largest-1) <- a.(j-1);
          a.(j-1) <- temp;
          max_heapify h a !largest
        end
    end
;;

let () =
  let h = read_int () and
      a = read_line () |> Str.split (Str.regexp " ")
          |> List.map int_of_string |> Array.of_list
  in
  for i = 1 to h/2 do
    let j = h/2 - i+1 in
    max_heapify h a j;
  done;
  print_array a;;