#include<stdio.h>
#define N 1000000

int A[N],n;
void maxHeapify(int);
void buildMaxHeap(void);


int main(){

  int i;
  scanf("%d",&n);

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

   buildMaxHeap();

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

    return 0;
  }

  void maxHeapify(int i){

    int left,right,large,tmp;

    left = 2*i;
    right = 2*i + 1;

    if(left <= n && (A[left]>A[i]) ){
      large = left;
    }
    else large = i;

    if(right <= n && (A[right]>A[large]) ){
      large = right;
    }

    if(large != i){
      tmp = A[i];
      A[i] = A[large];
      A[large] = tmp;
      maxHeapify(large);
    }
  }

  
void buildMaxHeap(void){

  int i;
  for(i = (n/2);i >=1;i--){
    maxHeapify(i);
  }
}