def maxHeapify(i)
	l = 2 * i
	r = 2 * i + 1
	
	if l <= H && $a[l] > $a[i]
		largest = l
	else
		largest = i
	end
	largest = r if r <= H && $a[r] > $a[largest]

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

H = gets.to_i
$a = gets.split.map &:to_i
$a.unshift(nil)

(H/2).downto(1) do |i|
	maxHeapify(i)
end

$a.shift
print " ", $a.join(" ")
puts