#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct MaiximumHeapData *MaiximumHeap;

#define parent(x) ((x-1)/2)
#define left(x)   (2*(x)+1)
#define right(x)  (2*(x)+2)

struct MaiximumHeapData
{
    int *array;
    int size;
    int last;
};

MaiximumHeap MaiximumHeapnew(int size);
void MaiximumHeapinsert(MaiximumHeap heap, int item);
void MaiximumHeapHeapify(MaiximumHeap heap);
void MaiximumHeapPrint(MaiximumHeap heap);
void MaiximumHeapFree(MaiximumHeap heap);

MaiximumHeap
MaiximumHeapnew(int size)
{
    MaiximumHeap newHeap = (MaiximumHeap)malloc(sizeof(struct MaiximumHeapData));
    newHeap->array = (int*)malloc(sizeof(int) * size);
    newHeap->size = size;
    newHeap->last = -1;
    return newHeap;
}

void
MaiximumHeapinsert(MaiximumHeap heap, int item)
{
    if (heap->last == heap->size - 1)
    {
	int *newArray = malloc(sizeof(int) * heap->size * 2);
	int i;
	for (i=0; i<heap->size; i++)
	    newArray[i] = heap->array[i];
	free(heap->array);
	newArray[++heap->last] = item;
	heap->array = newArray;
	heap->size *= 2;
    }
    else
	heap->array[++heap->last] = item;
    MaiximumHeapHeapify(heap);
}

void
swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void
MaiximumHeapHeapify(MaiximumHeap heap)
{
    int target = heap->last;
    while (1)
    {
	int l = heap->array[left(target)], r = heap->array[right(target)],
	    largest = heap->array[target];
	if (right(target) <= heap->last && r > l && r > largest)
	    swap(&heap->array[target], &heap->array[right(target)]);
	else if (left(target) <= heap->last && l > largest)
	    swap(&heap->array[target], &heap->array[left(target)]);

	/* Ternimate the loop when reach the root. */
	if (target == parent(target))
	    break;
	else
	    target = parent(target);
    }
}

void
MaiximumHeapPrint(MaiximumHeap heap)
{
    int i;
    for (i=0; i <= heap->last; i++)
	printf("%d ", heap->array[i]);
    puts("");
}

void
MaiximumHeapFree(MaiximumHeap heap)
{
    free(heap->array);
    free(heap);
}

int
main(void)
{
    int i, heapSize, key;
    MaiximumHeap heap = MaiximumHeapnew(100);
    scanf("%d", &heapSize);
    for (i=0; i<heapSize; i++)
    {
	scanf("%d", &key);
	MaiximumHeapinsert(heap, key);
    }
    MaiximumHeapPrint(heap);
    MaiximumHeapFree(heap);
    return 0;
}