def maxHeapify(heap_tree, i)
  heap_size = heap_tree.size
  #i_p = (i - 1) / 2
  i_l = 2 * i + 1
  i_r = 2 * i + 2
  largest = i

  if i_l <= heap_size - 1
    case heap_tree[i_l] <=> heap_tree[i]
    when 1
      largest = i_l
    end
  end

  if i_r <= heap_size - 1
    case heap_tree[i_r] <=> heap_tree[largest]
    when 1
      largest = i_r
    end
  end

  if largest != i
    heap_tree[i], heap_tree[largest] = heap_tree[largest], heap_tree[i]
    maxHeapify(heap_tree, largest)
  end
end

def buildMaxHeap(heap_tree)
  ((heap_tree.size - 1) / 2).downto(0) do |i|
    maxHeapify(heap_tree, i)
  end
end

n = gets.chomp.to_i
heap = gets.chomp.split(" ").map(&:to_i)
buildMaxHeap(heap)
print " "
puts heap.join(" ")