#include <stdio.h>
#include <math.h>
#define N 252

void buildMaxHeap(int* a_p, int h);
void maxh(int* a_p, int ix, int h);
int parent(int i);
int left(int i);
int right(int i);
void swap(int *a_p, int *b_p);

int main(){
  int n, i, j;
  int ix;
  int a[N];

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

  buildMaxHeap( a ,n );

  for( i = 1 ; i <= n; i++){
    printf(" %d" ,a[i]);
  }
  printf("\n");
    
  return 0; // Don't forget return 0
}

void buildMaxHeap(int* a_p, int h){
  int i;
  for( i=h/2 ; i>=1 ; i--){
    maxh( a_p ,i ,h );
  }
}

void maxh(int* a_p, int ix, int h){
  int largest;
  int l = left(ix);
  int r = right(ix);

  if( l<=h && a_p[l]>a_p[ix] ){
    largest = l;
  }else {
    largest = ix;
  }
  if( r <= h && a_p[r]>a_p[largest] ){
    largest = r;
  }

  if(largest!=ix){
    swap( &(a_p[ix]) ,&(a_p[largest]) );
    maxh( a_p ,largest ,h);
  }

}

int parent(int i){
  return floor(i/2);
}

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

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

void swap(int *a_p, int *b_p){
  int k;
  k    = *a_p;
  *a_p = *b_p;
  *b_p = k;
}