#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define mymalloc(p,n) {p = malloc((n)*sizeof(*p));if ((p)==NULL) {printf("not enough memory?\n"); exit(1);};}

typedef struct{
	long *A;
	long n;
}heap;

int p(int i){return (i/2);}
int l(int i){return (2*i);}
int r(int i){return (2*i+1);}

void maxHeapify(heap *H,long i){
	long *A;
	long n,largest,temp;
	A=H->A;n=H->n;
	if(l(i)<=n && A[l(i)] > A[i]){
		largest = l(i);
	}else{
		largest = i;
	}
	if(r(i)<=n && A[r(i)] > A[largest]){
		largest = r(i);
	}
	if(largest!=i){
		temp=A[i];A[i]=A[largest];A[largest]=temp;
		maxHeapify(H,largest);
	}
}

void buildMaxHeap(heap *H){
	long i;
	for(i=H->n/2;i>=1;i--){
		maxHeapify(H,i);
	}
}

main(){
	long n,i,m;
	heap *H;
	scanf("%ld",&n);
	mymalloc(H,1);
	H->n=n;
	mymalloc(H->A,n+1);
	for(i=1;i<=n;i++){
		scanf(" %ld",&m);
		H->A[i]=m;
	}
	buildMaxHeap(H);
	for(i=1;i<=n;i++){
		printf(" %ld",H->A[i]);
	}
	printf("\n");
	return 0;
}