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

int partition(int *A, int p, int r){
	int i, j = p-1, tmp;

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

	return j+1;
}

int main(){
	int n, q, i;
	int *A;

	scanf("%d", &n);
	A = (int *)malloc(sizeof(int) * n);
	for(i = 0; i < n; i++)
		scanf("%d", &A[i]);

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

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

	free(A);
	return 0;
}
