#include <stdio.h>
#include <stdlib.h>

int parent(int);
int left(int);
int right(int);
void maxHeapify(int *,int);
void buildMaxHeap(int *);

int N;

int main(){
  int i,p,l,r;

  scanf("%d",&N);
  int *ARRAY;
  ARRAY=(int *)malloc(sizeof(int)*N);

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

  buildMaxHeap(ARRAY);

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

  return 0;
}

void buildMaxHeap(int *x){
  int i;
  for(i = N/2;i>=1;i--){
    maxHeapify(x, i);
  }
}

void maxHeapify(int *x,int i){
  int largest,l,r,a;
  l = left(i);
  r = right(i);
  // ????????????????????????????????§???????????§????????????????????¶
  if(l <= N && x[l] > x[i]){
    largest = l;
  }
  else {
    largest = i;
  }
  if(r <= N && x[r] > x[largest]){
    largest = r;
  }

  if(largest != i){ // i ???????????????????????§????????´???
    //  A[i] ??¨ A[largest] ?????????
    a=x[i];
    x[i]=x[largest];
    x[largest]=a;
    maxHeapify(x, largest); // ?????°??????????????????
  }
}

int parent(int i){
  return i/2;
}

int left(int i){
  return 2*i;
}

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