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

int part(int *a, int p, int r){
  int i, j, x, tmp;

  x = a[r];
  i = p-1;
  for(j = p; j < r; 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;
}

int main(){
  int i, j, q, n;

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

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

  for(j = 0; j < q; j++)
      printf("%d ", a[j]);
  printf("[%d] ", a[j]);
  for(j = j+1; j < n-1; j++)
    printf("%d ", a[j]);
  printf("%d\n", a[j]);


  free(a);
  return 0;
}