#include <stdio.h>
#define N 500000

int n,tree[N];

void Swap(int *a,int *b){
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}

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

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

void maxHeapify(int i){
  int l;
  l = LeftC(i);
  int r;
  r = RightC(i);
  int largest;
  if(l <= n && tree[l] > tree[i]) largest = l;
  else largest = i;
  if (r <= n && tree[r] > tree[largest]) largest = r;
  if(largest != i) {
    Swap(&tree[i],&tree[largest]);
    maxHeapify(largest);
  }
}

int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++) scanf("%d",&tree[i]);
  for(i=n/2;i>=1;i--) maxHeapify(i);
  for(i=1;i<=n;i++) printf(" %d",tree[i]);
  printf("\n");
  return 0;
}