#include <stdio.h>

#define MAX 100000

int partition(int*,int,int);

int main(){
	int array[MAX] = {};
	int i,ans,times;
	
	scanf("%d\n", &times);
	
	for ( i = 0; i < times; i++ ) scanf("%d ", &array[i]);
	
	ans = partition(array, 0, times-1);
	for ( i = 0; i < times - 1 ; i++ ) {	
		if ( i == ans ) printf("[%d] ",array[i]);
        else printf("%d ",array[i]);
	}
	printf("%d\n",array[i]);
	return 0;
}

int partition( int *array, int p, int r ){
	int num,i,j,kari;

	num = array[r];
	i = p - 1;
	for ( j = p; j < r; j++ ) {
		if ( array[j] <= num ) {
			i = i+1;
			kari = array[i];
			array[i] = array[j];
			array[j] = kari;
		}
	}

	kari = array[r];
	array[r] = array[i+1];
	array[i+1] = kari;

	return i+1;
}
