#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n=0, v[500005], m, p, x;
void push(int x)
{
v[++n]=x; //cout<<v[n];
int p=n;
while(v[p]<v[p/2]&&p>1)
swap(v[p], v[p/2]), p/=2;
}
void pop()
{
g<<v[1]<<' ';
cout<<v[1]<<' ';
swap(v[1], v[n--]);
p=1;
int s;
while(2*p<=n)
{
s=2*p;
if(2*p+1>n)
{
if(v[p]>v[s]) swap(v[p], v[s]);
break;
}
else{
if(v[s]<=v[s+1]&&v[p]>v[s]) swap(v[p], v[s]);
else if(v[p]>v[s+1]) swap(v[p], v[s+1]), s++;
}
p=s;
}
//for(int i=1; i<=n; ++i) cout<<v[i]<<' '; cout<<'\n';
}
int main()
{
f>>m;
for(int i=1; i<=m; ++i)
{
f>>x;
push(x);
}
for(int i=1; i<=m; ++i)
pop();
return 0;
}