import std.stdio;
import std.array;
import std.conv;
import std.string;
import std.range;
import std.algorithm;

int partition(ref int[] a, int p, int r) {
  int x = a[r];
  int i = p;

  for (int j = p; j < r; j++) {
    if (a[j] <= x) {
      swap(a[i], a[j]);
      i++;
    }
  }

  swap(a[i], a[r]);

  return i;
}

void main() {
  int n = readln.chomp.to!int;
  int[] a = readln.chomp.split.map!(to!int).array;
  int pivot = a.back;

  partition(a, 0, (a.length - 1).to!int);

  foreach (int i, int e; a) {
    if (i != 0) { " ".write; }
    if (e == pivot) { writef("[%d]", e); }
    else { e.write; }
  }

  writeln;
}