#include<stdio.h>
#define MAX 500001

int A[MAX],H;

void MaxHeapify(int x){
  int l,r,largest;
  l = 2*x;
  r = 2*x+1;

  if(l<=H && A[l] > A[x]){
    largest = l;
  }
  else largest = x;
  if(r<=H && A[r] > A[largest]){
    largest = r;
  }
  if(largest!=x){
    int tmp = A[x];
    A[x]=A[largest];
    A[largest]= tmp;
    MaxHeapify(largest);
  }
  return ;
}

void BuildMaxHeap(){
  int i;
  for(i=H/2;i>=1;i--){
    MaxHeapify(i);
  }
  return;
}

int main(){
  int i;
  scanf("%d",&H);
  for(i=1;i<=H;i++){
    scanf("%d",&A[i]);
  }
  BuildMaxHeap();
  for(i=1;i<=H;i++)printf(" %d",A[i]);
  printf("\n");
  return 0;
}

