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

#define NIL -1

void maxHeapify( int );
void buildMaxHeap(void);
 
int m[500];
int n;

int main(){

  int i;

  scanf("%d",&n);

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

  buildMaxHeap();


  for( i = 1; i <= n; i++)
    printf(" %d",m[i]);
  printf("\n");

  return 0;
}

void maxHeapify( int i){

  int l, r;
  int largest;
  int tmp;
  l = i * 2;
  r = i * 2 + 1;

  if( l <= n && m[l] > m[i])
    largest = l;

  else
    largest = i;

  if(r <= n && m[r] > m[largest])
    largest = r;

  if(largest != i){
    tmp = m[i];
    m[i] = m[largest];
    m[largest] = tmp ;
    maxHeapify(largest);
  }
}

void buildMaxHeap(void){
  int j;
  for( j = n/2; j >= 1; j--)
    maxHeapify(j);
}