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

void buildmaxheap(void);
void maxheapfy(int);
void swap(int,int);

int n,i;
int *tree;

int main(){


  scanf("%d",&n);
  tree = malloc((n+1) * sizeof(int));
  for(i=1;i<n+1;i++)
    scanf("%d",&tree[i]);
  buildmaxheap();
  
  for(i=1;i<n;i++)
    printf("%d ",tree[i]);
  printf("%d\n",tree[i]);
  
  return 0;
}


void buildmaxheap(void){

  int i;
  for(i = n / 2;i > 0;i--) maxheapfy(i);

}

void maxheapfy(int i){
  int l,r,larger;
  l = 2 * i;
  r = l + 1;

  if( l <= n && tree[l] > tree[i]) larger = l;
  else larger = i;
  if( r <= n && tree[r] > tree[larger]) larger = r;

  if(larger != i){
    swap(larger,i);
    maxheapfy(larger);
  }
  else return;
}

void swap(int larger,int i){
  int box;
  box = tree[larger];
  tree[larger]=tree[i];
  tree[i]=box;
}