#include<stdio.h>
#define MAX 500000

void maxHeapify(int *,int);
int H;

int main(){
  int i,a[MAX+1];
  scanf("%d",&H);

  for ( i = 1; i <= H; i++ ){
    scanf("%d", &a[i]);
  }
  for ( i = H/2; i>=1; i-- ){
    maxHeapify(a,i);
  }
  for ( i = 1; i <= H; i++ ) {
    printf(" %d", a[i]);
  }
  printf("\n");
  
  return 0;
}


void maxHeapify(int a[],int i){
  int l, r, largest, x;
  l = 2*i;
  r = 2*i+1;
  if ( l <= H && a[l] > a[i] ){
    largest = l;
  }
  else {
    largest = i;
  }
  if ( r <= H && a[r] > a[largest] ) {
    largest = r;
  }
  if ( largest != i ){
    x = a[i];
    a[i] = a[largest];
    a[largest] = x;
    maxHeapify(a,largest);
  }
}

