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.chomp.to_i
@heap = gets.chomp.split.map(&:to_i)
@heap.unshift nil
buildMaxHeap
@heap.shift
@heap.each{ |i| print " ", i}
puts