(function main(){
  const lines = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n');
  const n = Number(lines.shift());
  const arr = lines.shift().split(' ').map(Number);
  let base_idx = partition(arr, 0, n - 1);
  arr[base_idx] = `[${arr[base_idx]}]`;
  console.log(arr.join(` `));
})();

function partition(arr, p, r) {
   let x  = arr[r];
   let i = p - 1;
   for (let j = p; j < r; j++) {
     if (arr[j] <= x) {
       i++;
       [arr[i], arr[j]] = [arr[j], arr[i]];
     }
   }
   [arr[i + 1], arr[r]] = [arr[r], arr[i + 1]];
   return i + 1;
}

