#include<stdio.h>

#define MAX 500000

struct node{
  int key;
};

int h;

int Parent(int i){
  if(i == 1) return 0;
  return i / 2;
}

int Left(int i){
  if(h < (2 * i)) return 0;
  else return 2 * i;
}

int Right(int i){
  if(h < (2 * i + 1)) return 0;
  return (2 * i) + 1;
}

//void problem1(struct node *n){
//  int i;
//  for(i = 1; i <= h; i++){
//    printf("node %d: key = %d, ",i,n[i].key);
//    if(Parent(i) != 0)
//      printf("parent key = %d, ",n[Parent(i)].key);
//    if(Left(i) != 0)
//      printf("left key = %d, ",n[Left(i)].key);
//    if(Right(i) != 0)
//      printf("right key = %d, ",n[Right(i)].key);
//    printf("\n");
//  }
//}

void maxHeapify(struct node *A, int i){
  int l,r,largest,buf;

  l = Left(i);
  r = Right(i);

  if(l <= MAX && A[l].key > A[i].key)
    largest = l;
  else
    largest = i;

  if(r <= MAX && A[r].key > A[largest].key)
    largest = r;

  if(largest != i){
    buf = A[i].key;
    A[i].key = A[largest].key;
    A[largest].key = buf;
    maxHeapify(A,largest);
  }
    
}

void buildMaxHeap(struct node *A){
  int i;
  for(i = h / 2; i > 0; i--)
    maxHeapify(A,i);
}

int main(){
  int i;
  struct node n[MAX];

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

  buildMaxHeap(n);
  for(i = 1; i <= h; i++)
    printf(" %d",n[i].key);
  printf("\n");
  
  return 0;
}

