#include <stdio.h>

#define MAX 100000

int partition(int *, int, int);

int main() {
  int i, n, data[MAX], k;

  scanf("%d", &n);

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

  k = partition(data, 0, n-1);

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

  return 0;
}


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

  x = a[r];
  i = p-1;

  for (j = p; j < r; j++) {
    if (a[j] <= x) {
      i++;
      temp = a[i];
      a[i] = a[j];
      a[j] = temp;
    }
  }
  temp = a[i+1];
  a[i+1] = a[r];
  a[r] = temp;
  
  return i+1;
}

