#include <stdio.h>

#define N 500000

int a;

int parent(int);
int left(int);
int right(int);
void maxHeapify(int*,int);
void buildMaxHeap(int*);

main(){

  int i,A[N];

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

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

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


int right(int i){
  return 2 * i +1;
}
void maxHeapify(int* A,int i){
  int x,l,r,largest;

  l = left(i);
  r = right(i);

  if(l <= a && A[l] > A[i]) largest = l;
  else largest = i;

  if(r <= a && A[r] > A[largest]) largest = r;

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

void buildMaxHeap(int* A){
  int i;
  for(i = a / 2;i >= 1; i--){
    maxHeapify(A,i);
  }
}