#include <stdio.h>

int swap(int *, int *);
int partition(int *, int, int);

int main() {
  int a[100001];
  int n,i,s;

  scanf("%d", &n);

  for(i = 0; i < n; i++){
    scanf("%d", &a[i]);
  }
  
  s = partition(a, 0, n - 1);

  for(i = 0; i < s; i++){
    printf("%d ", a[i]);
  }
  printf("[%d] ", a[i]);

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

int swap(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

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++;
      swap(&a[i], &a[j]);
    }
  }
  swap(&a[i + 1], &a[r]);

  return i + 1;
}


