//2019.12.22
//s1270188 xxxmk2
//alds1_06b

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

//prototype
int partition(int,int);

//global
int *A, num;

int main(){
  int i, x;

  //input
  scanf("%d",&num);
  A=(int *)malloc(sizeof(int)*num);
  for(i=0;i<num;i++) scanf("%d",&A[i]);

  //paritition
  x=partition(0,num-1);

  //output
  for(i=0;i<num-1;i++){
    if(i==x) printf("[%d] ",A[i]);
    else printf("%d ",A[i]);
  }
  printf("%d\n",A[i]);
  
  free(A);
  return 0;
}

int partition(int p,int r){
  int x, i, j, sub;

  x=A[r];
  i=p-1;

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

  sub=A[i+1];
  A[i+1]=A[r];
  A[r]=sub;

  return i+1;
}


