class Heap
	@@LOWER_LIMIT = -2_000_000_001
	@@UPPER_LIMIT =  2_000_000_001

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

	def to_s
		" #{@heap.join(" ")}"
	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] || @@LOWER_LIMIT }
		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)
puts Heap.build_max_heap(array)