//
//  main.c
//  A
//
//  Created by s1262004 on 2019/07/12.
//  Copyright © 2019 s1262004. All rights reserved.
//

#include <stdio.h>

#define H 500005
#define parent(x) x >> 1
#define left(x) x << 1
#define right(x) (x << 1) + 1

int n; //A.length
int node[H];
int heapSize;

void buildMaxHeap();
void maxHeapify(int);
void swap(int, int);

int main(int argc, const char * argv[]) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &node[i]);
    buildMaxHeap();
    for (int i = 1; i <= n; i++) printf(" %d", node[i]);
    printf("\n");
    return 0;
}

void buildMaxHeap(){
    heapSize = n;
    for (int i = (n/2); i >= 1; i--) maxHeapify(i);
}

void maxHeapify(int i){
    int l = left(i);
    int r = right(i);
    int maxIx;
    
    if (l <= heapSize && node[l] > node[i]) maxIx = l;
    else maxIx = i;
    if (r <= heapSize && node[r] > node[maxIx]) maxIx = r;
    
    if (maxIx != i) {
        swap(i, maxIx);
        maxHeapify(maxIx);
    }
}

void swap(int firstIx, int secondIx){
    node[firstIx] ^= node[secondIx];
    node[secondIx] ^= node[firstIx];
    node[firstIx] ^= node[secondIx];
}

