(function main(){
  const lines = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n');
  const n = Number(lines.shift());
  const arr = lines.shift().split(' ');
  function buildMaxHeap(arr) {
    const n = arr.length;
    const maxHeapify = (i) => {
      let left, right, largest = i;
      left = 2 * i + 1;
      right = 2 * i + 2;
      if (left < n && Number(arr[left]) > Number(arr[largest])) largest = left;
      if (right < n && Number(arr[right]) > Number(arr[largest])) largest = right;
      if (largest != i) {
        arr[largest] = arr[i] + (arr[i] = arr[largest], 0);
        maxHeapify(largest);
      }
    };
    for (let i = Math.floor(n / 2 - 1); i >= 0; i--) maxHeapify(i);
  }
  buildMaxHeap(arr);
  console.log(' ' + arr.join(' '));
})();

