# -*- coding: utf-8 -*-
def max_heapify(heap, i)
  left = 2 * i + 1
  right = 2 * i + 2
  largest = i
  heap_size = heap.size - 1

  if left <= heap_size && heap[i] < heap[left]
    largest = left
  else
    largest = i
  end

  if right <= heap_size && heap[largest] < heap[right]
    largest = right
  end

  unless largest == i
    heap[i], heap[largest] = heap[largest], heap[i]
    max_heapify(heap, largest)
  end
end

def build_max_heap(heap)
  ((heap.size - 1) / 2).downto(0) do |i|
    max_heapify(heap, i)
  end
end

n = gets.to_i
keys = gets.split.map(&:to_i)
build_max_heap(keys)
print " "
puts keys.join(" ")