#include<stdio.h>

void maxHeapify(int *,int);
void Swap(int *,int,int);

int N;
int T[500010] ={0};

int main()
{
    int i, j;
    scanf("%d", &N);
    for (i = 0; i < N; i++) {
        scanf("%d", &T[i]);
    }
    for (i = N/2+1; i >= 0; i--) {
        maxHeapify(T, i);
    }
    for (i = 0; i < N; i++) {
        printf(" %d", T[i]);
    }
    puts("");
  
    return 0;
}

void Swap(int T[], int x, int y)
{
    int tmp = T[x];
    T[x] = T[y];
    T[y] = tmp;
}

void maxHeapify(int T[], int i)
{
    int parent = i;
    int left = (i+1)*2 - 1;
    int right = (i+1)*2;
     
    int largest;
    if (right <= N && T[parent] < T[right]) {
        largest = right;
    }
    else {
        largest = parent;
    }
     
    if (left <= N && T[largest] < T[left]) {
        largest = left;
    }
  
    if (largest != parent) {
        Swap(T, largest, parent);
        maxHeapify(T, largest);
    }    
     
    return;
}
 