#include<stdio.h>
#define N 100000

int partition(int A[], int p, int r)
{
  int x,i,j,ch1,ch2;
  x = A[r];
  i = p-1;
  for(j=p; j<r; j++){
    if(A[j] <= x){
      i = i+1;
      ch1 = A[i];
      A[i] = A[j];
      A[j] = ch1;
    }
  }
      ch2 = A[i+1];
      A[i+1] = A[r];
      A[r] = ch2;
      return i+1; 
}
int main()
{
  int i,n,A[N],p=0,r,res;

  scanf("%d",&n);
  r=n-1;
  for(i=0; i<n; i++){
    scanf("%d",&A[i]);
  }
  res = partition(A,p,r);
  for(i=0; i<n-1; i++){
    if(i!=n-1) printf(" ");
    if(i==res)printf("[");
    printf("%d",A[i]);
    if(i==res)printf("] ");
  }
  printf("%d\n",A[n-1]);
  return 0;
}

