#include<stdio.h>
#define MAX 100010

int A[MAX];

int Partition(int A[], int sp, int ep) {	//A[sp] ~ A[ep]
	int x = A[ep];
	int i = sp - 1;
	int buff;
	for (int j = sp; j < ep; j++) {
		if (A[j] <= x) {
			i++;
			buff = A[i];
			A[i] = A[j];
			A[j] = buff;
		}
	}
	A[ep] = A[i + 1];
	A[i + 1] = x;
	return i + 1;
}

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

