#include <stdio.h>
#include <math.h>
#include <string.h>
#define MAX 500005
#define NIL -1

typedef struct{ 
	int pk, lk, rk, key;
}node;

node T[MAX];
int i, h, id, temp;

int parent(int x){
	return floor(x/2);
}
	
int left(int x){
	return 2*x;
}

int right(int x){
	return 2*x + 1;
}

void maxHeapify(int x){
	int l, r, larger;
	l = left(x);
	r = right(x);
	if (l <= h && T[l].key > T[x].key)
		larger = l;
	else larger = x;
	if (r <= h && T[r].key > T[larger].key)
		larger = r; 
	if (larger != x){
		temp = T[x].key;
		T[x].key = T[larger].key;
		T[larger].key = temp;
		maxHeapify(larger);
	}
}

void buildMax(){
	for(i=floor(h/2); i>=1; i--)
		maxHeapify(i);
}


int main(void) {
	scanf("%d", &h);
	
	//initialize
	for (i=1; i<=h; i++){
		T[i].pk = T[i].lk = T[i].rk = T[i].key = NIL;
	}
	
	for(i=1; i<=h; i++){
		scanf("%d", &id);
		T[i].key = id;
	}
	
	buildMax();

	for(i=1; i<=h; i++)
		printf(" %d", T[i].key);
	printf("\n");
	
	return 0;
}




