#include<stdio.h>

int Partition(int A[], int p, int r){
  int x = A[r], i = p-1, swap,j;
  
  for(j=p;j<r;j++){
    if(A[j]<=x){
      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;
}

int main(void){
  int n, i, mk;
  
  scanf("%d", &n);
  
  int A[n];
  
  for(i=0;i<n;i++)
    scanf("%d", &A[i]);
  mk = Partition(A, 0, n-1);
  for(i=0;i<mk;i++)
    printf("%d ", A[i]);
  printf("[%d]", A[mk]);
  for(i=mk+1;i<n;i++)
    printf(" %d", A[i]);
  printf("\n");
  
  return 0;
} 

