#include<stdio.h>
#include<stdlib.h>
int *A;
int partition(int p,int *A, int r);
int main(){
  int r,i,j,k,p=0;
  
  scanf("%d",&r);
  A =malloc(sizeof(int)*r);

  
  for(i=0;i<r;i++){
    scanf("%d",&A[i]);
  }

 k = partition(p,A,r);


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

    else{
      printf("%d ",A[j]);
    }
  }
  
  return 0;
}



  int partition(int p,int *A,int r){
  int tmp,tmp2;
  int x,i,j;
  x = A[r-1];
  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-1];
  A[r-1] = tmp2;

  
  return i + 1;
  }













 
  

