#include<stdio.h>

#define MAX 100000

int partition(int,int);
 
int arr[MAX];

int main(){

  int i,n,x;

  scanf("%d",&n);
  
  for(i=0;i<n;i++){
    scanf("%d",&arr[i]);
  }
  
  x = partition(0,n-1);
  
  for(i=0;i<x;i++){
    printf("%d ",arr[i]);
  }

  printf("[%d] ",arr[x]);
  
  
  for(i=x+1;i<n-1;i++){
    printf("%d ",arr[i]);
  }
  printf("%d\n",arr[n-1]);

  return 0;
}


int partition(int a,int b){

  int x,i,j,tmp;

  x = arr[b];
  i = a - 1;

  for(j=a;j<b;j++){
    if(arr[j]<=x){
      i++;
      tmp=arr[i];
      arr[i]=arr[j];
      arr[j]=tmp;
    }
  } 
  tmp=arr[i+1];
  arr[i+1]=arr[b];
  arr[b]=tmp; 
  return i+1;
}