#include<stdio.h>
#define N 100000

int partition(int [], int, int);

int main()
{
  int i=0,num,q,A[N];

  while(1)
    {
      scanf("%d",&num);
      if(num >= 1 && num <= N) break;
    }

  while(i<num)
    {
      scanf("%d",&A[i]);
      if(A[i] >= 1 && A[i] <= N) i++;
    }

  q = partition(A,0,num);

  for(i=0;i<num-1;i++)
    {
      if(i == q)
	  printf("[%d] ",A[i]);
      else
	printf("%d ",A[i]);
    }
  printf("%d\n",A[i]);
  
  return 0;
}

int partition(int a[],int p, int r)
{
  int i,j,x,swap;
  x = a[r-1];
  i = p;

  for(j = p; j < r; j++)
    {
      if(a[j] <= x)
	{
	  swap = a[i];
	  a[i] = a[j];
	  a[j] = swap;
	  i++;
	}
    }
  swap = a[i+1];
  a[i+1] = a[r-1];
  a[r-1] = swap;

  return i;
  
}

