#include <stdio.h>

void sort(int *x, int *y) {
  int c;
  c = *x;
  *x = *y;
  *y = c;
}

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

int main(void) {
  int n, a[100000];
  int q;
  
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  q = partition(a, 0, n - 1);

  for (int i = 0; i < n; i++) {
    if (i == q)
      printf("[%d]", a[i]);
    else
      printf("%d", a[i]);
    if (i != n - 1)
      printf(" ");
  }
  printf("\n");

  return 0;
}

