#include<stdio.h>

#define NIL -2000000001

typedef struct heap{
  int k;
  int pk;
  int lk;
  int rk;
}Heap;

int H;

void maxHeapify(Heap* A,int i){
  int l,r,largest,x;
  l = 2*i;
  r = 2*i+1;
  if ((l <= H) &&( A[l].k > A[i].k))
    largest = l;
  else 
    largest = i;
  if ((r<= H)&&( A[r].k > A[largest].k))
    largest = r;
  if (largest != i){
    x=A[i].k;
    A[i].k=A[largest].k;
    A[largest].k=x;
    maxHeapify(A, largest);
  }
}

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

int main(){
  Heap A[500000];
  int i,p,r,l;
  
  scanf("%d",&H);

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

  for(i=1;i<=H;i++){
    p=i/2;
    l=2*i;
    r=2*i+1;
    
    if(p!=0)
      A[i].pk=A[p].k;
    else
      A[i].pk=NIL;
    

    if(r<=H)
      A[i].rk=A[r].k;
    else
      A[i].rk=NIL;

    
    if(l<=H)
      A[i].lk=A[l].k;
    else
      A[i].lk=NIL;
  }

  buildMaxHeap(A);

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

  return 0;
}

