def maxHeapify(x)
  l = 2*x
  r = 2*x+1
  if l <= $n && $heap[l] > $heap[x]
    largest = l
  else
    largest = x
  end
  if r <= $n && $heap[r] > $heap[largest]
    largest = r
  end
  if largest != x
    $heap[x], $heap[largest] = $heap[largest], $heap[x]
    maxHeapify(largest)
  end
end
def buildMaxHeap
  ($n/2).downto(1){ |i| maxHeapify(i) }
end
$n = gets.to_i
$heap = gets.split.map(&:to_i)
$heap.unshit nil
buildMaxHeap
$heap.shift
$heap.each{ |i| print " ", i }
puts