// Partition
#include <stdio.h>
#include <stdlib.h>

int partition(int *, int, int);

int main() {
  int i, n;
  int p, r, q;
  int *a;

  // input
  scanf("%d", &n);
  a = (int *)malloc(n * sizeof(int));
  for(i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  // partition
  p = 0;
  r = n - 1;
  q = partition(a, p, r);
  for(i = 0; i < n; i++) {
    if(i == q) printf("[");
    printf("%d", a[i]);
    if(i == q) printf("]");
    if(i == n - 1) printf("\n");
    else printf(" ");
  }
  return 0;
}

int partition(int *a, int p, int r) {
  int x, tmp;
  int i, j;

  x = a[r];
  i = p - 1;
  for(j = p; j < r; j++) {
    if(a[j] <= x) {
      i = i + 1;
      // exchange a[i] and a[j]
      tmp = a[i];
      a[i] = a[j];
      a[j] = tmp;
    }
  }
  // exchange a[i + 1] and a[r]
  tmp = a[i + 1];
  a[i + 1] = a[r];
  a[r] = tmp;

  return i+1;
}

