#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 500000

void buildmaxheap(int *);
void maxheapify(int *,int);
int left(int);
int right(int);
int n;
int main(){
  int i;
  int A[N];

  scanf("%d",&n);

  for(i=1;i<=n;i++){
    scanf("%d",&A[i]);
  }

buildmaxheap(A);

  for(i=1;i<=n;i++){
    printf(" %d",A[i]);
  }
printf("\n");

return 0;
}

void buildmaxheap(int *A){
  int i;
  for(i=n/2;i>=1;i--){
    maxheapify(A,i);
  }
}

void maxheapify(int *A,int i){
  int l,r,temp,largest;
  l=left(i);
  r=right(i);
  if((l<=n)&&(A[l]>A[i])){
    largest=l;
  }
  else{
    largest=i;
  }
  if((r<=n)&&(A[r]>A[largest])){
    largest=r;
  }

  if(largest!=i){
    temp=A[i];
    A[i]=A[largest];
    A[largest]=temp;
    maxheapify(A,largest);
  }
}

int left(int id){
  int lid;
  lid=2*id;
  /*if(lid<=n){*/
  return lid;/*}
  else{return -1;}*/
}

int right(int id){
  int rid;
  rid=2*id+1;
  /*if(rid<=n){*/
  return rid;/*}
  else{return -1;}*/
}

