#include<stdio.h>
#define MAX 500001

void heap(int);
int n, i, tree[MAX];

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

  return 0;
}

void heap(int x){
  int largest, l, r, temp;
    l = 2*x;
    r = 2*x+1;
    if(l <= n && tree[l] > tree[x]) {
      largest = l;
    }else{
       largest = x;
    }
    if(r <= n && tree[r] > tree[largest]){
      largest = r;
    }
    if(largest != x){
        temp = tree[x];
        tree[x] = tree[largest];
        tree[largest] = temp;
        heap(largest);
    }
}

