class Heap

	include Enumerable

	def self.build_max_heap(array)
		(array.size / 2).downto(1) { |i| max_heapify(array, i - 1) }
		Heap.new(array)
	end

	def each
		@heap.each { |e| yield e }
	end

	private
	def initialize(array)
		@heap = array
	end

	def self.max_heapify(heap, index)
		left  = left_child_index(index)
		right = right_child_index(index)

		max = [index, left, right].max_by { |i| heap[i] || Float::MIN }
		if max != index
			heap[index], heap[max] = heap[max], heap[index]
			max_heapify(heap, max)
		end
	end

	def self.left_child_index(index)
		index * 2 + 1
	end

	def self.right_child_index(index)
		index * 2 + 2
	end

end

n = STDIN.gets.to_i
array = STDIN.gets.split[0...n].map(&:to_i)
Heap.build_max_heap(array).each { |e| print " #{e}" }
puts