#include <stdio.h>
#define N 100000
int partition(int,int);
int A[N];
int main(){
  int i,p=0,r,j;

  scanf("%d",&r);
  for(i=0;i<r;i++)scanf("%d",&A[i]);
 
  j=partition(p,r-1);
  
  for(i=0;i<r;i++){
    // if(i<r-1)printf("%d ",A[i]);
     if(i==j)printf("[%d] ",A[i]);
     else if(i<r-1)printf("%d ",A[i]);
     else printf("%d\n",A[i]);
  }
  return 0;
}
int partition(int p,int r){
  int x,i,j,y;
  x = A[r];
  i = p-1;
  
  for(j=p;j<r;j++){
    if(A[j]<=x){
      i++;
      y = A[i];
      A[i] =A[j];
      A[j]=y;
    }
  }
  y = A[i+1];
  A[i+1] = A[r];
  A[r] = y;
  //printf("a\n");
  return i+1;
}