#include <stdio.h>

#define MAX 500001

void max_H(int*,int,int);
void buildMax_H(int*,int);
void swap(int*,int*);

int main(){
  int H,i,a[MAX];
  scanf("%d",&H);
  for (i = 0 ; i < H ; i++) {
    scanf("%d",&a[i]);
  }
  buildMax_H(a,H);
  for(i = 0 ; i < H ; i++){
    printf(" %d",a[i]);
  }
  printf("\n");
  return 0;
}

void max_H(int *A,int i,int H){
  int l = i*2+1, r = i*2+2;
  int largest;
  if(l <= H && A[l] > A[i]){
    largest = l;
  }else{ largest = i; }

  if(r <= H && A[r] > A[largest]){
    largest = r;
  }
  if(largest != i){
    swap(&A[i],&A[largest]);
    max_H(A,largest,H);
  }
}

void buildMax_H(int *A,int H){
  int i;
  for(i = H/2 ; i >= 0 ; i--){
    max_H(A,i,H);
  }
}

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

