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

#define SWAP(a, b, v) v = a, a = b, b = v

int
partition(int *a, int p, int r)
{
	int x = a[r];
	int i, v;

	for (i = p; i < r; i++) {
		if (a[i] > x)
			continue;

		SWAP(a[p], a[i], v);
		p++;
	}

	SWAP(a[p], a[r], v);

	return p;
}

int
main(void)
{
	int i, n, q;
	int *a;

	scanf("%d", &n);

	a = (int *)malloc(sizeof(int) * n);
	if (a == NULL)
		return 1;

	for (i = 0; i < n; i++)
		scanf("%d", a + i);

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

	for (i = 0; i < q; i++)
		printf("%s%d", i ? " " : "", a[i]);
	printf(" [%d]", a[i++]);
	for (; i < n; i++)
		printf(" %d", a[i]);
	puts("");

	return 0;
}

