#include <stdio.h>
#define MAX 500001

void exchange(int *,int *);
void maxHeapify(int *,int,int);
void buildMaxHeap(int *,int);

int main(){
  int A[MAX],k,i;
  scanf("%d",&k);
  for(i=1;i<=k;i++) {scanf("%d",&A[i]);}
  buildMaxHeap(A,k);
  for(i=1;i<=k;i++) {printf(" %d",A[i]);}
  printf("\n");
  return 0;
}

void exchange(int *a,int *b){
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}

void maxHeapify(int A[],int i,int H){
  int c,d,largest;
  c = 2*i;
  d = 2*i+1;
  
  if (c <= H && A[c] > A[i]){ largest = c; }
  else 	{largest = i;}
  if (d <= H && A[d] > A[largest])
   { largest = d; }
  if (largest != i){ 
    exchange(&A[i],&A[largest]);
    maxHeapify(A, largest,H); 
  }
}

void buildMaxHeap(int A[],int H){
  int i;
  for (i = H/2;i>=1;i--){
    maxHeapify(A,i,H);
  }
}

