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

void swap (unsigned short  *A,int i,int j){
  int cmp;
  cmp=A[i];
  A[i]=A[j];
  A[j]=cmp;
}

int partition(unsigned short *A,int p,int r){
  int x = A[r];
  int j,i = p-1;

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

  return i+1;
}


int main(){
  unsigned short *A;
  int n,i,ren;

  scanf("%d",&n);

  A = malloc(sizeof(short)*n + 1 );

  for(i=1;i<=n;++i) scanf("%d",&A[i]);
  
  ren = partition(A,1,n);

  for(i=1;i<ren;i++)printf("%d ",A[i]);
 
  printf("[%d] ",A[ren]);
  for(i=ren+1;i<n;++i)printf("%d ",A[i]);
  printf("%d\n",A[n]);

  return 0;
  
}


