/* 
 * File:   main.c
 * Author: s1252007
 *
 * Created on July 27, 2018, 12:04 PM
 */

#include <stdio.h>
#include <stdlib.h>
#define MAX 5000001

/*
 * 
 */

int H, Heap[MAX];

void maxHeapify(int i);
void buildHeap();

int main(int argc, char** argv) {
    scanf("%d", &H);
    for (int i = 1; i <= H; ++i) scanf("%d", &Heap[i]);
    buildHeap();
    for (int i = 1; i <= H; ++i) printf(" %d", Heap[i]);
    printf("\n");
    return (EXIT_SUCCESS);
}

void maxHeapify(int i)
{
    int left, right, max, temp;
    left = i*2;
    right = i*2+1;
    
    if (left <= H && Heap[left] > Heap[i]) max = left;
    else max = i;
    if (right <= H && Heap[right] > Heap[max]) max = right;
    
    if (max != i)
    {
        temp = Heap[i];
        Heap[i] = Heap[max];
        Heap[max] = temp;
        maxHeapify(max);
    }
}

void buildHeap()
{
    for (int i = H/2; i >= 1; --i) maxHeapify(i);
}





