def parent(i)
  return i/2
end
def left(i)
  return i*2
end
def right(i)
  return i*2+1
end
def maxHeap(arr,i)
  l=left(i)
  r=right(i)
  if l<=@n && arr[l]>arr[i]
    largest=l
  else
    largest=i
  end
  if r<=@n && arr[r]>=arr[largest]
    largest=r
  end
  if largest!=i
    arr[i],arr[largest]=arr[largest],arr[i]
    maxHeap(arr,largest)
  end
end
def buildMaxHeap(arr)
  @n.downto(1) do |i|
    maxHeap(arr,i)
  end
end
arr=Array.new()
@n=gets.to_i
arr=gets.split.map(&:to_i)
arr.unshift(nil)
buildMaxHeap(arr)
puts arr.join(' ')