let print_array l =
  Array.map string_of_int l |> Array.to_list
  |> String.concat " "
  |> print_endline;;

let () =
  let h = read_int () and
      a = read_line () |> Str.split (Str.regexp " ")
          |> List.map int_of_string |> Array.of_list
  in
  let rec build_max_heap = function
      0 -> ()
    | i ->
       let rec max_heapify j =
         let l = j*2 and
             r = j*2+1
         in
         let maybe_largest = if l <= h && a.(l-1) > a.(j-1) then l else j in
         let largest = if r <= h && a.(r-1) > a.(maybe_largest-1) then r else maybe_largest in
         if largest != j then
           begin
             let temp = a.(largest-1) in
             a.(largest-1) <- a.(j-1);
             a.(j-1) <- temp;
             max_heapify largest
           end
       in
       max_heapify i;
       build_max_heap (i-1)
  in
  build_max_heap (h/2);
  print_array a;;