#include<stdio.h>

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

int main()
{
	int A[100010];
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", &A[i]);
	int k = partition(A, 0, n - 1);
	for (int i = 0; i < n; i++)
	{
		if (i)
			printf(" ");
		if (i == k)
			printf("[");
		printf("%d", A[i]);
		if (i == k)
			printf("]");
	}
	printf("\n");
}
