#include<stdio.h>



int main(){
  int number;
  int array[10000];

  scanf("%d",&number);



  int i;
  for(i=1;i<=number;i++){
    scanf("%d",&array[i]);
  }


  int kazu; 
  kazu=partition(array,1,number); 


  for(i=1;i<=number;i++){


    if(i==number){printf("%d\n",array[i]);}  



    else{
      if(i==kazu){printf("[%d] ",array[i]);}
      else{printf("%d ",array[i]);}}
  }
 
  
  return 0;
}


int partition(int A[],int p,int r){

  int x;
  
  x=A[r];

   int i;
  i=p-1;

  
  int j;
  int tmp;
 

  
  for(j=p;j<r-1;j++){
    if(A[j]<=x){

      i=i+1;
      tmp=A[i];
      A[i]=A[j];
      A[j]=tmp;

    }
  }


  tmp=A[i+1];
  A[i+1]=A[r];
  A[r]=tmp;

  
  
  return i+1;
}

