#include <stdio.h>
#define N 500001

int H;

void maxHeap(int *A,int i){
  int l,r;
  int tmp;
  int L;
  l = i * 2;
  r = i * 2 + 1;

  if(l <= H && A[l] > A[i])
    L = l;
  else L = i;

  if(r <= H && A[r] > A[L])
    L = r;

  if(L != i){
    tmp = A[i];
    A[i] = A[L];
    A[L] = tmp;
    maxHeap(A,L);
  }
}

void buildmaxHeap(int *A){
  int i;
  for(i = H/2 ; i >= 1 ; i--)maxHeap(A,i);
}
int main(){
  int i;
  int A[N];
  scanf("%d",&H);
  for(i = 1 ; i <= H ; i++)scanf("%d",&A[i]);
  buildmaxHeap(A);
  for(i = 1 ; i <= H ; i++)printf(" %d",A[i]);
  printf("\n");
  return 0;
}

