#include<stdio.h>
#define MAX 2000000
int A[MAX+1],n;
int left(int i){ return 2*i;}
int right(int i){ return 2*i+1;}
int parent(int i){ return i/2;}

/*void print(int u){
  printf("node %d: key = %d, ",u,A[u]);
  if(u>1) printf("parent key = %d, ",A[parent(u)]);
  if(left(u)<=n) printf("left key = %d, ",A[left(u)]);
  if(right(u)<=n) printf("right key = %d, ",A[right(u)]);
  printf("\n");
}*/

void maxHeapify(int i){
  int l,r,largest,tmp;
  l=left(i);
  r=right(i);

  if(A[i]<A[l]&&l<=n) largest=l;
  else largest=i;
  if(A[largest]<A[r]&&r<=n) largest=r;

  if(largest!=i){
    tmp=A[i]; A[i]=A[largest]; A[largest]=tmp;
    maxHeapify(largest);
  }
}
void buildMaxHeap(void){
  int i;
  for(i=n/2;i>0;i--) maxHeapify(i);
}
int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&A[i]);
  }
  buildMaxHeap();
  for(i=1;i<=n;i++){
    printf(" %d",A[i]);
  }
  printf("\n");

  return 0;
}


