#include <stdio.h>

#define N 255

int A[N];
int n;

int parent(int);
int left(int);
int right(int);
void maxHeapify(int);
void buildMaxHeap();

int main(){

  int i;

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

  buildMaxHeap();

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


int parent(int i){
  return (i+1)/2-1;
}

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

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

void maxHeapify(int i){
  int l = left(i);
  int r = right(i);
  int largest,key;
  
  if(l<n && A[l]>A[i]){
    largest = l;
  }else{
    largest = i;
  }
  if(r<n && A[r]>A[largest]){
    largest = r;
  }

  if(largest!=i){
    key = A[largest];
    A[largest] = A[i];
    A[i] = key;   
    maxHeapify(largest);
  } 
}

void buildMaxHeap(){
  int i;
  for(i=n/2-1;i>=0;i--){
    maxHeapify(i);
  }
  
}