H = gets.chomp.to_i
hKeys = gets.chomp.split(' ').map(&:to_i)
hKeys.unshift(nil)

def maxHeapify(array, i)
    l = i * 2
    r = i * 2 + 1
    if l <= H && array[l] > array[i] then
        largest = l
    else
        largest = i
    end
    if r <= H && array[r] > array[largest] then
        largest = r
    end
    
    if largest != i then
        tmp = array[i]
        array[i] = array[largest]
        array[largest] = tmp
        maxHeapify(array, largest)
    end
end

(H / 2).downto(1) do |i|
    maxHeapify(hKeys, i)
end

puts hKeys.join(' ')
