#include <stdio.h>

#define N 100001
int A[N];
int n;

int partition(int *, int, int);

main() {
  int i, p, part;
  scanf("%d", &n);
  
  p = 1;
  for(i = p; i < n + 1; i++) {
    scanf("%d", &A[i]);
  }
  
  part = partition(A, p, n);
  
  for(i = p; i < n + 1; i++) {
    if(i == n && i == part) {
      printf("[%d]\n", A[i]);
    }
    else if(i == n && i != part) {
      printf("%d\n", A[i]);
    }
    else if(i == part && i != n) {
      printf("[%d] ", A[i]);
    }
    else {
      printf("%d ", A[i]);
    }
  }
  return 0;
}

int partition(int *A, int p, int r) {
  int i, j, x, tmp;
  x = A[r];
  i = p - 1;

  for(j = 1; j < r; j++) {
    if(A[j] <= x) {
      i = i + 1;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i + 1];
  A[i + 1] = A[r];
  A[r] = tmp;

  return i + 1;
}