#include <stdio.h>

#define N 100000

int partition(int *,int,int);
int h;
int main()
{
  int n,i,j,p,r,A[N],k;

  scanf("%d",&n);

  for(i=0;i<n;i++) scanf("%d",&A[i]);
  p = 0;
  r = n-1;

  k = partition(A,p,r);

  for(i=0;i<k;i++) printf("%d ",A[i]);
  printf("[%d] ",A[k]);
  for(j=k+1;j<n-1;j++) printf("%d ",A[j]);
  printf("%d\n",A[n-1]);

  return 0;
}

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

  for(j=p;j<r;j++){
    if(A[j] <= x){
      i = i+1;
      k = A[j];
      A[j] = A[i];
      A[i] = k;
    }
  }
  k = A[i+1];
  A[i+1] = A[r];
  A[r] = k;
  return i + 1;
}
      
  

  

