#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<ctype.h>
#include<stack>
#include<math.h>
#include <string>
#include<algorithm>
#include<iomanip>
#define _for(i,a,b) for(int i = (a);i < (b);i++)
#define mset(x) memset(x,0,sizeof(x));
using namespace std;
int h[101];
int n;//the namber of stack elements or the big of the stack
void swap(int a,int b) {
int temp = h[a];
h[a] = h[b];
h[b] = temp;
return;
}
void siftdown(int i) {
int t,flag = 0;
while(2 * i <= n && flag == 0) {
if(h[i] > h[2 * i])
t = 2 * i;
else t = i;
if(i * 2 + 1 <= n) { //判断一下右儿子是否比左儿子小,看更新哪一个
if(h[t] > h[i * 2 + 1])
t = i * 2 + 1;
}
if(t != i) {
swap(t,i);
i = t;//继续判断以后要不要优化
} else
flag = 1;
}
return;
}
void creat() {
for(int i = n / 2; i >= 1; i-- ) {
siftdown(i);
}
return;
}
int deletemax() {
int t;
t = h[1];
h[1] = h[n];
n--;
siftdown(1);
return t;
}
int main() {
int num;
scanf("%d",&num);
for(int i = 1; i <= num; i++) {
scanf("%d",&h[i]);
}
n = num;
// swap(h[1],h[2]);
// swap(h[2],h[3]);
creat();
for(int i = 1; i <= num; i++)
printf("%d ",deletemax());
return 0;
}