#include<stdio.h>
int maxHeapify(int);
int A[500010];
int l,r;
int N,i,L=0;
int key;

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

int maxHeapify(i){
  l=2*i;
  r=2*i+1;
  if(l<=N && A[l]>A[i])L=l;
  else L=i;
  if(r<=N && A[r]>A[L])L=r;
  if(L!=i){
    key=A[i];
    A[i]=A[L];
    A[L]=key;
    return maxHeapify(L);
  }
  return 0;
}