#include<stdio.h>

#define MAX 500005

void maxHeapify( int*, int );
void swap( int* ,int* );

int main() {
  int i, H, A[MAX];

  scanf("%d",&H);

  for( i = 1 ; i <= H ; i++ ){

    scanf("%d",&A[i]);
  }

  for( i = H/2 ; i < 0 ; i-- ) {
    maxHeapify( A , i );
  }

  for( i = 1 ; i <= H ; i++ ){

    printf("%d ",A[i]);
  }
  printf("\n");

  return 0;

}


  void maxHeapify( int* A, int i ){
    int l , r , largest;
    l = 2 * i;
    r = 2 * i + 1;

    if( A[l] < A[r] && r <= 2*i ) largest = r; 
    if( A[l] > A[r] && l <= 2*i ) largest = l; 
    if( A[largest] < A[i] && r <= 2*i && l <= 2*i ) largest = i;

    if( largest != i ){
      swap( &A[i] ,&A[largest] );
      maxHeapify( A,largest );
    }
  }


void swap( int* x , int* y ) {
  int temp;

  temp = *x;
  *x = *y;
  *y = temp;
}