#include<stdio.h>
#include<stdlib.h>

int partition(int*, int, int);

int main()
{
  int *A;
  int i, n, base;

  scanf("%d", &n);
  A = (int *)malloc(n * sizeof(int));

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

  base = partition(A, 0, n-1);

  for(i=0;i<n;i++)
    {
      if(i)printf(" ");
      if(i == base)printf("[");
      printf("%d", A[i]);
      if(i == base)printf("]");
    }

  printf("\n");

  free(A);
  return 0;
}

int partition(int *A, int p, int r)
{
  int base, swap, i, j;
  base = A[r];
  i = p-1;
  for(j=p;j<r;j++)
    {
      if(A[j] <= base)
        {
          i++;
          swap = A[i]; A[i] = A[j]; A[j] = swap;
        }
    }
    swap = A[i+1]; A[i+1] = A[r]; A[r] = swap;
    return i+1;
}

