#include <stdio.h>
#define h 500000
void maxheapify(int );

void buildmaxheapify(void);

int A[h];
int H;
int main(){

  int i;

  scanf("%d",&H);

  for(i = 1 ; i <= H ; i++){
    scanf("%d",&A[i]);
  }

  for(i = 1 ; i <= H ; i++) maxheapify(i);

  buildmaxheapify();

  for(i = 1 ; i <= H ; i++)printf(" %d",A[i]);
  printf("\n");
  return 0;
}

void maxheapify(int k){

  int r,l,largest,temp;
  l = 2*k;
  r = 2*k+1;

  if(l <= H && A[l] > A[k])
    largest = l;
  else largest = k;

  if(r <= H && A[r] > A[largest])
    largest = r;

  if(largest != k){
    temp = A[k];
    A[k] = A[largest];
    A[largest] = temp;

    maxheapify(largest);
  }
}

void buildmaxheapify(void){
  int i;
  for( i = H/2 ; i >= 1 ; i--)maxheapify(i);
}