#include <stdio.h>
#define SIZE 500000+1
int heep[SIZE];
int n;

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

int main(){
  int i; 
  scanf("%d",&n);
  for(i=1; i<=n; i++) scanf("%d",&heep[i]);
  
  buildMaxheap();
  
  for(i=1; i<=n; i++){
    printf(" %d",heep[i]);
  }
  printf("\n");
  
  return 0;
}

void swap(int *a,int *b){
  int tmp;
  tmp = *a;
  *a = *b;
  *b = tmp;
}

void maxHeapify(int i){
  int left,right,maximum;
  left = i*2;
  right = i*2+1;
  if(left <= n && heep[left] > heep[i])
    maximum = left;
  else maximum = i;
  
  if(right <= n && heep[right] > heep[maximum])
    maximum = right;
  
  if(maximum != i){
    swap(&heep[i],&heep[maximum]);
    maxHeapify(maximum);
  }
}

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

