#include <stdio.h>

#define MAX 500000

void Maximum_Heap(int*,int);
void buildMaxHeap(int*);

int heap_size;

int main(){
  int n,i,a[MAX];

  scanf("%d" ,&n);
  for(i = 0 ; i < n ; i++){
    scanf("%d" ,&a[i]);
  }
  heap_size = n;
  buildMaxHeap(a);
  for(i = 0 ; i < n ; i++){
    printf(" %d" ,a[i]);
  }
  printf("\n");

  return 0;
}

void Maximum_Heap(int a[],int p){
  int l = -1,r = -1,tmp,largest;

  if(2*p+1 < heap_size){
    l = 2*p+1;
  }

  if(2*p+2 < heap_size){
    r = 2*p+2;
  }

  if(l != -1 && a[l] > a[p]){
    largest = l;
  }else{
    largest = p;
  }

  if(r != -1 && a[r] > a[largest]){
    largest = r;
  }

  if(largest != p){
    tmp = a[p];
    a[p] = a[largest];
    a[largest] = tmp;

    Maximum_Heap(a,largest);
  }
}

void buildMaxHeap(int a[]){
  int i;

  for(i = heap_size/2-1 ; i >= 0 ; i--){
    Maximum_Heap(a,i);
  }
}