#include<stdio.h>
int partition(int *,int,int);
#define N 100000
int main(){ 
  int n,arr[N];
  int i,num;
  scanf("%d",&n);
  for(i = 0;i < n;i++){
    scanf("%d",&arr[i]);
  }
  num = partition(arr,0,n-1);

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

  return 0;
}

int partition(int* A, int p, int r){
  int x,i,j,k,n;  
  x = A[r];
  i = p-1;
  for (j = p;j < r;j++){
    if (A[j] <= x){
      i = i+1;
      k = A[i];
      A[i] = A[j];
      A[j] = k;
    }
  }
  k = A[r];
  A[r] = A[i+1];
  A[i+1] = k;
  return i+1;
}