#include<stdio.h>

int n;

int parent(i){
  if(i==1) return -1;
  else return i/2;
}

int left(i){
  if(2*i > n) return -1;
  else return 2*i;
}

int right(i){
  if(2*i+1 > n) return -1;
  else return 2*i+1;
}

void maxHeapify(long int H[], int i){
  int l,r,largest;
  int x;
  l = left(i);
  r = right(i);

  if(l<=n && H[l]>H[i]) largest = l;
  else largest = i;
  if(r<=n && H[r]>H[largest]) largest = r;

  if(largest!=i){
    x = H[i];
    H[i] = H[largest];
    H[largest] = x;

    maxHeapify(H,largest);
  }
		      
}

void buildMaxHeap(long int H[]){
  int i;
  for(i=n/2;i>=1;i--) maxHeapify(H,i);
}
	       

int main(){

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

  long int H[n+1];

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

  buildMaxHeap(H);

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

