#include<stdio.h>
#define MAX 500001

int n;

int left(int);
int right(int);

void maxHeapify(int H[],int i){
  int l,r,largest,temp;
  l=left(i);
  r=right(i);
  if(l<=n&&H[l]>H[i])
    largest=l;
  else
    largest=i;

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

  if(largest!=i){
    temp=H[i];
    H[i]=H[largest];
    H[largest]=temp;
    maxHeapify(H,largest);
  }
}

void buildMaxHeap(int H[]){
  int i;
  for(i=n/2;i>=1;i--){
    maxHeapify(H,i);
  }
}


int left(int i){
  i*=2;
  if(i>n)
    return MAX;
  else
    return i;
}

int right(int i){
  i=2*i+1;
  if(i>n)
    return MAX;
  else
    return i;
}

int main(){
  int i,H[MAX];
  scanf("%d",&n);
  for(i=1;i<n+1;i++){
    scanf("%d",&H[i]);
  }
  buildMaxHeap(H);

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

  return 0;
}