#include<stdio.h>
#define MAX 2000000
int n,d[MAX+1];
void change(int I,int L){
    int temp;
    temp=d[I];
    d[I]=d[L];
    d[L]=temp;
}
void maxHeapify(int j){
  int l,r,largest;
  l = 2 * j;
  r = 2 * j +1;
  largest = (l<=n && d[l] > d[j]) ? l: j;
  if(r<=n && d[r] > d[largest]) largest = r;
  if(largest != j){
    change(j,largest);
    maxHeapify(largest);
  }
}
int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++) scanf("%d",&d[i]);
  for(i=n/2;i>=1;i--) maxHeapify(i);
  for(i=1;i<=n;i++) printf(" %d",d[i]);
  printf("\n");
  return 0;
}

