def maxHeapify i
  l = i*2
  r = l+1
  max = l<=$H && $a[l-1] > $a[i-1] ? l : i
  max = r if r <= $H && $a[r-1] > $a[max-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}
puts $a.join " "