def max_heapify nodes, i
  n = nodes.length

  p_i = i
  lc_i = (i + 1) * 2 - 1
  rc_i = (i + 1) * 2
  p_k = nodes[i]
  lc_k = lc_i >= 0 && lc_i <= n - 1 ? nodes[lc_i] : -Float::INFINITY
  rc_k = rc_i >= 0 && rc_i <= n - 1 ? nodes[rc_i] : -Float::INFINITY

  largest_i = p_i
  largest_k = p_k
  if largest_k < lc_k
    largest_i = lc_i
    largest_k = lc_k
  end
  if largest_k < rc_k
    largest_i = rc_i
    largest_k = rc_k
  end

  if largest_i != p_i
    nodes[largest_i], nodes[p_i] = nodes[p_i], nodes[largest_i]
    max_heapify nodes, largest_i
  end
end

n = gets.chomp.to_i
nodes = gets.chomp.split(' ').map(&:to_i)
(n - 1).downto(0) do |i|
  max_heapify nodes, i
end
puts ' ' + nodes.join(' ')
