#include<stdio.h>

#define N 500001
#define NIL 2000000001

int heap[N],n;

void maxHeapify(int *heap, int i){
  int l,r,largest,a;
  
    l = 2*i; 
    r = 2*i+1;
  
    if(l<=n && heap[l]>heap[i]){
      largest = l;
    }
    else largest = i;
  
    if(r<=n && heap[r]>heap[largest]){
      largest = r;
    }

    if(largest!=i){
      a=heap[largest];
      heap[largest]=heap[i];
      heap[i]=a;
      maxHeapify(heap, largest);
    }
}

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

main(){
  int i;

  for(i=0;i<N;i++){
    heap[i]=NIL;
  }

  scanf("%d",&n);

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

  buildMaxHeap(heap);

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

  printf("\n");

  return 0;
}