#include <stdio.h>
#define MAX 500001

void maxHeapify(int);

int n;
int array[MAX];

int main(){
  int i;

  scanf("%d", &n);

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

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

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

  return 0;
}

void maxHeapify( int num ){
  int left, right, large, tmp;

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

  if( left <= n && array[left] > array[num] ) large = left;
  else large = num;

  if( right <= n && array[right] > array[large] ) large = right;

  if( large != num ){
    tmp = array[num];
    array[num] = array[large];
    array[large] = tmp;
    maxHeapify(large);
  }

}