#include<stdio.h>

#define MAX 1000000
int h,array[MAX+1];


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

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

void maxHeap(int A[],int i){
  int l,r,largest;
  int temp;
  l = left(i);
  r = right(i);

  if(l <= h && A[l] > A[i]){
    largest = l;
  }else{
    largest = i;
  }
  if(r <= h && A[r] > A[largest]){
    largest = r;
  }
  if(largest != i){
    temp = A[i];
    A[i] = A[largest];
    A[largest] = temp;
    maxHeap(A,largest);
  }
    
}

void buildMaxHeap(int A[]){
  int i;
  for(i = h/2;i >= 1;i--){
    maxHeap(A,i);
  } 
}


int main(){
  int i;

  scanf("%d",&h);
  for(i = 1;i<=h;i++){
    scanf("%d",&array[i]);

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

