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

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

void maxHeapify(int a[],int i) 
{
  int l,r,largest,tmp;
  l = left(i); 
  r = right(i);
  if(l<=n && a[l]>a[i])   largest = l;
  else largest = i;
  if(r<=n && a[r]>a[largest]) largest = r;
  
  if(largest != i){
    tmp = a[i];
    a[i] = a[largest];
    a[largest] = tmp;
    maxHeapify(a,largest);
  }
}

void buildMaxHeap(int a[]) 
{
  int i;
  for(i=n/2; i>=1; i--) maxHeapify(a,i);
}
  
int main() 
{
  int *a;
  int i;
  
  scanf("%d",&n);
  a = (int*)malloc(sizeof(int)*n+1);
  for(i=1; i<=n; i++) scanf("%d",&a[i]);
  buildMaxHeap(a);
  for(i=1; i<=n; i++) printf(" %d",a[i]);
  printf("\n");
  return 0;
}