#include<stdio.h>
#define MAX 100000
int H;
int right(int i){
  return 2*i + 1;
}
int left(int i){
  return 2 * i;
}
void swap(int *a,int *b){
  int tmp;
  tmp = *a;
  *a = *b;
  *b = tmp;
}
void MaxHeapify(int i,int *A){
  int l,r,largest;
  l = left(i);
  r = right(i);
  if(l <= H&&A[l] > A[i]){
    largest = l;
  }else{
    largest = i;
  }
  if(r <= H&&A[r] > A[largest]){
    largest = r;
  }
  if(largest != i){
    swap(&A[i],&A[largest]);
    MaxHeapify(largest,A);
  }
}
int main(){
  int i,A[MAX + 1];
  scanf("%d",&H);
  for(i = 1;i <= H;i++) scanf("%d",&A[i]);
  for(i = H/2;i > 0;i--) MaxHeapify(i,A);
  for(i =1;i <= H;i++) printf(" %d",A[i]);
  printf("\n");
  return 0;
}

