include<stdio.h>
#define N 10000
int partition(int*, int ,int);
int A[N];
int main(){

  int i,num,mid;

  scanf("%d",&num);
  for(i=0;i<num;i++){

    scanf("%d",&A[i]);
  }


   mid = partition(A,0,num-1);
  for(i=0;i<num-1;i++){

    if(i==mid){
      printf("[ %d ]",A[i]);
    }
      else  printf("%d ",A[i]);
int partition(int *A, int p, int r){

  int x,tmp,tmp2,j,i;

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

  for(j=p; j<=r-1;j++){


    if(A[j]<=x){

      i=i+1;
      tmp=A[i];
      A[i]=A[j];
      A[j]=tmp;
    }
  }
    tmp2 = A[i+1];
    A[i+1]=A[r];
    A[r]=tmp2;


  return i+1;
}


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