def parent(i)
  i / 2
end

def left(i)
  2 * i
end

def right(i)
  2 * i + 1
end

def max_heapify(nodes, i)
  h = nodes.size - 1
  l = left(i)
  r = right(i)

  largest = i

  if l <= h && nodes[l] > nodes[i]
    largest = l
  end

  if r <= h && nodes[r] > nodes[largest]
    largest = r
  end

  if largest != i
    nodes[i], nodes[largest] = nodes[largest], nodes[i]
    max_heapify(nodes, largest)
  end
end

if $PROGRAM_NAME == __FILE__
  n = gets.to_i

  nodes = [nil] + gets.chomp.split.map(&:to_i)

  (n / 2).downto(1) do |i|
    max_heapify(nodes, i)
  end

  puts " #{nodes[1..-1].join(' ')}"
end