#include <stdio.h>
#include <stdlib.h>
int A[100000],n,p;
int partition(int [],int ,int);
int main(){
  int i,r,q;

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

 
  q=partition(A,p,r);

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

  return 0;
}
int partition(int A[],int p,int r){
  int i,j,tmp,x;
  x=A[r];
  i=p-1;
  for(j=p;j<r;j++){
    if(A[j]<= x){
      i=i+1;
      tmp=A[j];
      A[j]=A[i];
      A[i]=tmp;
    }
  }
    tmp=A[i+1];
    A[i+1]=A[r];
    A[r]=tmp;

 
  return i+1;
}