#include<stdio.h>
#include<stdlib.h>
#define Max 2000000

int heap,A[Max+1];
void MaxHeapify(int);
void swap(int *,int *);
int main(){
  int i;
  scanf("%d",&heap);
  for( i=1;i<=heap;i++)
    scanf("%d",&A[i]);
  for( i=heap/2;i>=1;i--)MaxHeapify(i);
  for( i=1;i<=heap;i++){
    printf(" %d",A[i]);
  }
  printf("\n");

  return 0;
}
void MaxHeapify(int x){
  int left,right,maximum;
  left=2*x;
  right=2*x+1;
  if(left<=heap&&A[left]>A[x])maximum=left;
  else maximum=x;
  if(right<=heap&&A[right]>A[maximum])maximum=right;

  if(maximum !=x){
    swap(&A[x],&A[maximum]);
    MaxHeapify(maximum);
  }
}

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