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


int* scanArray( int arrayLength) {

  int i;
  int* a;
  
  a = malloc( sizeof(int) * arrayLength );
  for( i=0; i<arrayLength; i++) {
    scanf("%d", &a[i]);
  }

  return a;
  
}


void swap( int *n1, int *n2) {
  
  int tmp;

  tmp = *n1;
  *n1 = *n2;
  *n2 = tmp;

}


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

  int x;
  int i, j;

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

  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;

}


/*
void printArray( int *a, int arrayLength) {

  int i;

  for( i=0; i<(arrayLength-1); i++) {
    printf("%d ", a[i]);
  }
  printf("%d\n", a[i]);

}
*/


void printPartitionArray( int *a, int arrayLength, int shaft) {

  int i;

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

}



main() {

  int n, q;
  int* a;

  int i;

  scanf("%d", &n);
  
  a = scanArray( n);

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

  printPartitionArray( a, n, q);

  return 0;

}