#include<stdio.h>
#define MAX 500001
#define NIL -100
struct Node{
  int key;
  int p;
  int l;
  int r;
};
struct Node A[MAX];
void buildMaxHeap(struct Node *);
void maxHeapify(struct Node *,int);
int n;

int main(){
  int i,x;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    A[i].p=NIL;
    A[i].l=NIL;
    A[i].r=NIL;
  }
  for(i=0;i<n;i++){
    scanf("%d",&A[i].key);
    if(i!=0){
      if(i%2==1)
	A[i].p=A[i/2].key;
      else A[i].p=A[i/2-1].key;
    }
  }
  for(i=0;i<=n/2;i++){
    if((2*i+2)<n)A[i].r=A[2*i+2].key;
    if((2*i+1)<n)A[i].l=A[2*i+1].key;
  }
  buildMaxHeap(A);
  for(i=0;i<n;i++){
    printf(" %d",A[i].key);
  }
  return 0;
}
void buildMaxHeap(struct Node *A){
  int i;
  for (i=n/2-1;i>=0;i--)
    maxHeapify(A, i);
}
void maxHeapify(struct Node *A,int i){
  int largest;
  struct Node T;
  if( i*2+1 < n && A[i*2+1].key > A[i].key){
    largest = i*2+1;
  }
  else largest = i;
  if(i*2+2 < n && A[i*2+2].key > A[largest].key)
    largest = i*2+2;
  if (largest != i){
    T.key=A[i].key;
    A[i].key=A[largest].key;
    A[largest].key=T.key;
    maxHeapify(A, largest);
  }
printf("\n");
}