#include<stdio.h>
#include<stdlib.h>
int parttition(int*,int,int);
int main(){
  int i,n,*a,k;
  scanf("%d",&n);
  a=(int *)malloc(n *sizeof(int));
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  k=parttition(a,0,n-1);
  printf("%d",a[0]);
  for(i=1;i<n;i++){
    if(i==k)printf(" [%d]",a[i]);
    else printf(" %d",a[i]);
  }
  printf("\n");
  return 0;
}
int parttition(int a[],int p,int r){
  int i,j,x,key;
  x=a[r];
  i=p-1;
  for(j=p;j<r;j++){
    if(a[j]<=x){
      i++;
      key=a[i];
      a[i]=a[j];
      a[j]=key;
    }
  }
    key=a[i+1];
    a[i+1]=a[r];
    a[r]=key;

  return i+1;
}

