#include <stdio.h>
#define MAX 100000
int partition(int *a,int p,int r){
  int x;
  int i,j;
  int tmp;
  x=a[r];
  i=p-1;
  for(j=p;j<r;j++){
    if(a[j]<=x){
      i++;
      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;
}
int main(){
  int i,j;
  int n;
  int a[MAX];
  scanf("%d",&n);
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  j=partition(a,0,n-1);
  for(i=0;i<j;i++)printf("%d ",a[i]);
  printf("[%d] ",a[j]);
  for(i=j+1;i<n-1;i++)printf("%d ",a[i]);
  printf("%d",a[j]);
  printf("\n");
  return 0;
}