#include<stdio.h>
#define SIZE 5000001

int A[SIZE];

void maxheap(int H,int i){
  int l,r,largest;

  l = i*2;
  r = i*2+1;

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

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

  if(largest != i){
    l = A[i];
    A[i] = A[largest];
    A[largest] = l;
    maxheap(H,largest);
  }
}

void build(int H){
  int i;
  for(i = H/2;i > 0;i--) maxheap(H,i);
}

int main(){
  int n,i;

  scanf("%d",&n);

  for(i = 1;i <= n;i++) scanf("%d",&A[i]);

  build(n);

  for(i = 1;i <= n;i++)printf(" %d",A[i]);

  printf("\n");

  return 0;
}

