#include <stdio.h>

int partition(int a[], int p, int r)
{
  int x = a[r];
  int i = p-1;
  for (int j = p; j <= r-1; j++) {
    if (a[j] <= x) {
      i++;
      int temp = a[i];
      a[i] = a[j];
      a[j] = temp;
    }
  }
  int temp = a[i+1];
  a[i+1] = a[r];
  a[r] = temp;
  return i+1;
}

int main(void)
{
  int n, i, x;
  int a[100000];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  x = partition(a, 0, n-1);
  printf("%d", a[0]);
  for (i = 1; i < n; i++) {
    if (i == x)
      printf(" [%d]", a[i]);
    else
      printf(" %d", a[i]);
  } printf("\n");
}

