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

void swap(int* num_1, int* num_2) {
	int temp = *num_1;
	*num_1 = *num_2;
	*num_2 = temp;
}

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

	for (j = p; j < r; j++) {
		if (array[j] <= array[r]) {
			i++;
			swap(&array[i], &array[j]);
		}
	}
	swap(&array[i + 1], &array[r]);
	return i + 1;
}

int main() {
	int part, array_num, i;
	scanf("%d", &array_num);

	//dynamic scan
	int* array;
	array = (int*)malloc(array_num * sizeof(int));
	for (int i = 0; i != array_num; i++) {
		scanf("%d", &array[i]);
	}

	part = partition(array, 0, array_num - 1);

	for (i = 0; i != array_num - 1; i++) {
		if (i)printf(" ");
		if (i == part) printf("[");
		printf("%d", array[i]);
		if (i == part) printf("]");
	}
	printf(" %d\n", array[array_num - 1]);

	return 0;
}
