#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define rad M_PI/3
#define N 500001
#define Max 10000
#define NTL -1
int n;
int heap[Max];
void maxHeapify(int A[],int i){
  int l = i*2;
  int r = i*2+1;
  int largest;
  if(l<=n&&A[l]>A[i]){
    largest = l;
  }else{
    largest = i;
  }
  if(r<=n&&A[r]>A[largest]){
    largest = r;
  }
  if(largest != i){
    int tmp = A[i];
    A[i]=A[largest];
    A[largest]=tmp;
    maxHeapify(A,largest);
  }
}
void buildMaxHeap(int A[]){
  for(int i=n/2;i>0;--i){
    maxHeapify(A,i);
  }
}
int main(){
  int i;
  scanf("%d",&n);
  for(i=0;i<n;++i){
    scanf("%d", heap + 1 + i);
  }
  buildMaxHeap(heap);
  for(i=0;i<n;++i){
    printf(" %d", heap[i+1]);
  }
  printf("\n");
  return 0;
}
