#include <stdio.h>
#include <stdlib.h>

int partition(int *, int, int);

int main()
{
  int n, i, index;
  int *a;

  scanf("%d", &n);
  a = (int *)malloc(n*sizeof(int));
  for(i=0; i<n; i++) {
    scanf("%d", &a[i]);
  }

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

  return 0;
}

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