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

#define N 100000
#define MAX 100000 

int partition(int *, int, int);

int A[MAX];


int main()
{

  int i, n, a, j;

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

  a = partition(A, 0, n-1);

  for(j=0; j<n; j++){
    if(j == a) printf("[%d] ", A[a]);
    else if(j == n-1) printf("%d\n", A[j]);
    else printf("%d ", A[j]);

   }  

  return 0;

}

int partition(int *A, int p, int r)
{

  int i, x, j, tmp1, tmp2;

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

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

    tmp2 = A[i+1];
    A[i+1] = A[r];
    A[r] = tmp2;

    return i+1;

}