#include<stdio.h>
#include<stdlib.h>
#define H 500000

void buildMaxHeap(int *);
int left(int);
int right(int);
void maxHeapify(int *,int);

int n;
int Heap_size;

int main()
{
  int i;
  int *A;

  scanf("%d",&n);
  A = (int *)malloc(n * sizeof(int));
  for(i = 1;i <= n;i++){
    scanf("%d",&A[i]);
  }

  buildMaxHeap(A);

  for(i = 1;i <= n;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
  free(A);
  return 0;
}

void buildMaxHeap(int *A)
{
  int i;
  Heap_size = n;
  for(i = Heap_size/2;i >= 1;i--){
    maxHeapify(A,i);
  }
}
void maxHeapify(int *A,int i)
{
  int l,r,largest,tmp;
  l = left(i);
  r = right(i);
  if(l <= Heap_size && A[l] > A[i]) largest = l;
  else largest = i;
  if(r <= Heap_size && A[r] > A[largest]) largest = r;
  if(largest != i){
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(A,largest);
  }
}

int left(int i)
{
  return 2*i;
}

int right(int i)
{
  return 2*i+1;
}