#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 105;
int a[N];
void Heapify(int a[], int i, int size) { /* 大根堆化 使以 i 为根的子树成为最大堆 */
int ls = 2*i, rs = 2*i+1;
int large;
if(ls <= size && a[ls] > a[i]) {
large = ls;
}
else large = i;
if(rs <= size && a[rs] > a[large]) {
large = rs;
}
if(large != i) {
swap(a[large], a[i]);
Heapify(a, large, size);
}
}
void Build_Heap(int a[], int size) { // 建立堆树
for(int i = size/2; i >= 1; i--) {
Heapify(a, i, size);
}
for(int i = 1; i <= size; i++){
printf("%d ", a[i]);
}
printf("\n");
}
void Heap_sort(int a[], int size) {
Build_Heap(a, size);
int len = size;
for(int i = len; i >= 2; i--) {
cout << "首尾未交换时:\n";
for(int j = 1; j <= size; j++){
printf("%d ", a[j]);
}
cout << endl;
swap(a[i], a[1]);
cout << "首尾一次交换结束:\n";
for(int j = 1; j <= size; j++){
printf("%d ", a[j]);
}
cout << endl;
len--;
Heapify(a, 1, len);
}
}
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
Heap_sort(a, n);
cout << endl;
return 0;
}