#include <stdio.h>

int swap(int *, int *);
int partition(int *, int, int);

int main() {
  int A[100001];
  int n, i, split;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  
  split = partition(A, 0, n - 1);

  for (i = 0; i < split; i++) {
    printf("%d ", A[i]);
  }
  printf("[%d] ", A[i]);

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

int swap(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

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

  for (j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      swap(&A[i], &A[j]);
    }
  }
  swap(&A[i + 1], &A[r]);

  return i + 1;
}

