#include <stdio.h>

#define Max 100000

void  swap(int *x, int *y)
{
    int  buff;
	
	buff = *x;
	*x = *y;  
	*y = buff;
}

void printA(int A[Max], int N, int q) {
	int i;

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

int partition(int A[Max], int p, int r) {
	int x = A[r];
	int i = p-1;
	int j;
	
	for (j = p; j < r; j++)
		if (A[j] <= x) swap(&A[++i], &A[j]);

	swap(&A[i + 1], &A[r]);

	return i + 1;
}

int main( ) {
	int N, i, q;
	int A[Max];

	scanf("%d", &N);

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

	q = partition(A, 0, N - 1);

	printA(A, N, q);

	return 0;
}