#include <stdio.h>
#define N 500000

int H, A[N];

void maxHeapify(int);

int main(){
  int i;
  scanf("%d",&H);

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

  for( i = H/2; i >= 1; i-- )
    maxHeapify(i);

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

  return 0;
}

void maxHeapify(int i){
  int left, right, largest, tmp;
  left = 2 * i;
  right = 2 * i + 1;

  if( left <= H && A[left] > A[i] )
    largest = left;
  else
    largest = i;
  if( right <= H && A[right] > A[largest] )
    largest = right;

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

