#include <stdio.h>
#define MAX 5000001
#define NIL -2000000001

int key[MAX],n;

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

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

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

void maxHeapify(int key[],int p){
  int l,r,largest=NIL,temp;
  l = left(p);
  r = right(p);
 
  if(l <= n && key[l]>key[p]) largest = l;
  else largest = p;
  if(r<= n && key[r]>key[largest]) largest = r;
  if(largest != p){
    temp = key[p];
    key[p] = key[largest];
    key[largest] = temp;
    maxHeapify(key,largest);
  }
  
}

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

int main(){
  int i,v;

  scanf("%d",&n);

  for ( i = 0; i < MAX; i++ ){
    key[i] = NIL;
  }

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


    buildMaxHeap(key);

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