def maxHeapify i
  l = i*2
  r = l+1
  l = i unless $a[l-1]
  r = i unless $a[r-1]
  max = [i,l,r].max_by {|i| $a[i-1]}
  unless max == i
    $a[i-1],$a[max-1] = $a[max-1],$a[i-1]
    maxHeapify max
  end
end
$H=gets.to_i
$a=gets.split.map &:to_i
($H/2).downto(1) {|i| maxHeapify i}
print ' ',$a.join(' ')
puts