#include <stdio.h>
#define N 500000

int H[N],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,v,largest;
  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){
    v =H[i];
    H[i] = H[largest];
    H[largest]=v;
    maxHeapify(H,largest);
  } 
}

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

int main(){
  int i;

  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;
}