#include<stdio.h>

#define N 5000000

int left(int);
int right(int);
void maxHeapify(int);
void buildMaxheap(void);

int heap,A[N];

int main(){
  int i;
  scanf("%d",&heap);

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

  buildMaxheap();

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


int left(int i){
  return i*2;
}

int right(int i){
  return 2*i+1;
}

void maxHeapify(int i){
  int l,r,max,con;
  l = left(i);
  r = right(i);

  if(l <= heap && A[l] > A[i]){
    max = l;
  }
  else max=i;
  if(r <= heap && A[r] > A[max]){
    max = r;
  }
  if(max != i){
    con = A[max];
    A[max]=A[i];
    A[i]=con;
    maxHeapify(max);
  }
}

void buildMaxheap(){
  int i;
  for(i = heap/2;i > 0;i--){
    maxHeapify(i);
  }
}