$heap = []
def max_heapify(index)
  left = index * 2
  right = index * 2 + 1
  if left <= $size && $heap[left] > $heap[index]
    largest = left
  else
    largest = index
  end
  largest = right if right <= $size && $heap[right] > $heap[largest]

  unless largest == index
    $heap[index], $heap[largest] = $heap[largest], $heap[index]
    max_heapify largest
  end
end

def build_max_heap
  ($size).downto 1 do |index|
    max_heapify index
  end
end

$size = gets.to_i
$heap = gets.split(' ').map(&:to_i)
$heap.unshift nil
build_max_heap
$heap.shift
puts " " << $heap.join(' ')

