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

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

int n=0;

int main(){
  int i;
  int *a;
  scanf("%d",&n);
  a=malloc(sizeof(int)*(n+1));
  for(i=1; i<=n; i++){
    scanf("%d",&a[i]);
  }
  /*for(i=1; i<=n; i++) printf(" %d",a[i]);
    printf("\n");*/
  buildMaxHeap(a);
  for(i=1; i<=n; i++) printf(" %d",a[i]);
  printf("\n");
  return 0;
}

void maxHeapify(int *a,int i){
  int l,r,largest;
  l=i*2;
  r=i*2+1;
  if(l<=n && a[l]>a[i]) largest=l;
  else largest=i;
  if(r<=n && a[r]>a[largest]) largest=r;
  //printf("DEBUG: largest=%d,a[largest]=%d,i=%d,a[i]=%d\n",largest,a[largest],i,a[i]);
  if(largest!=i){
    l=a[i];a[i]=a[largest];a[largest]=l;
    maxHeapify(a,largest);
  }
}

void buildMaxHeap(int *a){
  int i;
  //printf("DEBUG: a[1]=%d,n=%d\n",a[1],n);
  for(i=n/2; i>=1; i--){
    maxHeapify(a,i);
  }
}

