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

int Partition(int *A, int f, int l){
  int i, j, x;
  i = f-1;
  for(j = f; j < l; j++){
    if(A[j] < A[l]){
      i = i+1;
      x = A[j];
      A[j] = A[i];
      A[i] = x;
    }
  }
    x = A[i+1];
    A[i+1] = A[l];
    A[l] = x;
  return i+1;
}
int main(){
  int i, n, m, *A;
  scanf("%d",&n);
  A = malloc(sizeof(int)*n);
  for(i = 0; i < n; i++) scanf("%d",&A[i]);
  m = Partition(A,0,n-1);
  for(i = 0; i < n; i++){
    if(i > 0) printf(" ");
    if(i == m){
      printf("[%d]",A[i]);
      continue;
    }
    printf("%d",A[i]);
  }
  printf("\n");
  return 0;
}