#include <stdio.h>

int partition(int a[], int p, int r)
{
	int x, i, j, temp;

	x = a[r];
	i = p - 1;

	for (j = p; j < r ;j++) {
		if (a[j] <= x) {
			i++;
			temp = a[j];
			a[j] = a[i];
			a[i] = temp;
		}
	}
	temp = a[i + 1];
	a[i + 1] = a[r];
	a[r] = temp;

	return i + 1;
}


int main(void) {
	int n, r;
	int number[1000001];
	int i;

	scanf("%d", &n);

	for (i = 0; i < n; i++) {
		scanf("%d", &number[i]);
	}

	r = partition(number, 0, n - 1);

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

	return 0;
}
