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

int pertition(int *, int, int);

int partition(int *A, int p, int r)
{
  int x = A[r];
  int i = p - 1;
  int j, tmp;

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


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


  scanf("%d", &n);

  A = malloc(sizeof(int)*n);

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

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

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

      if(i != n - 1)
	{
	  printf(" ");
	}
      else
	{
	  printf("\n");
	}

    }
      free(A);
      return 0;
}