#include<stdio.h>
#define MAX 500000

int n;

int Left(int i){
  return 2*i;
};
int Right(int i){
  return 2*i+1;
};

void MaxHeapify(int H[],int i){
  int l, r, largest;
  int tmp;
  
  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){
    tmp = H[i];
    H[i] = H[largest];
    H[largest] = tmp;

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

int main(){
  int i;
  int H[MAX];

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

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