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

int partition(int *, int, int);

int main()
{
  int a, i, n;
  int *A;
  
  scanf("%d",&n);

  A = (int *)malloc(sizeof(int)*n+1);
  
  for( i=0 ; i<n ; i++ )
    scanf("%d",&A[i]);

  a = partition(&A[0],0,n-1);

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

  free(A);

  return 0;
}

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