#include<stdio.h>
#define H 500001


int A[H];
void maxHeapify(int i) {
    int l, r, largest, tmp;
    l = 2*i;
    r = 2*i + 1;
    if (l <= H && A[l] > A[i]) largest = l;
    else largest = i;
    
    if (r <= H && A[r] > A[largest]) largest = r;
    
    if (largest != i) {
	tmp = A[i]; A[i] = A[largest]; A[largest] = tmp;
	maxHeapify(largest);
    }
}

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

void print(int n) {
    int i;
    for (i = 1; i < n+1; i++) printf(" %d", A[i]);
    printf("\n");
}
	
int main() {
    int n, i;
    scanf("%d", &n);
    for (i =1; i < n+1; i++) scanf("%d", &A[i]);
    buildMaxHeap();

    print(n);
    
    return 0;
}