#include<stdio.h>
/*????????? partition sort ??????*/
#define MAX 100000
int partition(int *, int, int);
main()
{
  int i,n,p,r,q=0;
  int A[MAX];

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

  r = n-1;
  p = 0;

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

}

int partition( int *a,int p, int r)
{
  int x,i,j,ex;
  printf("%d\n",r);

  x = a[r];
  i = p-1;

  for(j=p;j<r;j++)
    {
      if(a[j]<=x)
	{
	  i = i+1;
	  ex = a[i];
	  a[i] = a[j];
	  a[j] = ex;
	}
    }
  ex = a[i+1];
  a[i+1] = a[r];
  a[r] = ex;
  printf("%d\n",a[r]);

  return i+1;
}