#include <stdio.h>
#include<stdlib.h>

int n;

void maxHeapify(int *a, int i){
  int l = 2*i, r = 2*i+1, largest;
  if(l <= n && a[l] > a[i]) largest = l;
  else largest = i;
  if(r <= n && a[r] > a[largest]) largest = r;
  if(largest != i){
    a[i] += a[largest];
    a[largest] = a[i] - a[largest];
    a[i] -= a[largest];
    maxHeapify(a, largest);
  }
  return;
}

int main(){
  int a[500005], i;

  scanf("%d", &n);
  for(int i=1   ; i<=n ; ++i)   scanf("%d", a + i);
  for(int i=n/2 ; i>0  ; --i)   maxHeapify(a, i);
  for(int i=1   ; i<=n ; ++i)   printf(" %d", *(a + i));
  puts("");
  return 0;
}

