#include<stdio.h>
#include<stdlib.h>
int left(int i){
  return i*2;
}
int right(int i){
  return i*2+1;
}
void maxheapify(int A[],int i,int x){
  int l=left(i);
  int r=right(i);
  int key,largest;
  if(l<=x && A[l]>A[i])largest=l;
  else largest=i;
  if(r<=x && A[r]>A[largest])largest=r;
  if(largest!=i){
    key=A[i];
    A[i]=A[largest];
    A[largest]=key;
    maxheapify(A,largest,x);
  }
}
void buildmaxheap(int A[],int x){
  for(int i=x;1<=i;i--){
    maxheapify(A,i,x);
  }
}
int main(){
  int *h;
  int i,j,n;
  scanf("%d",&n);
  h=malloc(n *sizeof(int));
  for(i=1;i<=n;i++){
    scanf("%d",&h[i]);
  }
  buildmaxheap(h,n);
  for(i=1;i<=n;i++){
    printf(" %d",h[i]);
  }
  printf("\n");
  free(h);
  return 0;
}

