#include <stdio.h>
#include <stdlib.h>
 
int partition(int*, int);
 
int main() {
  int i, n, key;
  int *a;
   
  scanf("%d",&n);
  a = (int*)malloc(sizeof(int) * n);
  for(i = 0;i < n;i++) 
    scanf("%d",&a[i]);
  key = partition(a, n);
  for(i = 0;i < n;i++) {
    if(i == key) printf("[%d]",a[i]);
    else printf("%d",a[i]);
    if(i != n-1) printf(" ");
  }
  printf("\n");
  free(a);
  return 0;
}
 
int partition(int* a, int r) {
  int j, change;
  int x = a[r-1];
  int i = -1;
  int k;
  for(j = 0;j < r-1;j++) {
    if(a[j] <= x) {
      i++;
      change = a[i];
      a[i] = a[j];
      a[j] = change;
    }
  }
  change = a[i+1];
  a[i+1] = a[r-1];
  a[r-1] = change;
  return i+1;
}