#include<stdio.h>
#define sizeH 500000

const int INF = 999999999;

typedef long int lint;

void maxHeap(lint H[], int i, int n){
  lint left,right;
  lint largeVal;
  lint temp;
  
  left = i*2;
  right = i*2+1;

  if(left <= n && H[left] > H[i]){
    largeVal = left;
  }
  else{
    largeVal = i;
  }

  if(right <= n && H[right] > H[largeVal]){
    largeVal = right;
  }

  if(largeVal != i){
    temp = H[i];
    H[i] = H[largeVal];
    H[largeVal] = temp;

    maxHeap(H, largeVal, n);
  }
}

void buildMaxHeap(lint H[], int n){
  int i;
  for(i = n/2;i > 0;i--){
    maxHeap(H, i, n);
  }
}

int main()
{
  int i;
  int n;
  lint key;
  lint H[sizeH*2+1]={};
  
  scanf("%ld",&n);

  for(i = 1;i < n+1;i++){
    scanf("%ld",&key);
    H[i] = key;
  }
  
  buildMaxHeap(H, n);

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

