include<stdio.h>
#define N 100000
int n;

int Left(int i){
  return 2*i;
}

int Right(int i){
  return 2*i+1;
}

void maxHeapify(int T[],int i){
  int l,r,largest,key;

  l=Left(i);
  r=Right(i);

  if(l<=n&&T[l]>T[i]) largest=l;
  else largest=i;

  if(r<=n&&T[r]>T[largest]) largest=r;

  if(largest!=i){
    key=T[i];
    T[i]=T[largest];
    T[largest]=key;
    maxHeapify(T,largest);
  }

}

void buildMaxHeap(int T[]){
  int i;

  for(i=n/2;i>=1;i--){
    maxHeapify(T,i);
  }
  
}

int main(void){
  int i,T[N];

  scanf("%d",&n);

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

  buildMaxHeap(T);

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

  printf("\n");
  return 0;

}