#include "iostream"
using namespace std;
int n,size,t;
//保持最大堆的性质
void Max_Heap(int s[1000],int a)
{
// cout<<a<<endl;
int record;
int l=2*a;//左孩子
int r=2*a+1; //右孩子
if(l<=n&&s[l]>s[a])
record=l;
else
record=a;
if(r<=n&&s[r]>s[record])
record=r;
if(record!=a)
{
t=s[a];
s[a]=s[record];
s[record]=t;
Max_Heap(s,record);
}
}
//建立最大堆
void Build_Max_Heap(int s[1000])
{
for(int i=n/2;i>=1;i--)
Max_Heap(s,i);
}
//堆排
void Heapsort(int s[1000])
{
for(int i=size;i>=1;i--)
{
cout<<s[1]<<endl;
t=s[1];
s[1]=s[i];
s[i]=t;
n--;
Max_Heap(s,1);
}
}
//堆实现最大优先队列
int Heap_Maximum(int s[1000])
{
int max;
if(n>=1)
{
max=s[1];
s[1]=s[size];
n--;
Max_Heap(s,1);
}
return max;
}
//将元素i的关键字的值增大到key(k>s[i])
void Heap_Increase_Key(int s[1000],int i,int key)
{
if(s[i]<key)
{
s[i]=key;
while(i>=1&&s[i]>s[i/2])
{
t=s[i];
s[i]=s[i/2];
s[i/2]=t;
i=i/2;
}
}
}
//在堆中插入关键字key
void Heap_Insert(int s[1000],int key)
{
size++;
n++;
s[n]=-1000;
Heap_Increase_Key(s,n,key);
}
int main()
{ int s[1000];
freopen("data.in","r",stdin);
cin>>n;
size=n;
for(int i=1;i<=n;i++)
cin>>s[i];
Build_Max_Heap(s);
Heapsort(s);
for(int i=1;i<=size;i++)
cout<<Heap_Maximum(s)<<endl;
Heap_Increase_Key(s,1,17);
Heap_Insert(s,19);
for(int j=1;j<=n;j++)
cout<<s[j]<<endl;
}