#include <stdio.h>
#define MAX 500000
 
int T[MAX]; 
int n;

void maxHeapify(int);
void buildMaxHeap();
int parent(int);
int left(int);
int right(int);
 
int main()
{
  int i;
  
  scanf("%d", &n); 
  for(i=1; i<=n; i++){
    scanf("%d", &T[i]);
  }
  
  buildMaxHeap();
 
  for(i=1; i<=n; i++){   
    printf(" %d", T[i]);
  }
 
  printf("\n");
 
  return 0;
}


void buildMaxHeap()
{
  int i;

  for(i=(n/2); i>=1; i--){
    maxHeapify(i);
  }
}


void maxHeapify(int i)
{
  int largest, exchange, getl, getr;

  getl = left(i);
  getr = right(i);
  
  if((n >= getl) && (T[getl] > T[i])){
	largest = getl;
  }
  else{
	largest = i;
  }
   
  if((n >= getr) && (T[getr] > T[largest])){
	largest = getr;
  }

  if (largest != i){
    exchange = T[i];
    T[i] = T[largest];
    T[largest] = exchange;
    maxHeapify(largest);
  } 

}


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