#include <bits/stdc++.h>
using namespace std;
int heap[1000001], tmp, heap_size, son, pa, res, now;
void push(int d){
heap[heap_size++]=d;
while(son>1){
pa=son/2;
if(heap[son]>=heap[pa]) return;
else{
swap(heap[son], heap[pa]);
son=pa;
}
}
}
int pop(){
res=heap[1];
heap[1]=heap[heap_size--];
now=1;
while(now*2<=heap_size){
son=now*2;
if(son<heap_size&&heap[son]>heap[son+1]){
son++;
}
if(heap[now]<=heap[son]) return res;
swap(heap[now], heap[son]);
now=son;
}
return res;
}
int main(){
cin>>heap_size;
for(int i=1; i<=heap_size; i++){
cin>>tmp;
push(tmp);
}
for(int i=1; i<=heap_size; i++){
cout<<pop()<<" ";
}
cout<<pop();
return 0;
}
RE, A not allowed system call