#include<stdio.h>
#define MAX 500001

int A[MAX];
int n;

void maxHeapify(int);
void buildMaxHeap(void);
int left(int);
int right(int);

int main(){

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

  return 0;
}

void maxHeapify(int i){
  int largest ,tmp, l, r;
  l=left(i);
  r=right(i);

  if((n+1 >l) && (A[l] > A[i])) largest=l;
  else largest=i;
  if((n+1>r) && (A[r]>A[largest])) largest=r;
  if (largest!=i){
    tmp=A[i];
    A[i]=A[largest];
    A[largest]=tmp;
    maxHeapify(largest);
  }
}

void buildMaxHeap(){
  int i;
  for (i=(n/2); i+1>1; i--){
    maxHeapify(i);
  }
}

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

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