#include <stdio.h>

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

int main(int argc, char const *argv[]) {

  int p=0;
  int n;
  scanf("%d",&n);
  int A[n];
  int i;
  int v;

  for( i = 0 ; i < n ; i++ ){
    scanf("%d",&A[i]);
  }

  v = partition(A,p,n);

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

  printf("[%d] ",A[v]);

  for( i = v+1 ; i < n-1 ; i++ ){
    printf("%d ",A[i]);
  }

  printf("%d\n",A[i]);



  return 0;
}

int partition(int *A, int p, int r){

  int x;
  int i,j;

  x = A[r-1];
  i = p-1;

  for( j = p ; j < r-1 ; j++ ){
    if( A[j] <= x ){
      i++;
      swap(&A[i],&A[j]);
    }
  }

  swap(&A[i+1],&A[r-1]);

  return i+1;

}

void swap(int *x, int *y){

  int temp;

  temp = *x;
  *x = *y;
  *y = temp;

}