#include <stdio.h>

#define N 100000

int partition(int *, int, int);
void swap(int *x, int *y){
  int temp;
  temp = *x;
  *x = *y;
  *y = temp;
}

int main(){
  int n, i, a[N], b;
  scanf("%d",&n);
  for(i = 0; i < n; i++){
    scanf("%d",&a[i]);
  }

  b = partition(a,0,n-1);

  for(i = 0; i < n-1; i++){
    if(i == b){
      printf("[%d] ",a[i]);
      continue;
    }
    printf("%d ",a[i]);
  }
  printf("%d\n",a[i]);

  return 0;
}

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

