#define MAX 100000
#include <stdio.h>

int partition(int *,int,int);
int main(int argc, char const *argv[]) {
  int a[MAX],n,res,i;

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

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

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

  return 0;
}

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