#include <stdio.h>
#include <stdlib.h>
#define H 500000

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

  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]);
    if(i!=n) printf(" ");
    else printf("\n");
  }

  return 0;
}


void maxHeapify(int i){
  int l,r;
  int largest;
  int s;

  l=2*i;
  r=2*i+1;


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

  if(r <=n && A[r] > A[largest]){
    largest=r;
  }

  if(largest!=i){
    s=A[i];
    A[i]=A[largest];
    A[largest]=s;
    maxHeapify(largest);
  }
}

void buildMaxHeap(){
  int j;
  for(j=n/2; j >=1; j--){
    maxHeapify(j);
  }
}