#include <stdio.h>

void maxHeapify(int *, int);
void buildMaxheap(int*);

int n, a[500000];

int main(){

  int i;

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

  buildMaxheap(a);

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

void buildMaxheap(int *a){
  int i;

  for(i=n/2; i>=1; i--){
    maxHeapify(a, i);
  }
}
void maxHeapify(int *a, int i){ //error

  int left =(i*2);
  int right=(i*2+1);
  int largest, temp;
  
  //左の子、自分、右の子でMAXを選ぶ

  if(left<=n && a[i]< a[left]){ //左の子存在、左の子より小さい
    largest=left; //あくまで節点の場所
  }else largest=i;

  if(right<=n && a[largest]<a[right]){

    largest=right;
  }

  if(largest != i){

    temp=a[largest];

    a[largest]=a[i];
    a[i]=temp;

    maxHeapify(a, largest);
  }
}

