#include<stdio.h>
#include<stdlib.h>
#define NIL -1
#define N 500001

int n, A[N];

void buildMaxHeap();
void maxHeapify(int);

void init(){
  int i;
  for(i = 0; i < N; i++){
    A[i] = NIL;
  }
}

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

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

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

void maxHeapify(int i){
  int l, r, largest, temp;
  l = left(i);
  r = right(i);

  if(l <= n && A[l] > A[i]) {
    largest = l;
    // printf("!%d!\n",largest);
  }  
else {
    largest = i;
    //printf("[%d]\n",largest);
  }
  if(r <= n && A[r] > A[largest]){
    largest = r;
    //printf("*%d*\n",largest);
  }

  if(largest != i){
    //printf("[%d],[%d]\n",A[i],A[largest]);
    temp = A[i];
    A[i] = A[largest];
    A[largest] = temp;
    maxHeapify(largest);
  }

}

void buildMaxHeap(){
  int i;
  for(i = n/2; i > 0; i--){
    //printf("%d\n", i);
    maxHeapify(i);
  }
}



int main(){

  int i, p, l, r;

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

  buildMaxHeap();

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

  printf("\n");

  return 0;

}