#include <stdio.h>

#define MAX 100000

int n, k;
int A[MAX];

void exchange(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

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

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

  return i+1;
}

int main() {
  int i, q;

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

  q = partition(A, 0, n-1);
  for (i = 0; i < n; i++) {
    if (i == n-1) {
      printf("%d\n", A[i]);
      break;
    } if (i == q) {
      printf("[%d] ", A[i]);
      continue;
    }
    printf("%d ", A[i]);
  }

  return 0;
}