#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#define maxs 202020
#define mme(i,j) memset(i,j,sizeof(i))
#define ll long long
using namespace std;
int a[maxs];
void heapadjust(int now, int n) //堆调整
{
int l = now * 2, r = l + 1;
int m = a[l] > a[r] ? l : r;
if (now <= n / 2) //范围
{
if (a[now] <= a[l] || a[now] <= a[r])
{
int t = a[now];
a[now] = a[m];
a[m] = t;
}
heapadjust(m, n); //避免交换后m为节点的子树不是堆 进行递归处理
}
}
void heapbuild(int n) //建堆
{
for (int i = n / 2; i > 0; i--) heapadjust(i, n); //从最小的子树开始 做堆调整
}
int main()
{
int n;
while (cin >> n)
{
for (int i = 1; i <= n; i++)cin >> a[i];
heapbuild(n);
for (int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
}
}
#include <string>
#include <cstring>
#include <algorithm>
#define maxs 202020
#define mme(i,j) memset(i,j,sizeof(i))
#define ll long long
using namespace std;
int a[maxs];
void heapadjust(int now, int n) //堆调整
{
int l = now * 2, r = l + 1;
int m = a[l] > a[r] ? l : r;
if (now <= n / 2) //范围
{
if (a[now] <= a[l] || a[now] <= a[r])
{
int t = a[now];
a[now] = a[m];
a[m] = t;
}
heapadjust(m, n); //避免交换后m为节点的子树不是堆 进行递归处理
}
}
void heapbuild(int n) //建堆
{
for (int i = n / 2; i > 0; i--) heapadjust(i, n); //从最小的子树开始 做堆调整
}
int main()
{
int n;
while (cin >> n)
{
for (int i = 1; i <= n; i++)cin >> a[i];
heapbuild(n);
for (int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
}
}