void maxHeapify(int );
void buildmaxHeap();
int x;
int A[2000001];
int main(){

  int i;

  scanf("%d",&x);

  for(i=1;i<=x;i++)
    scanf("%d",&A[i]);

  buildmaxHeap();


  for(i=1;i<x+1;i++)
    printf(" %d",A[i]);

  printf("\n");

  return 0;
}
void maxHeapify(int i)
{
  int l=0,r=0,largest=0,w=0;

  l=i*2;
  r=i*2+1;

  if(l <= x && A[l] > A[i])
    largest = l;

  else largest = i;

  if(r <= x && A[r] > A[largest])
    largest = r;

  if(largest != i){
    w=A[i];
    A[i]=A[largest];
    A[largest]=w;
    maxHeapify(largest);
  }
}
void buildmaxHeap()
{
  int i;
  for(i=(x/2);i>=1;i--)
    maxHeapify(i);
}