#include<stdio.h>
#define MAX 500000

int n;
void maxHeapify(int *,int);
void buildMaxHeap(int *);

int main(){

  int H[MAX];
  int i;
  
  scanf("%d",&n);
  
  for( i = 1 ; i <= n; i++ )
    scanf("%d",&H[i]);

  buildMaxHeap(H);

  for( i = 1 ; i <= n ; i++ )
    printf(" %d",H[i]);
  
  printf("\n");
  
  return 0;
}
 
void maxHeapify(int H[],int i) 
{
  int l,r;
  int largest;
  int tmp;
  
  l = i * 2;
  r = i * 2 + 1;
  
  if( l <= n && H[l] > H[i] ) 
    largest = l;
  else
    largest = i;
  if( r <= n && H[r] > H[largest] )
    largest = r;
   
  if( largest != i ) {
    tmp = H[i];

    H[i] = H[largest];
    H[largest] = tmp;
    maxHeapify(H,largest);
  }
}
 
void buildMaxHeap(int H[]) 
{
  int i;
  for(i = n/2; i >= 1; i--)
    maxHeapify(H,i);
}