#include<stdio.h>
#include<stdlib.h>
int partition(int*,int,int);

int main()
{
  int n,i,q;
  int *a;
  
  scanf("%d",&n);
  a=(int*)malloc(sizeof(int)*n);
  for(i=0;i<n;i++) scanf("%d",&a[i]);
  q = partition(a,0,n-1);
  for(i=0;i<n;i++) {
    if(i>0) printf(" ");
    if(i==q) printf("[%d]",a[i]);
    else printf("%d",a[i]);
  }
  printf("\n");
  free(a);
}

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