#include<stdio.h>
#include<stdlib.h>
int H[2000000000];

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

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

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

void print(int *H,int i,int n){
  int l,r,p;
  l=left(i);
  r=right(i);
  p=parent(i);
  printf("node %d: key = %d, ",i,H[i]);
  if(i==1){
    if(l<=n){
      printf("left key = %d, ",H[l]);
    }
    if(r<=n){
      printf("right key = %d, ",H[r]);
    }

  }
  else {
    if(p<=n){
      printf("parent key = %d, ",H[p]);
    }
    if(l<=n){
      printf("left key = %d, ",H[l]);
    }
    if(r<=n){
      printf("right key = %d, ",H[r]);
    }


  }
  printf("\n");
}

void maxHeapify(int *H,int i,int n){
  int largest,temp,l,r;
  l=left(i);
  r=right(i);
  if(l<=n&&(H[l] > H[i])){
    largest = l;
}
  else{
    largest=i;
}
  if(r <= n&&(H[r] > H[largest])){
    largest = r;
}
  if(largest!=i){
    temp=H[i];
    H[i]=H[largest];
    H[largest]=temp;
    maxHeapify(H,largest,n);
}
}

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

int main(){
  int i,n;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&H[i]);
  }
  buildMaxHeap(H,n);
  /* for(i=1;i<=n;i++){
    print(H,i,n);
    }*/
  for(i=1;i<=n;i++){
    printf(" %d",H[i]);
}
  printf("\n");
  return 0;
}

