
#include<stdio.h>

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

void maxHeapify(int A[],int i,int n){
  int l = left(i);
  int r = right(i);
  int large;
  int tmp;

  if(l <= n && A[l] > A[i]){
    large = l;
  }else{
    large = i;
  }
  if(r <=n && A[r] > A[large]){
    large = r;
  }

  if(large != i){
    tmp = A[i];
    A[i] = A[large];
    A[large] = tmp;
    maxHeapify(A,large,n);
  }
}


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

int main(){
  int i;
  int n;
  int A[500000];

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

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