#include<stdio.h>
#include<stdlib.h>

void maxHeapfy(int *,int);
void buildMaxHeap(int *);

int h;

int main(){
  int *A;
  int i;

  scanf("%d",&h);
  A=(int*)malloc((h+1)*sizeof(int));
  for(i=1;i<=h;i++){
    scanf("%d",&A[i]);
  }

  buildMaxHeap(A);

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

  free(A);

  return 0;
}

void buildMaxHeap(int* A){
  for(int i=h/2;i>0;i--){
    maxHeapfy(A,i);
  }
}

void maxHeapfy(int* A,int i){
  int l=i*2;
  int r=(i*2)+1;
  int largest;
  int temp;

  if(l<=h && A[l]>A[i]){
    largest=l;
  }
  else{
    largest=i;
  }
  if(r<=h && A[r]>A[largest]){
    largest=r;
  }

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

