#include<stdio.h>
#include<stdlib.h>

void maxHeapify(int *,int,int);
void swap(int *,int,int);

int main(){
  int i,n,*A;
  scanf("%d",&n);
  A = malloc(sizeof(int)*n);
  for(i=0;i<n;i++)scanf("%d",&A[i]);
  for(i=(n-1)/2;i>=0;i--)maxHeapify(A,i,n);
  for(i=0;i<n-1;i++)printf(" %d",A[i]);
  printf(" %d\n",A[n-1]);
  free(A);
  return 0;
}

void maxHeapify(int *A,int i,int n){
  int l,r,max;
  l = i*2+1;
  r = (i+1)*2;
  if(l < n && A[l]>A[i])max = l;
  else max = i;
  if(r < n && A[r]>A[max])max = r;
  if(max != i){
    swap(A,i,max);
    maxHeapify(A,max,n);
  }
}

void swap(int *A,int i,int j){
  int tmp;
  tmp = A[i];
  A[i] = A[j];
  A[j] = tmp;
}

