#include<stdio.h>
#include<stdlib.h>
int partition(int *, int, int);
main(){
  int n, i, c;
  int *A;
  scanf("%d", &n);
  A = (int *)malloc(sizeof(int)*n);
  if(A == NULL) exit(1);
  
  for(i = 0; i < n; i++){
    scanf("%d", &A[i]);
  }
  c = partition(A, 0, n);
  for(i = 0; i < n; i++){
    if     (i == n-1 && i != c) printf("%d\n",   A[i]);
    else if(i == n-1 && i == c) printf("[%d]\n", A[i]);
    else if(i != n-1 && i == c) printf("[%d] ",  A[i]);
    else if(i != n-1 && i != c) printf("%d ",    A[i]);
  }
  free(A);
  return 0;
}

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