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;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-1;

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

  return i;
}
