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

struct node{
  //int id;
  int k;
  //int pk;
  //int lk;
  //int rk;
};
typedef struct node *Node;

int largest;

int parent(i){
  return floor(i/2);
}

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

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

void maxHeapify(Node A,int i, int H){
  struct node b;
  int l = left(i)+1;
  int r = right(i)+1;
  if(l <= H && A[l].k > A[i].k)
    largest = l;
  else{
    largest = i;
  }
  if(r<=H && A[r].k > A[largest].k){
    largest = r;
  }

  if(largest != i){
    b = A[i];
    A[i] = A[largest];
    A[largest] = b;
    maxHeapify(A, largest, H);
  }
}

void buildMaxHeap(Node A, int H){
  int i;
  for(i=H/2-1; i>=0; i--){
    maxHeapify(A, i, H);
  }
}

int main(){

  int H, i, K, P;
  Node n;
  //int *A;
  scanf("%d",&H);

  //A = (int *)malloc(sizeof(int)*H);
  n = (Node)malloc(sizeof(Node)*H);

  for(i=0; i<H; i++){
    scanf("%d", &K);
    n[i].k = K;
    }

  buildMaxHeap(n,H);

  for(i=0; i<H; i++){
    printf(" %d",n[i].k);
  }
  printf("\n");

  return 0;
}
