#include <stdio.h>

void buildMaxHeap(int); // n??即i                                                                                 
void maxHeapify(int, int);
int heap[500000];

int main(void){
  int n,i;

  scanf("%d",&n); // heap_size                                                                                  
  for( i = 0 ; i < n ; i++ ){
    scanf("%d",&heap[i]);
  }

  buildMaxHeap(n);

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

  printf("\n");
  return 0;
}

void buildMaxHeap(int size){
  int i;
  for( i = size/2 ; i >= 0 ; i-- ){
    maxHeapify(i, size);
  }
}

void maxHeapify(int x, int size){
  int l,r,largest,tmp;
  l = x*2+1;
  r = x*2+2;

  if( l <= size && heap[l] > heap[x] ) largest = l;
  else largest = x;
  if( r <= size && heap[r] > heap[largest] ) largest = r;
  if( largest != x ){
    tmp = heap[x];
    heap[x] = heap[largest];
    heap[largest] = tmp;
    maxHeapify(largest, size);
  }
}