#include<stdio.h>
#define N 100000

int partition(int *, int, int);

int main(){
  int a[N];
  int n, kijun;
  int i;

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

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

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

  return 0;
}

int partition(int *a, int p, int r){
  int x, v;
  int i, j;
  x = a[r];
  i = p - 1;
  for(j=p;j<r;j++){
    if(a[j] <= x){
      i++;
      //a[i]とa[j]を交換
      v = a[i];
      a[i] = a[j];
      a[j] = v;
    }
  }
  //a[i+1]とa[r]を交換
  v = a[i+1];
  a[i+1] = a[r];
  a[r] = v;

  return i+1;
}

