/*
 * FileName:     max_heap_fix
 * CreatedDate:  2020-08-19 23:12:45 +0900
 * LastModified: 2020-08-19 23:23:26 +0900
 */

#include <stdio.h>
#include <stdlib.h>


void maxheapify(int *a, int index, int H){
    int key = index;
    if(2*index<=H && a[2*index]>a[index]){
        key = 2*index;
    }
    if(2*index+1<=H && a[2*index+1]>a[key]){
        key = 2*index+1;
    }
    if(key != index){
        int tmp = a[key];
        a[key] = a[index];
        a[index] = tmp;
        maxheapify(a, key, H);
    }
}


void build_heap(int *a, int H){
    for(int i=H/2; i>0; i--){
        maxheapify(a, i, H);
    }
}


int main(void){
    int H;scanf("%d", &H);
    int *a = malloc((H+1)*sizeof(int));
    for(int i=1; i<H+1; i++) scanf("%d", a+i);
    build_heap(a, H);
    for(int i=1; i<H+1; i++){
        printf(" %d", a[i]);
    }
    printf("\n");
    free(a);
    return 0;
}

