#include <stdio.h>

#define MAX 100000

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

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

int partition( int *A, int p, int r ) 
{
	int x = 0;
	int i = 0;
	int j = 0;
	int tmp = 0;
	
	x = A[r];
	i = p - 1;
	for ( j = p; j < r; j++ ) {
		if ( A[j] <= x ) {
			i = i+1;
			tmp = A[i];
			A[i] = A[j];
			A[j] = tmp;
		}
	}
	
	tmp = A[r];
	A[r] = A[i+1];
	A[i+1] = tmp;
	return i+1;
}
