n = gets.to_i
arr = gets.split.map(&:to_i).unshift("")

def max_heapify(arr, i)
    l = i * 2
    r = l + 1

    if (l < arr.size) && (arr[i] < arr[l])
        m = l
    else
        m = i
    end

    if (r < arr.size) && (arr[m] < arr[r])
        m = r
    end

    if m != i
        tmp = arr[i]; arr[i] = arr[m]; arr[m] = tmp
        max_heapify(arr, m)
    end

    arr
end

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

puts arr.join(" ")

