import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
import std.typecons;
import std.math;
import std.random;
import std.range;
import std.functional;
import std.container;
void maxHeapify(int[] k, int i)
{
    auto l = 2 * i;
    auto r = 2 * i + 1;
    int largest;
    if (l < k.length && k[l] > k[i])
        largest = l;
    else
        largest = i;
    if (r < k.length && k[r] > k[largest])
        largest = r;
    if (largest != i)
    {
        k[i].swap(k[largest]);
        maxHeapify(k, largest);
    }
}
void main()
{
    auto c = readln.strip.to!int;
    auto k = [0] ~ readln.strip.split(' ').map!(to!int).array;
    for (auto i = c / 2; i >= 1; i--)
        maxHeapify(k, i);
    foreach (i; k.drop(1))
        write(" ", i);
    writeln;
}
