#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int *A;
int h;

int left(int i){
  return i * 2;
}

int right(int i){
  return (i * 2) + 1;
}

void maxHeapify(int *A,int i){
  int l,r,largest,a;
  l = left(i);
  r = right(i);

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

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


void buildMaxHeap(int *A,int length){
  int i,g;
  double f;
  f = floor(length / 2);
  g = (int)f;
  for(i = g;i >= 1;i--){
    maxHeapify(A,i);
  }
}

int main(){
  int i;

  scanf("%d",&h);

  A = malloc(sizeof(int)*(h+1));

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

  buildMaxHeap(A,h);

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

  return 0;
}