#include <stdio.h>
#define MAX 500001

int n;
int heap[MAX];
void getmax(int i){
  int l=2*i;
  int r=2*i+1;
  int m,p;

  if(l<=n && heap[i]< heap[l]){
    m=l;
    }
    else {
      m=i;
    }
    if(heap[r] > heap[m] && r<=n){
      m=r;
    }
    if(m!=i){
      p=heap[i];
      heap[i]=heap[m];
      heap[m]=p;
      getmax(m);
    }
}
void build(void){
  int i;
  for(i=(int)(n/2);i>=1;i--){
  getmax(i);
  }
}
int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&heap[i]);
  }
  //printf("agasdfga\n");
/*  for(i=(int)(n/2);i>=1;i--){
    getmax(i);
  }
*/
  build();
  for(i = 1;i <= n;i++){
    printf(" %d",heap[i]);
  }
  printf("\n");

  return 0;

}