#include<stdio.h>

int partition(int*,int,int);
int main(){

  int a[1000000],a_num,i,key,x;


  scanf("%d",&a_num);
  for(i = 0; i < a_num; i++){
    scanf("%d",&a[i]);
  }

  
  key = a[a_num-1];
  x =parttition(a,0,a_num-1);

  for(i = 0; i < x; i++){
    printf("%d ",a[i]);
  }
  printf("[%d]",key);
  for(i = x+1; i < a_num; i++){
    printf(" %d",a[i]);
  }
  printf("\n");
  return 0;
}


int parttition(int *a, int p, int last)
{
  int key,i,j,swap;

  key = a[last];
  i = p-1;


  for(j = p; j < last; j++){
    if(a[j] <= key){
      i = i+1;
      swap = a[i];
      a[i] = a[j];
      a[j] = swap;
    }
  }
      swap = a[i+1];
      a[i+1] = a[last];
      a[last] = swap;

      return i+1;
}